How does Elasticsearch handle full-text search vs exact matching?
Learn how Elasticsearch handles full-text search with text fields and match versus exact matching with keyword fields and term, with examples and FAQs.
Expected Interview Answer
Elasticsearch handles full-text search by analyzing text into normalized tokens so a match query finds documents by relevance, while exact matching uses non-analyzed keyword fields with a term query that requires the value to match byte-for-byte.
The distinction comes down to field mapping and analysis. A text field is passed through an analyzer that lowercases, tokenizes, and often stems the content, so "Running Shoes" becomes tokens like [running, shoe] and a match query on "run shoe" can find it, ranked by relevance scoring. A keyword field is stored verbatim with no analysis, so a term query must match the exact string including case. Multi-field mappings commonly index the same value both ways: the text version for search and the keyword sub-field for filtering, sorting, and aggregations. Using a term query on an analyzed text field is a classic bug because it compares your raw input against already-tokenized terms.
- text + match: relevance-ranked, tolerant full-text search
- keyword + term: precise, case-sensitive exact matching
- Multi-fields index one value for both search and filtering
- Analyzers enable stemming, lowercasing, and tokenization
- keyword fields power sorting and aggregations via doc_values
AI Mentor Explanation
Full-text search is like an umpire recognizing an appeal whether shouted as "howzat", "how's that", or "owzat" because they all reduce to the same intent. Exact matching is like a scorer requiring a player's registered name spelled precisely to log a run. One tolerates variation and ranks closeness; the other demands a byte-for-byte match.
Step-by-Step Explanation
Step 1
Decide the field type
Map free text you want to search as text, and codes or exact values as keyword.
Step 2
Understand analysis
text fields run through an analyzer that lowercases, tokenizes, and often stems the content.
Step 3
Use match for full text
A match query analyzes your input the same way and finds relevance-ranked documents.
Step 4
Use term for exact values
A term query compares your raw value against the un-analyzed keyword field, case-sensitively.
Step 5
Index both with multi-fields
Map a value as text with a .keyword sub-field so you can search and also filter, sort, and aggregate.
What Interviewer Expects
- text is analyzed; keyword is stored verbatim
- match analyzes input and ranks by relevance; term is exact
- The multi-field pattern for one value indexed two ways
- That term on an analyzed text field is a common bug
Common Mistakes
- Running a term query against an analyzed text field and getting no hits
- Expecting keyword matches to be case-insensitive
- Aggregating or sorting on a text field instead of its keyword sub-field
- Confusing match_phrase with a plain match on token order
Best Answer (HR Friendly)
“Full-text search breaks text into normalized words so it can find relevant matches even with different casing or wording, and it ranks results. Exact matching compares the whole value literally, which is right for codes and IDs. Elasticsearch supports both by mapping a field as text for search or keyword for exact values.”
Code Example
// Full-text: analyzed, relevance-ranked
GET /products/_search
{ "query": { "match": { "title": "running shoe" } } }
// Exact: un-analyzed keyword, case-sensitive
GET /products/_search
{ "query": { "term": { "status.keyword": "Active" } } }Follow-up Questions
- What is an analyzer and what steps does it perform?
- Why does a term query on a text field often return nothing?
- What is a multi-field mapping and why is it useful?
- When would you use match_phrase instead of match?
- How do you make keyword matching case-insensitive?
MCQ Practice
1. Which query type performs analyzed, relevance-ranked full-text search?
match analyzes the input like the field and scores results by relevance; term is for exact, un-analyzed values.
2. A field mapped as keyword is?
keyword fields are indexed exactly as given, enabling precise term matches, sorting, and aggregations.
3. Why might a term query on a text field return no hits?
text is analyzed into lowercased tokens, so a raw, cased term value rarely matches a stored token exactly.
Flash Cards
text field? — Analyzed into normalized tokens for full-text, relevance-ranked search via match.
keyword field? — Stored verbatim, no analysis; used with term for exact, case-sensitive matching.
Multi-field pattern? — Index one value as text and as a .keyword sub-field for both search and filtering/aggregations.
Classic mismatch bug? — Running term on an analyzed text field; your raw value won't match tokenized terms.
Continue Learning
Related Interview Questions
What is the difference between a term query and a match query in Elasticsearch?
medium
What is the difference between keyword and text field types in Elasticsearch?
medium
What is the difference between filter context and query context in Elasticsearch?
medium
What is mapping in Elasticsearch and why does it matter?
medium