Match vs Term Query in Elasticsearch: Key Differences and Usage
match query analyzes the input text and performs a full-text search, making it suitable for natural language queries. A term query, however, searches for exact values without analysis, ideal for structured or keyword fields.Quick Comparison
This table summarizes the main differences between match and term queries in Elasticsearch.
| Aspect | Match Query | Term Query |
|---|---|---|
| Type of Search | Full-text search with analysis | Exact value search without analysis |
| Input Processing | Analyzes input text (tokenizes, lowercases) | Uses input as-is, no analysis |
| Best For | Text fields with natural language | Keyword or structured fields |
| Case Sensitivity | Case insensitive due to analysis | Case sensitive, exact match required |
| Performance | Slower due to analysis | Faster, simple exact match |
| Use Case Example | Searching documents by content | Filtering by exact ID or tag |
Key Differences
The match query is designed for full-text search. It processes the input text through the same analyzer used at index time, breaking it into tokens and normalizing it. This means it can find documents even if the exact input phrase is not present, supporting partial matches and relevance scoring.
In contrast, the term query does not analyze the input. It looks for exact matches of the provided term in the inverted index. This makes it perfect for keyword fields like IDs, tags, or status codes where exact matching is required.
Because match queries analyze input, they are case insensitive and flexible with word forms, while term queries are case sensitive and strict. This difference affects both the results and performance, with term queries generally being faster.
Code Comparison
Here is an example of a match query searching for documents containing the word "Apple" in the description field.
{
"query": {
"match": {
"description": "Apple"
}
}
}Term Query Equivalent
This example shows a term query searching for the exact term "Apple" in the description.keyword field, which is not analyzed.
{
"query": {
"term": {
"description.keyword": {
"value": "Apple"
}
}
}
}When to Use Which
Choose match queries when you want to search text fields with natural language input, allowing flexible and relevant results. Use term queries when you need exact matches on keyword or structured fields like IDs, tags, or codes, where no text analysis is desired.
For example, use match to find articles mentioning "Elasticsearch" in their content, and term to filter users by exact user ID.
Key Takeaways
match for full-text, analyzed searches on text fields.term for exact, unanalyzed matches on keyword or structured fields.match queries are case insensitive; term queries are case sensitive.term queries are faster but less flexible than match queries.