What is the difference between a term query and a match query in Elasticsearch?
Learn the difference between term and match queries in Elasticsearch, when to use analyzed vs exact matching, with examples, mistakes and interview answers.
Expected Interview Answer
A term query looks for an exact, un-analyzed token in an inverted index, while a match query first runs the search text through the field's analyzer and then searches for the resulting tokens, which makes match the right choice for full-text search.
Because a term query does not analyze its input, it compares your raw string against the exact tokens stored in the index, so searching a text field for "Hello World" fails when the analyzer already lowercased and split it into "hello" and "world". A match query applies the same analyzer used at index time, tokenizing and normalizing your query so it lines up with the stored tokens, and it builds a boolean query that can score and rank results. In practice, use term (and keyword fields) for exact filters like status codes, IDs, and enums, and use match for human language searched against analyzed text fields.
- term gives predictable exact-match filtering
- match handles real full-text search
- match respects analyzers and normalization
- term is ideal for keyword, numeric and boolean fields
- match produces relevance scores for ranking
AI Mentor Explanation
A term query is like asking the scorer for a player whose name is spelled exactly "V Kohli" on the sheet; if the book stored it lowercased as "v kohli" you get nothing back. A match query is like telling the scorer the name and letting them apply the same shorthand rules the book uses, so your request is normalized the same way the entry was and you find the right batter every time.
Step-by-Step Explanation
Step 1
Know your field type
Check whether the field is text (analyzed) or keyword/numeric/boolean (not analyzed) in the mapping.
Step 2
Pick term for exact matches
Use a term query on keyword-like fields for filters such as status, tags, IDs and enums.
Step 3
Pick match for full text
Use a match query on analyzed text fields so the query text is tokenized the same way as the indexed content.
Step 4
Mind the analyzer
Remember that match applies the search analyzer, so casing and tokenization are normalized before lookup.
Step 5
Use scores when needed
Choose match in a query context when you need relevance ranking; use term in a filter context when you only need yes/no matching.
What Interviewer Expects
- Understanding that term is not analyzed and match is analyzed
- Knowing which field types suit each query
- Awareness of how the analyzer changes stored tokens
- Recognizing term as a filter and match as full-text search
- A clear example of when term silently returns nothing
Common Mistakes
- Running a term query against an analyzed text field and getting no results
- Believing match is just a case-insensitive term query
- Ignoring the analyzer applied at index and search time
- Using match for exact keyword filters where term is better
- Confusing term with the terms (plural) multi-value query
Best Answer (HR Friendly)
“A term query looks for an exact word exactly as it was stored, so it is great for precise filters like status codes or IDs. A match query is smarter for real text search because it cleans and splits your words the same way the data was processed, so everyday searches find the right results.”
Code Example
// Exact filter on a keyword field
GET /articles/_search
{
"query": {
"term": { "status": "published" }
}
}
// Full-text search on an analyzed text field
GET /articles/_search
{
"query": {
"match": { "title": "Hello World" }
}
}Follow-up Questions
- Why might a term query on a text field return zero results?
- What is the difference between query context and filter context?
- How does match_phrase differ from match?
- When would you use a keyword sub-field for a text field?
- How do the term and terms queries differ?
MCQ Practice
1. Which query analyzes its input before searching?
A match query passes the search text through the field's analyzer before looking up tokens; a term query does not.
2. You want an exact filter on a status field storing enums. Which is best?
Exact enum filters belong on keyword fields with a term query, which compares the raw value without analysis.
3. A term query on an analyzed text field for "Hello" returns nothing. Why?
The standard analyzer lowercased the stored token to "hello", so the exact, case-sensitive term "Hello" does not match.
Flash Cards
Is a term query analyzed? — No. It matches the exact token as stored in the inverted index, with no analysis.
Is a match query analyzed? — Yes. It runs the query text through the field's analyzer, then searches the resulting tokens.
Best query for a status enum? — A term query on a keyword field for exact, predictable filtering.
Best query for human language? — A match query on an analyzed text field so tokenization lines up.
Continue Learning
Related Interview Questions
How does Elasticsearch handle full-text search vs exact matching?
medium
What is analysis in Elasticsearch and how do analyzers, tokenizers, and filters work?
medium
What is the difference between keyword and text field types in Elasticsearch?
medium
What is mapping in Elasticsearch and why does it matter?
medium