Skip to main content
Queries are JSON strings that describe what documents to find. The simplest form specifies field-value pairs: The most common way to search is by providing field values directly. This approach is recommended for most use cases and provides intelligent matching behavior.
// Search for a term in a specific field
await index.query({
  filter: {
    name: "headphones",
  },
});

// Search across multiple fields (implicit AND)
await index.query({
  filter: {
    name: "wireless",
    category: "electronics",
  },
});

// Search with exact values for non-text fields
await index.query({
  filter: {
    inStock: true,
    price: 199.99,
  },
});

Smart Matching for Text Fields

When you provide a value directly to a text field (without explicit operators), the search engine applies smart matching:
  • Single-word values: Performs a term search, matching the word against tokens in the field.
  • Multi-word values: Combines phrase matching, term matching, and fuzzy matching with different boost weights to rank exact phrases highest while still finding partial matches.
  • Double-quoted phrases: Forces exact phrase matching (e.g., "\"noise cancelling\"" matches only those words adjacent and in order).
For more control, use explicit operators like $phrase, $fuzzy, or $contains.

Query Options

The SEARCH.QUERY command supports several options to control result format and ordering.

Pagination with Limit and Offset

Limit controls how many results to return. Offset controls how many results to skip. Used together, these options provide a way to do pagination.
// Page 1: first 10 results (with optional offset)
const page1 = await index.query({
  filter: {
    description: "wireless",
  },
  limit: 10,
});

// Page 2: results 11-20
const page2 = await index.query({
  filter: {
    description: "wireless",
  },
  limit: 10,
  offset: 10,
});

// Page 3: results 21-30
const page3 = await index.query({
  filter: {
    description: "wireless",
  },
  limit: 10,
  offset: 20,
});

Sorting Results

Normally, search results are sorted in descending order of query relevance. It is possible to override this, and sort the results by a certain field in ascending or descending order. Only fields defined as FAST in the schema can be used as the sort field. When using SORTBY, the score in results reflects the sort field’s value rather than relevance.
// Sort by price, cheapest first
await products.query({
  filter: {
    category: "electronics",
  },
  orderBy: {
    price: "ASC",
  },
});

// Sort by date, newest first
await articles.query({
  filter: {
    author: "john",
  },
  orderBy: {
    publishedAt: "DESC",
  },
});

// Sort by rating, highest first, which can be combined with LIMIT and OFFSET
await products.query({
  filter: {
    inStock: true,
  },
  orderBy: {
    rating: "DESC",
  },
  limit: 5,
});

Controlling Output

By default, search results include document key, relevance score, and the contents of the document (including the non-indexed fields). For JSON and string indexes, that means the stored JSON objects as whole. For hash indexes, it means all fields and values. It is possible to get only document keys and relevance scores using NOCONTENT.
// Return only keys and scores
await products.query({
  filter: {
    name: "headphones",
  },
  select: {},
});
It is also possible to select only the specified fields of the documents, whether they are indexed or not.
// Return specific fields only
await products.query({
  filter: {
    name: "headphones",
  },
  select: {
    name: true,
    price: true,
  },
});

Highlighting

Highlighting allows you to see why a document matched the query by marking the matching portions of the document’s fields. By default, <em> and </em> are used as the highlight tags.
// Highlight matching terms
await products.query({
  filter: {
    description: "wireless noise cancelling",
  },
  highlight: {
    fields: ["description"],
  },
});

// Custom open and close highlight tags
await products.query({
  filter: {
    description: "wireless",
  },
  highlight: {
    fields: ["description"],
    preTag: "!!",
    postTag: "**",
  },
});
Note that, highlighting only works for operators that resolve to terms, such as term or phrase queries.