> ## Documentation Index
> Fetch the complete documentation index at: https://docs.lambdadb.ai/llms.txt
> Use this file to discover all available pages before exploring further.

# Substring filter

> Filter documents whose text field contains an exact string, including spaces, punctuation, and parts of words, using a substring index enabled when the collection is created.

A `substring` clause matches documents whose text field contains a given string exactly. Use it for literal lookups that text analysis does not handle, such as order numbers, code identifiers, log fragments, or part of a word. It is a filter: it does not affect scores, and it can be combined with any other query.

[Query string search](https://docs.lambdadb.ai/guides/search/query-string.md) splits text into terms with the field's analyzers and ranks matches. A substring filter compares the stored text instead: `Order #A-1024` matches only text that contains those characters in that order, and `efund` matches `refund`.

The examples on this page are REST request bodies.

## Enable the substring index

Set `substringIndex: true` on a `text` field when you create the collection. This request body for [creating a collection](https://docs.lambdadb.ai/guides/collections/create-a-collection.md) enables it on three fields:

```json theme={null}
{
  "collectionName": "support-tickets",
  "indexConfigs": {
    "subject": { "type": "text", "analyzers": ["english"], "substringIndex": true },
    "body": { "type": "text", "analyzers": [], "substringIndex": true },
    "status": { "type": "keyword" },
    "metadata": {
      "type": "object",
      "objectIndexConfigs": {
        "note": { "type": "text", "substringIndex": true }
      }
    }
  }
}
```

* `substringIndex` is allowed on `text` fields only, at the top level or inside an object. Sending it on any other field type, even as `false`, returns `400`. Omitting it, `false`, and `null` all leave the index off.
* It works alongside `analyzers`. `subject` and `metadata.note` (which uses the default `["standard"]`) can be searched with query strings and filtered by substring. `body` sets `"analyzers": []`, so it is indexed for substring filters only and is not searchable with query strings.
* The substring index adds indexing work and index size for the field.

You can enable the index only when the collection is created. It cannot be added to, changed on, or removed from an existing field, and a [schema update](https://docs.lambdadb.ai/guides/collections/manage-collections.md#update-the-index-schema) that adds a new field with `substringIndex: true` returns `400`. To filter an existing field by substring, create a new collection with the option and import the documents again.

## Filter with a substring clause

Place a `substring` clause wherever a query clause is accepted. This [query](https://docs.lambdadb.ai/reference/api/endpoint/document-query.md) request body returns open tickets whose `body` contains `Order #A-1024`:

```json theme={null}
{
  "query": {
    "bool": [
      { "substring": { "field": "body", "value": "Order #A-1024" }, "occur": "filter" },
      { "queryString": { "query": "status:open" }, "occur": "filter" }
    ]
  },
  "size": 10
}
```

| Parameter | Description | Type | Required | Default |
| :- | :- | :- | :- | :- |
| field | A `text` field with `substringIndex: true`. Use a dotted path, such as `metadata.note`, for a field inside an object. | string | ✓ | |
| value | The string to find: 1 to 8,192 UTF-8 bytes, matched as written, including spaces, punctuation, and line breaks. | string | ✓ | |
| ignoreCase | Compare without case, using the case folding described below. Omitted or `null` means `false`. | boolean or null | | `false` |

### What matches

A document matches when its string value of `field` contains `value` as a contiguous sequence of Unicode code points. The value is not analyzed: there is no tokenization or stemming, and `*`, `?`, and other characters have no special meaning.

* By default the comparison is case-sensitive.
* With `ignoreCase: true`, both sides are compared after Unicode 16.0.0 simple case folding (statuses `C` and `S` of `CaseFolding.txt`), which maps one code point to one code point. Text is not normalized and full case folding is not applied, so `ß` does not match `ss`, `ẞ` matches `ß`, and a precomposed `é` does not match `e` followed by a combining accent. If you compare results with a client-side search, a client that uses a later Unicode version can fold characters assigned after Unicode 16.0.0 differently.
* A document without the field, or with `null`, does not match.
* An unpaired surrogate in a document's text is matched as U+FFFD. A `value` that contains an unpaired surrogate is rejected.

### Placement and scoring

A `substring` clause is accepted in a `bool` at any depth and with any `occur`, in `knn.filter`, inside the subqueries of a [hybrid query](https://docs.lambdadb.ai/guides/search/hybrid.md), in queries with [facets](https://docs.lambdadb.ai/guides/search/facets.md), in [list filters](#list-documents-with-a-substring-filter), and in [delete filters](#delete-documents-by-substring).

The clause never contributes to the score, and a `boost` on it has no effect. Under `filter` or `must` it restricts the results, and under `must_not` it excludes matching documents. A query whose only clause is `substring` returns its matches with a score of 0. Following the [boolean query](https://docs.lambdadb.ai/guides/search/boolean.md#occur) rules, a `should` substring clause beside a `must` or `filter` clause changes neither the results nor the scores.

Substring filters read the same documents as other filters: a [consistent read](https://docs.lambdadb.ai/guides/data-versioning/branches-tags-aliases.md#consistent-reads) of a branch includes its pending writes, and a read of a tag includes the pending writes the tag captured.

## List documents with a substring filter

An [extended list request](https://docs.lambdadb.ai/guides/documents/list-data.md#extended-list-request) accepts a substring filter on its own or inside a `bool`:

```json theme={null}
{
  "size": 100,
  "filter": {
    "substring": { "field": "body", "value": "refund", "ignoreCase": true }
  }
}
```

With a substring filter, a page can hold fewer than `size` documents, or none, while `nextPageToken` is present. The page ends early when checking candidates reaches the [verification budget](#limits-and-errors), and the token continues from where the check stopped. Keep requesting pages until `nextPageToken` is absent; a short or empty page is not the end of the list.

## Delete documents by substring

A [delete by query](https://docs.lambdadb.ai/guides/documents/delete-data.md#delete-documents-by-query) accepts a substring filter and deletes every matching document:

```json theme={null}
{
  "filter": {
    "substring": { "field": "body", "value": "[test-fixture]" }
  }
}
```

Until the delete is indexed, reads that include pending writes check the documents it can remove, and that work counts toward the read's verification budget. A broad pending substring delete can make such reads return the verification error until indexing catches up.

## Limits and errors

LambdaDB uses the substring index to find candidate documents and then checks each candidate's stored text. A value of one or two code points expands to every indexed term that starts with it. These budgets bound that work:

| Budget | Limit |
| :- | :- |
| Index terms expanded for a one- or two-code-point value | 65,536 |
| Candidate documents checked | 10,000 |
| Stored document data read to check candidates | 256 MiB |

The candidate and stored-data budgets together make up the verification budget. The budgets apply to each search LambdaDB runs to serve a request, so a request that reads both published data and pending writes can check more in total.

* A query that exceeds a budget returns `400` with no partial results.
* A list request that reaches the verification budget [ends the page early](#list-documents-with-a-substring-filter) instead. Exceeding the index-term budget, or exceeding the verification budget while checking a pending substring delete, still returns `400`.
* When every substring clause of a query is reached only through `filter` or `must`, and the query has no facets or hybrid fusion, LambdaDB checks candidates in result order and stops once it has `size` results. The cost then depends on the non-matching candidates ranked before those results. In `knn.filter` and in queries with facets, every candidate is checked, so a common value can exceed the budget. `should` and `must_not` positions can also check more candidates.
* To stay within budget, combine the substring clause with selective filters, such as a `keyword` condition or a [`partitionFilter`](https://docs.lambdadb.ai/guides/search/search-overview#select-partitions-to-search), and use longer values.

Every error below returns HTTP `400` with the message shown:

| Condition | Message |
| :- | :- |
| `field` has no substring index, is not a text field, or does not exist | `Field '<field>' has no 'substringIndex'; it can only be enabled when the collection is created` |
| `value` is empty, longer than 8,192 UTF-8 bytes, or not a string | `'substring' value must be a nonempty string of at most 8192 UTF-8 bytes` |
| `value` contains an unpaired surrogate | `'substring' value must not contain unpaired surrogates` |
| `ignoreCase` is not a boolean or `null` | `'substring' ignoreCase must be a JSON boolean` |
| The clause body is not an object, has an unknown key, or has a non-string `field` | `'substring' must be a JSON object`, `'substring' contains unknown field: <key>`, or `'substring' field must be a JSON string` |
| A short value expands to too many index terms | `'substring' value matches too many index terms; use a longer value` |
| Checking candidates exceeds a budget | `'substring' filter needs to verify too many documents; add more selective filters or use a longer value` |
| `substringIndex` on a field that is not `text` | `substringIndex is not allowed for <type> field type` |
| A schema update adds a field with `substringIndex: true` | `'substringIndex' can only be enabled when the collection is created` |
| A new text field has `"analyzers": []` without `substringIndex: true` | `Text field '<field>' must enable at least one index: a nonempty 'analyzers' list or 'substringIndex: true'` |

See [Limits](https://docs.lambdadb.ai/guides/get-started/limits.md#search-and-reads) for the other search limits.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.