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

# Substring indexes

> Index String properties by trigram so contains, grep and regex filters read candidates instead of scanning

Guide

A substring index stores the three-character windows (trigrams) of one `String`
property, or of every `String` property, on the nodes or edges of a label. Filters that
look for text inside a value, such as `contains`, `grep`, `matches` and Cypher `=~`, can
then read the records that might match instead of scanning every record of the label.

Results stay exact. The index only proposes candidates, and every candidate is checked
against the original filter, so a query returns the same rows with or without the index.

## Create a substring index

```rust Rust theme={"languages":{"custom":["languages/helixql.json"]}}
write_batch()
    .var_as(
        "index",
        g().create_index_if_not_exists(IndexSpec::node_substring("Resource", "payload")),
    )
    .returning(["index"]);
```

```ts TypeScript theme={"languages":{"custom":["languages/helixql.json"]}}
writeBatch()
  .varAs("index", g().createIndexIfNotExists(IndexSpec.nodeSubstring("Resource", "payload")))
  .returning(["index"]);
```

```go Go theme={"languages":{"custom":["languages/helixql.json"]}}
helix.WriteQuery("create_resource_substring_index").
	VarAs(
		"index",
		helix.G().CreateIndexIfNotExists(helix.NodeSubstringIndex("Resource", "payload")),
	).
	Returning("index")
```

```python Python theme={"languages":{"custom":["languages/helixql.json"]}}
(
    write_batch()
    .var_as(
        "index",
        g().create_index_if_not_exists(IndexSpec.node_substring("Resource", "payload")),
    )
    .returning(["index"])
)
```

```json JSON theme={"languages":{"custom":["languages/helixql.json"]}}
{
  "request_type": "write",
  "query_name": "create_resource_substring_index",
  "query": {
    "write": {
      "entries": [{
        "query": {
          "name": "index",
          "root": {
            "create_index": {
              "spec": {
                "node_substring": {
                  "label": "Resource",
                  "source": { "property": "payload" }
                }
              },
              "if_not_exists": true
            }
          }
        }
      }],
      "returns": ["index"]
    }
  }
}
```

Use `edge_substring` (`edgeSubstring`, `EdgeSubstringIndex`) for an edge label, and
drop either with `drop_index` and the same spec. The index builds in the background,
like every index; its operation status reports the family `substring`.

## Index every String property

One all-strings index serves text filters on any `String` property of the label,
including properties that only appear in later writes. Use it when queries search
several properties, or any of them.

```rust Rust theme={"languages":{"custom":["languages/helixql.json"]}}
g().create_index_if_not_exists(IndexSpec::node_substring_all_strings("Resource"))
```

```ts TypeScript theme={"languages":{"custom":["languages/helixql.json"]}}
g().createIndexIfNotExists(IndexSpec.nodeSubstringAllStrings("Resource"))
```

```go Go theme={"languages":{"custom":["languages/helixql.json"]}}
helix.G().CreateIndexIfNotExists(helix.NodeSubstringAllStringsIndex("Resource"))
```

```python Python theme={"languages":{"custom":["languages/helixql.json"]}}
g().create_index_if_not_exists(IndexSpec.node_substring_all_strings("Resource"))
```

```json JSON theme={"languages":{"custom":["languages/helixql.json"]}}
{ "node_substring": { "label": "Resource", "source": "all_strings" } }
```

The edge forms are `edge_substring_all_strings`, `edgeSubstringAllStrings` and
`EdgeSubstringAllStringsIndex`. A label can have both kinds: a property's own index
serves filters on that property, because it holds only that property's text, and the
all-strings index serves every other property.

To search for one pattern in any `String` property, combine one condition per
property with OR. Conditions with the same pattern read the index once:

```rust Rust theme={"languages":{"custom":["languages/helixql.json"]}}
let needle = || GrepPattern::literal("timeout").case_insensitive();
g().n_with_label("Resource").where_(Predicate::and(vec![
    Predicate::eq("tenant", "t1"),
    Predicate::or(vec![
        Predicate::matches("name", needle()),
        Predicate::matches("payload", needle()),
        Predicate::matches("owner", needle()),
    ]),
]))
```

```cypher Cypher theme={"languages":{"custom":["languages/helixql.json"]}}
MATCH (r:Resource)
WHERE r.tenant = 't1'
  AND (r.name =~ '(?i).*timeout.*' OR r.payload =~ '(?i).*timeout.*'
       OR r.owner =~ '(?i).*timeout.*')
RETURN r
```

