---
title: Search text
description: Find rows by the words they contain with a table's full-text index, with match modes, search as you type, snippets and scores.
related:
  - /data/define-tables
  - /data/query-across-lunas
  - /sdk/db
---

A `like` condition finds a word only where it appears exactly, and reads every row to do it. A full-text index finds rows by their words, ranks the best matches first and marks where each word appears.

## Search a table

Add `search` to a table's options and name the text columns to index. The table then has a `search` method.

```ts luna/lunas/board.ts
import { defineLuna, t, table } from "luna";

export const Board = defineLuna({
  name: "Board",
  key: { id: t.string() },
  schema: {
    cards: table(
      { id: t.id(), title: t.string(), body: t.string(), lane: t.string() },
      { search: { columns: ["title", "body"] } },
    ),
  },
  handlers: (h) => ({
    find: h({ args: { q: t.string() }, query: true }, (ctx, { q }) =>
      ctx.db.cards.search(q).map((hit) => hit.row),
    ),
  }),
});
```

`search` returns hits, best first, each with the matching `row` and a `score`. It returns at most 20 unless you pass `limit`, and `offset` skips hits for the next page.

Luna keeps the index in step with every insert, update and delete, including those you make with SQL. Search columns are `t.string()`, `t.enum()` or `t.json()` columns.

## Match all words, any word or a phrase

By default a row must contain every word of the query. `match` changes how the words combine.

| `match` | Finds rows with |
| --- | --- |
| `"all"` | Every word, anywhere. The default |
| `"any"` | At least one of the words |
| `"phrase"` | The words together, in the order given |
| `"raw"` | A query in SQLite's FTS5 syntax, passed through as written |

```ts luna/lunas/board.ts
ctx.db.cards.search(q, { match: "any", limit: 10 });
```

Outside `"raw"`, Luna quotes each word, so punctuation a person types never breaks the query.

## Search as you type

With `prefix: true`, the last word also matches words that start with it. A search for `"buy mi"` finds a card titled `Buy milk`.

```ts luna/lunas/board.ts
suggest: h({ args: { typed: t.string() }, query: true }, (ctx, { typed }) =>
  ctx.db.cards
    .search(typed, { prefix: true, limit: 5 })
    .map((hit) => hit.row.title),
),
```

## Narrow the results with where

`where` adds conditions on other columns, with the operators of [Read and write rows](/data/read-and-write-rows).

```ts luna/lunas/board.ts
ctx.db.cards.search(q, { where: { lane: "todo" } });
```

## Show snippets of the matches

`snippet` names a search column to cut a short passage from, with the matching words marked. Each hit then has a `snippet`.

```ts luna/lunas/board.ts
ctx.db.cards
  .search(q, { snippet: "body" })
  .map((hit) => ({ id: hit.row.id, snippet: hit.snippet, score: hit.score }));
```

The words are wrapped in `<mark>` and `</mark>`, and a cut is shown as `…`. Pass an object to change the marks and the length, from 1 to 64 words, 12 by default.

```ts luna/lunas/board.ts
ctx.db.cards.search(q, {
  snippet: { column: "body", open: "**", close: "**", tokens: 20 },
});
```

## Rank hits by score

Hits come back sorted by `score`, best first. A higher score is a better match, by the BM25 ranking that SQLite computes for each search.

## Pick a tokenizer

The tokenizer decides how text splits into searchable terms. Set it with `tokenizer` beside `columns`.

| `tokenizer` | Matches |
| --- | --- |
| `"unicode61"` | Whole words, ignoring case and accents. `cafe` finds `Café`. The default |
| `"porter"` | English word stems. `run` finds `running` |
| `"trigram"` | Any part of a word, three characters or longer, in any language |

Changing the columns or the tokenizer rebuilds the index from the rows, in each luna, as it moves to the new version.

## Keep only the words with indexOnly

Some text is needed only to be found, such as a long body kept in full elsewhere. List those columns in `indexOnly` to keep their words in the index but not their text in the table.

```ts luna/lunas/board.ts
cards: table(
  { id: t.id(), title: t.string(), body: t.string().optional() },
  { search: { columns: ["title", "body"], indexOnly: ["body"] } },
),
```

An index-only column must be optional. It reads back as `null`, and it gives no snippets.

:::warning
Write an index-only column on every insert, and on every update that changes another search column of the row. An update that changes `title` without `body` drops the body's words from the index, and the row is no longer found by them.
:::

## Next steps

- [Query across lunas](/data/query-across-lunas) searches the rows of many lunas at once.
- [Search vectors](/data/search-vectors) finds rows by meaning instead of words.
- [ctx.db](/sdk/db) lists every option of `search`.
