0
0
ElasticsearchComparisonBeginner · 4 min read

Match vs Term Query in Elasticsearch: Key Differences and Usage

In Elasticsearch, a 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.

AspectMatch QueryTerm Query
Type of SearchFull-text search with analysisExact value search without analysis
Input ProcessingAnalyzes input text (tokenizes, lowercases)Uses input as-is, no analysis
Best ForText fields with natural languageKeyword or structured fields
Case SensitivityCase insensitive due to analysisCase sensitive, exact match required
PerformanceSlower due to analysisFaster, simple exact match
Use Case ExampleSearching documents by contentFiltering 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.

json
{
  "query": {
    "match": {
      "description": "Apple"
    }
  }
}
Output
Returns documents where the description contains terms like "apple", "Apple", or related forms after analysis.
↔️

Term Query Equivalent

This example shows a term query searching for the exact term "Apple" in the description.keyword field, which is not analyzed.

json
{
  "query": {
    "term": {
      "description.keyword": {
        "value": "Apple"
      }
    }
  }
}
Output
Returns documents where the description.keyword field exactly matches "Apple" (case sensitive).
🎯

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

Use match for full-text, analyzed searches on text fields.
Use 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.
Pick the query type based on whether you need flexible text search or exact filtering.