```ts TypeScript theme={"languages":{"custom":["languages/helixql.json"]}}
const needle = () => GrepPattern.literal("timeout").caseInsensitive();
g().nWithLabel("Resource").where(Predicate.and([
  Predicate.eq("tenant", "t1"),
  Predicate.or([
    Predicate.matches("name", needle()),
    Predicate.matches("payload", needle()),
    Predicate.matches("owner", needle()),
  ]),
]))
```

```go Go theme={"languages":{"custom":["languages/helixql.json"]}}
needle := func() helix.GrepPattern { return helix.GrepLiteral("timeout").CaseInsensitive() }
helix.G().NWithLabel("Resource").Where(helix.PredAnd(
	helix.PredEq("tenant", "t1"),
	helix.PredOr(
		helix.PredMatches("name", needle()),
		helix.PredMatches("payload", needle()),
		helix.PredMatches("owner", needle()),
	),
))
```

```python Python theme={"languages":{"custom":["languages/helixql.json"]}}
needle = lambda: GrepPattern.literal("timeout").case_insensitive()
g().n_with_label("Resource").where(Predicate.and_([
    Predicate.eq("tenant", "t1"),
    Predicate.or_([
        Predicate.matches("name", needle()),
        Predicate.matches("payload", needle()),
        Predicate.matches("owner", needle()),
    ]),
]))
```

```json JSON theme={"languages":{"custom":["languages/helixql.json"]}}
{
  "where": {
    "input": {
      "nodes_where": {
        "predicate": {
          "eq": { "left": { "property": "$label" }, "right": { "constant": { "string": "Resource" } } }
        }
      }
    },
    "predicate": {
      "and": {
        "predicates": [
          { "eq": { "left": { "property": "tenant" }, "right": { "constant": { "string": "t1" } } } },
          {
            "or": {
              "predicates": [
                { "matches": { "value": { "property": "name" },
                  "pattern": { "literal": { "text": { "value": { "string": "timeout" } }, "case_insensitive": true } } } },
                { "matches": { "value": { "property": "payload" },
                  "pattern": { "literal": { "text": { "value": { "string": "timeout" } }, "case_insensitive": true } } } },
                { "matches": { "value": { "property": "owner" },
                  "pattern": { "literal": { "text": { "value": { "string": "timeout" } }, "case_insensitive": true } } } }
              ]
            }
          }
        ]
      }
    }
  }
}
```

## Index the prefilter properties too

Queries usually narrow by equality before they search text, for example by tenant and
application. Create [equality indexes](https://docs.helix-db.com/database/helix-db/query-guides/secondary-indexes.md)
on those properties as well. The substring index then checks only candidates inside the
records the equality indexes select. Without them the equality conditions are evaluated
row by row, which is still exact but slower.

## Indexed values

* Only `String` values are indexed. Other values, `DateTime` included, are skipped and
  never fail a write.
* A write that stores more than 4 MiB of text in one indexed property fails. With an
  all-strings index every `String` property counts on its own, so several properties
  may hold more than 4 MiB together.
* An all-strings index skips properties whose names start with `$`.
* A filter on a dotted path such as `meta.title` reads a value nested in an object,
  which no substring index holds, so it scans the records instead.

## Storage version

Creating the first substring index moves the database's index storage format from
version 8 to version 9, in the same commit that creates the index. A database without a
substring index stays at version 8. Version 9 is one-way: releases that support at most
version 8 refuse to open the database, and dropping every substring index does not move
it back. Upgrade every reader before you create the first substring index.

## Next steps

> [card] [Filtering](https://docs.helix-db.com/database/helix-db/query-guides/filtering.md)
>
> Combine text conditions with equality and range conditions.

> [card] [Secondary indexes](https://docs.helix-db.com/database/helix-db/query-guides/secondary-indexes.md)
>
> Index the equality properties your queries filter on.

> [card] [Text indexes](https://docs.helix-db.com/database/helix-db/query-guides/text-indexes.md)
>
> Rank records by keyword relevance instead of matching text exactly.

> [card] [Troubleshoot index operations](https://docs.helix-db.com/database/helix-db/query-guides/troubleshooting.md#index-operation-is-blocked)
>
> Resolve blocked builds and lifecycle errors.


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