Lesson 1 of 9 · 26m
Inverted Indexes, Tokens, and Analysis Chains
Build the mental model of terms, postings, analyzers, and indexing-time transformations that make search fast and sometimes surprisingly wrong.
The first time a production search for C++ returned thousands of documents about the letter C, the database was not broken. Analysis was doing exactly what it had been configured to do: strip punctuation, lowercase text, and index tokens. Search engines are not magic document databases. They are large, carefully engineered term indexes, and every relevance mystery eventually comes back to what tokens were written.
The inverted index is the core abstraction
A relational index usually maps a key to rows. An inverted index maps terms to document positions:
term postings
----------- -------------------------------
error doc 1 pos 4, doc 8 pos 2
timeout doc 1 pos 9, doc 3 pos 15
connection doc 3 pos 2, doc 8 pos 7A query for connection timeout becomes postings-list work: find matching terms, intersect or score documents, and return the best candidates. That is why search engines scale well for text retrieval and poorly for arbitrary relational joins.
Analysis happens before the index
Analysis is usually a pipeline of a character filter, tokenizer, and token filters. In Elasticsearch or OpenSearch, inspect it instead of guessing:
curl -s 'http://localhost:9200/_analyze?pretty' -H 'content-type: application/json' -d '{
"analyzer": "standard",
"text": "C++ search timeout in api-gateway"
}'The standard analyzer produces tokens such as c, search, timeout, in, api, and gateway. If your users expect exact product names, punctuation, part numbers, or case-sensitive identifiers to matter, that default can quietly erase meaning.
Index-time and search-time analysis must agree
A common production bug is indexing with one analyzer and searching with another. The text indexed as api gateway might be queried as api-gateway, producing different token streams. Always test both sides:
{
"mappings": {
"properties": {
"message": {
"type": "text",
"analyzer": "standard",
"search_analyzer": "standard"
}
}
}
}Custom search analyzers are useful, but asymmetry is not free. Synonym expansion at search time can be safer than rebuilding indexes for every synonym change; indexing-time synonyms can be faster but stale.
Keywords are not failed text fields
Use keyword when the value is an atomic label: status, hostname, environment, exact product SKU, or tenant ID. Use text when users need linguistic full-text search. Mature mappings often use both:
{
"service": {
"type": "text",
"fields": {
"raw": {"type": "keyword", "ignore_above": 256}
}
}
}That pattern supports matching api gateway as text while still aggregating or filtering exactly on service.raw.
Field notes
_analyzeis the first debugging tool for strange matches; use it before changing scoring or reindexing data.- Stopword removal, stemming, lowercase conversion, and punctuation stripping all change recall and precision.
- Very long tokens can waste memory or be rejected; use
ignore_abovedeliberately for keyword fields. - Reanalysis generally requires reindexing existing documents. Changing an analyzer does not rewrite old indexed terms.
Was this lesson helpful?