AI Glossary / Graphs and symbolic AI / Intermediate
Trigram search
Definition
Trigram search retrieves or compares text using overlapping sequences of three characters. Shared trigrams can help identify similar strings or narrow the candidates for a text pattern. The exact treatment of spaces, punctuation, case and scoring depends on the implementation.
What it means
A character trigram is a short fragment such as “pla” or “lan”. Breaking a word into these fragments allows a search system to compare parts of its spelling rather than requiring the complete word to match exactly.
This can help with misspellings, partial names and approximate lookup. It does not make the system understand meaning: two different words can share fragments, while synonymous words may share few. Character trigram search also differs from a language model that uses sequences of three whole words.
The word search covers more than one use. A system may rank strings by trigram similarity, or use trigram indexes to accelerate an exact pattern query. In the latter case, candidate filtering does not change the final query's matching rules.
How it works
First decide how to prepare text. Case folding, word boundaries and padding can change the extracted trigrams. For example, an implementation may add boundary spaces so that fragments near the beginning and end of a word contribute to matching.
An index associates fragments with records containing them. At query time, the system extracts relevant fragments, finds candidate records and applies the intended comparison or pattern test. Similarity search then ranks or filters candidates using a specified score and threshold.
There is no single universal trigram score. A teaching example can use set overlap divided by set union, while production functions may distinguish complete strings, word extents and word boundaries. Short queries or patterns with no usable trigrams can provide little filtering benefit. Evaluate the actual query type and implementation rather than assuming every three-character index behaves identically.
Three practical examples
- A product catalog suggests likely item names when a customer makes a small spelling error.
- A record-cleaning tool proposes similarly spelled organization names for a person to review before any merge.
- A database uses a trigram index to narrow candidates for a supported substring or regular-expression search, then checks the requested pattern.
Think of it this way
Think of comparing words by a collection of overlapping three-letter window views. Words with many matching views often have related spellings even when their full text differs.
The analogy does not capture meaning or identity. Two files can have similar names and unrelated contents, just as two customer records can share spelling fragments without referring to the same person. A similarity result is evidence for a comparison, not proof of equivalence.
Worked example
Calculating one explicitly defined score
Use unpadded character trigrams and treat them as sets for this invented example. “planet” gives {pla, lan, ane, net}. “planer” gives {pla, lan, ane, ner}.
The shared set contains three fragments: pla, lan and ane. The union contains five: pla, lan, ane, net and ner. Under the chosen Jaccard score, similarity is 3/5=0.6.
Suppose a teaching search accepts scores of at least 0.5. This pair qualifies. Raising that inclusive threshold to 0.7 rejects it. Neither result says that a planet and a planer have the same meaning.
These numbers are not asserted as PostgreSQL output. Its pg_trgm extraction includes word-boundary padding and other documented conventions, which change the sets. Stating the extraction and scoring rules makes the arithmetic reproducible and prevents accidental comparison of incompatible scores.
Illustrative case study
Fictional Willow Catalogue introduces spelling suggestions for equipment names. An early demonstration shows plausible results, but staff notice that short product codes produce weak or confusing matches and that similarly named products can have different specifications.
The team keeps exact identifiers separate, labels approximate results as suggestions and evaluates longer names using examples of real search intent. It records missed desired matches as well as irrelevant suggestions before selecting a threshold.
For duplicate review, it requires additional evidence before combining records. The fictional case illustrates how a useful candidate generator needs context and evaluation; it makes no claim of an observed accuracy increase or reduced operating cost.
Common misconceptions and mistakes
- Trigram similarity measures semantic similarity. It primarily measures shared text fragments under the chosen rules.
- A score has the same interpretation in every system. Padding, normalization, set handling and scoring functions can differ.
- A trigram index guarantees fast queries. Selectivity, text length, query form, index type and data distribution affect performance.
Business use
Trigram search is useful when users know roughly how something is spelled but exact matching is too brittle. It can also complement full-text search by suggesting corrections or supporting certain substring queries.
Evaluate success using representative misspellings, names, languages and short inputs. Measure the relevance of suggestions, missed intended results, latency and index maintenance cost. Keep record merging and other consequential actions separate from the similarity score. If meaning rather than spelling is the objective, compare or combine the method with retrieval approaches designed for that purpose.
People also ask
Does trigram mean three words?
In this entry it means three characters. Word-trigram language modeling is a separate use of the broader n-gram idea.
Can it handle spelling errors?
Often it can provide useful candidates because an error may leave other fragments unchanged, but success is not guaranteed.
Are spaces always ignored?
No. Extraction rules vary; some systems use boundary padding and special treatment of punctuation.
Check your understanding
Two strings receive a high character-trigram similarity score. Can a records system conclude that they identify the same organization?
Answer: No. Similar spelling supports candidate review, but identity needs additional evidence such as verified identifiers or other matching attributes.
Related terms
- N-gram
- Edit distance
- Inverted index
- string similarity
- Jaccard similarity
- approximate matching
- full-text search
- entity resolution
Learn next
Read edit distance and full-text search. Extract unpadded trigrams from two short words by hand, then repeat with explicit boundary padding and compare how the scores change.
More in Graphs and symbolic AI
- Tree decomposition - A tree decomposition represents a graph as overlapping groups of vertices, called bags, connected in a tree.
- Tree-like search - Tree-like search explores a tree of possible action sequences without maintaining a global record of every state already reached.
- Tree width - Tree width, commonly written treewidth, measures how small the largest bag can be in a valid tree decomposition of a graph, minus one.
- Triplestore - A triplestore is a database designed to store and query statements represented as subject - predicate - object triples, most commonly using RDF.
- Truth - In logic-based AI, truth concerns whether a statement holds under a specified interpretation or model.
- Truth-revealing - Truth-revealing describes a mechanism whose incentives make accurately reporting private information an appropriate strategy under specified assumptions.
Sources
PostgreSQL project, pg_trgm documentation, explaining character trigrams, extraction conventions, similarity functions and supported index searches: https://www.postgresql.org/docs/current/pgtrgm.html
The relevant official documentation was read. The planet/planer calculation deliberately uses a separately specified teaching convention and is not presented as database execution. Product behaviour and performance should be checked against the deployed version and workload.
Previous: Trigram Next: Trimmed samples