---
title: "Search your data"
description: "Find rows by the words in a text field: a search builder checked against your schema, a ready-made search query, and useSearch to drive it from an input."
canonical_url: "https://nuxt-backend.dev/docs/build/data/read-write/search"
---
# Search your data

> Find rows by the words in a text field: a search builder checked against your schema, a ready-made search query, and useSearch to drive it from an input.

Version: nuxt-backend 0.6.x · Tab: Build · Kind: how-to · Status: stable

Every page: [llms.txt](https://nuxt-backend.dev/llms.txt) · Skill for coding agents: [nuxt-backend](https://nuxt-backend.dev/.well-known/skills/nuxt-backend/SKILL.md)

Commands use npm; use your project's package manager.

- [API reference](https://nuxt-backend.dev/docs/reference/api/search)
- [Source](https://github.com/qruto/nuxt-backend/blob/main/src/convex/integrations/search.ts)

Convex ranks the rows of a table by how well a text field matches the words typed, live, through a search index you declare beside the table. The package adds a builder over it whose index, field and filter names are checked against your schema, a one-line search query for public data, and `useSearch`, which drives either from an input with a debounce.

## Declare a search index

```ts [backend/schema.ts]
import { defineSchema, defineTable } from 'convex/server'
import { v } from 'convex/values'

export default defineSchema({
  // …your other tables
  messages: defineTable({ text: v.string(), userId: v.string() })
    .searchIndex('search_text', { searchField: 'text', filterFields: ['userId'] }),
})
```

`searchField` is the text that is searched; `filterFields` are the fields a search can narrow by exactly, such as the owner or the workspace. Convex matches whole words and the prefix of the last one, ranks by relevance, and returns at most 1,024 rows per search ([its limits](https://docs.convex.dev/search/text-search#limits)).

## Search in a function

`search(ctx, table)` from `nuxt-backend/search` builds the read. Search a person's own rows in a tier that knows who they are:

```ts [backend/messages.ts]
import { search } from 'nuxt-backend/search'
import { v } from 'convex/values'
import { authed } from './functions'

export const searchMine = authed.query({
  args: { query: v.string() },
  handler: (ctx, { query }) => search(ctx, 'messages')
    .withSearchIndex('search_text')
    .search('text', query)
    .eq('userId', ctx.user.id)
    .take(20),
})
```

`.eq` accepts only the index's `filterFields`, and chains for each one. Finish with `.take(n)`, `.collect()` (every ranked match) or `.first()` (the top match, or `null`). A blank term returns `[]` or `null` without touching the database.

## A ready-made search query

`defineSearch(query, { table, index, searchField, defaultLimit? })` turns the same read into a query that takes `{ query, limit? }` and returns up to `limit` matches (`defaultLimit`, 20 unless you set it):

```ts [backend/search.ts]
import { defineSearch } from 'nuxt-backend/search'
import { query } from './_generated/server'

export const searchArticles = defineSearch(query, {
  table: 'articles',
  index: 'search_body',
  searchField: 'body',
})
```

<warning>

A query made by `defineSearch` searches the whole table for whoever calls it, with no filter. Use it for data everyone may read, such as published articles or help pages. Search anything that belongs to a person or a workspace in a function of your own, behind its tier and filtered by its owner, as above.

</warning>

## Drive it from an input

`useSearch(query, options)` runs any query that takes `{ query: string }`: one from `defineSearch`, or your own. It waits for typing to pause, pauses while the term is blank, and keeps the results live:

```vue
<script setup lang="ts">
import { api } from '#backend/api'

const term = ref('')
const { results, isLoading, error } = useSearch(api.messages.searchMine, { term, debounce: 200 })
</script>

<template>
  <input v-model="term" type="search" placeholder="Search messages…">
  <p v-if="error">{{ error.message }}</p>
  <p v-else-if="isLoading">Searching…</p>
  <ul>
    <li v-for="message in results" :key="message._id">{{ message.text }}</li>
  </ul>
</template>
```

<table>
<thead>
  <tr>
    <th>
      Option
    </th>
    
    <th>
      Default
    </th>
    
    <th>
      Effect
    </th>
  </tr>
</thead>

<tbody>
  <tr>
    <td>
      <code>
        term
      </code>
    </td>
    
    <td>
      (required)
    </td>
    
    <td>
      The search term: a value, a ref or a getter. Blank, whitespace only, <code>
        null
      </code>
      
       or <code>
        undefined
      </code>
      
       pauses the search
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        debounce
      </code>
    </td>
    
    <td>
      <code>
        200
      </code>
    </td>
    
    <td>
      Milliseconds of quiet typing before the term is sent; also a ref or a getter
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        args
      </code>
    </td>
    
    <td>
      none
    </td>
    
    <td>
      Merged with <code>
        { query }
      </code>
      
       on every request: <code>
        { limit }
      </code>
      
       for a <code>
        defineSearch
      </code>
      
       query, or the arguments your own query declares
    </td>
  </tr>
</tbody>
</table>

It returns `results` (`[]` while loading, while the term is blank and after a failed read), `isLoading` (a term is in flight with no result yet), `error` (why the search failed, as `{ code, data, message }`, or `null`) and `term` (the debounced term driving the query). A failed search never throws ([Composable conventions](https://nuxt-backend.dev/docs/reference/nuxt/conventions)).

## Related

- [Your data](https://nuxt-backend.dev/docs/build/data/your-data): the tables and tiers a search reads through.
- [Keep counts and sums](https://nuxt-backend.dev/docs/build/data/read-write/aggregates): how many rows match, without reading them.
- [Query and update data](https://nuxt-backend.dev/docs/build/data/read-write): the live queries `useSearch` is built on.
- [Convex](https://nuxt-backend.dev/docs/services/convex): search indexes count toward your deployment's limits.
- [`nuxt-backend/search`](https://nuxt-backend.dev/docs/reference/api/search) and [`useSearch`](https://nuxt-backend.dev/docs/reference/api/use-search): the API reference.


## Sitemap

See the full [sitemap](https://nuxt-backend.dev/sitemap.md) for all pages.
