Back to Lance

N-gram Index

docs/src/format/index/scalar/ngram.md

9.0.01.8 KB
Original Source

N-gram Index

N-gram indices break text into overlapping sequences (trigrams) for efficient substring matching. They provide fast text search by indexing all 3-character sequences in the text after applying ASCII folding and lowercasing.

Index Details

protobuf
%%% proto.message.NGramIndexDetails %%%

Storage Layout

The N-gram index stores tokenized text as trigrams with their posting lists:

  1. ngram_postings.lance - Trigram tokens and their posting lists

File Schema

ColumnTypeNullableDescription
tokensUInt32trueHashed trigram token
posting_listBinaryfalseCompressed bitmap of row IDs containing the token

Accelerated Queries

The N-gram index provides inexact results for the following query types:

Query TypeDescriptionOperationResult Type
containsSubstring search in textFinds all trigrams in query, intersects posting listsAtMost
regexp_like / regexp_matchRegular-expression matchDerives a necessary trigram condition from the pattern (AND of intersections, OR of unions), then rechecks the true regexAtMost
LIKE (infix)Wildcard match such as %foo%bar%Uses the literal segments of the pattern as a trigram condition, then rechecks the LIKEAtMost

Patterns from which no trigram can be derived - for example a.b, .*, case-insensitive matches, or literal runs shorter than three characters - fall back to rechecking every row. This is always correct, just not accelerated.