Search and Elasticsearch · core
The inverted index, analyzers and mappings
Elasticsearch finds documents by looking up terms in an inverted index. Analyzers decide the terms, and the mapping decides whether a field is searched as text or matched exactly as a keyword.
Explain it out loud
Explain it out loud to an AI interviewer that follows up, and see where your answer is thin.
Practise by voiceThe notes
What you must know
- Inverted index: for each field, a sorted dictionary of terms, each pointing to the documents (with positions and frequencies) that contain it. A search looks up a few terms and does not scan documents.
- An analyzer runs at index time and again at search time: character filters, a tokenizer, then token filters (lowercase, stemming, synonyms). The standard analyzer turns iPhone 15 128GB into iphone, 15, 128gb.
- text fields are analysed for full-text search. keyword fields keep the exact value for filters, sorting and aggregations (stored as doc values, a column store).
- match and multi_match analyse the query text. term does not analyse, so a term query for iPhone 15 looks for one exact term that does not exist and finds nothing.
- A keyword field is exact and case-sensitive. To ignore case, add a normalizer with a lowercase filter (needs a reindex).
- Map a SKU as keyword. As text it is split into pieces and match finds every product containing one of them.
- Money: do not use float. Use scaled_float with a scaling factor or a long in paise.
- Near real time: new documents go to a memory buffer and the translog, and a refresh (every 1 second by default) writes a searchable segment. A new document is not searchable until then.
The red pen
Common traps: mistake → fix
Mistake: term query is the exact version of match so it is safer
Fix: term does not analyse, so on a text field it usually finds nothing.
Mistake: A document is searchable the moment I index it
Fix: It becomes searchable after the next refresh.
Mistake: I can change a field type later
Fix: A mapping cannot change for existing data. You create a new index and reindex.
Easy to hard
Questions interviewers ask, easy to hard
- What is an inverted index?
- What is the difference between text and keyword fields?
- Why does a term query for iPhone 15 return nothing?
- Why is the SKU field a keyword and not text?
- A search for iphone15 should find iPhone 15. How do you change the analysis?
Explain it out loud
Explain it out loud to an AI interviewer that follows up, and see where your answer is thin.
Practise by voiceYour first interview is free
Sign up and start with a 15-minute interview with the full report, and an English conversation. No card needed.