Elasticsearch match query vs term query
Elasticsearch is a popular open-source search and analytics engine many organizations use to search, analyze, and visualize data. It is known for its scalability, speed, and flexibility. Elasticsearch provides several types of queries to search data, including match query and term query. This article will discuss the differences between Elasticsearch match query and term query.
Match Query:
A match query is used to search for a full-text search term in a specified field. It is used to search for a phrase, word, or a sentence in a field. The match query analyzes the text provided to it and creates a set of terms which it then searches against the inverted index. The inverted index is a data structure used by Elasticsearch to improve the search speed. When a user searches for a term, the search engine looks for the term in the inverted index instead of searching through the entire document.
For example, suppose we have an index of products that contain the following fields: product_id, name, description, and price. We want to search for products that contain the word “Apple” in their name field. We can use the match query as follows:
GET /products/_search
{
"query": {
"match": {
"name": "Apple"
}
}
}
This will return all products that contain the word “Apple” in their name field.
Term Query:
A term query is used to search for an exact match of a term in a specified field. Unlike the match query, it does not analyze the text provided to it. It searches for the exact term in the inverted index. The term query is useful for searching for fields that contain values that are not analyzed or are not analyzed in a specific way.
For example, suppose we have an index of products that contain the following fields: product_id, name, description, and price. We want to search for products that have a product_id of “1001”. We can use the term query as follows:
GET /products/_search
{
"query": {
"term": {
"product_id": "1001"
}
}
}
This will return all products that have a product_id of “1001”.
Differences between Match Query and Term Query:
- Analyzing the text: The match query analyzes the text provided to it and creates a set of terms which it then searches against the inverted index. Conversely, the term query does not analyze the text and searches for the exact term in the inverted index.
- Matching documents: The match query returns documents containing the searched term, while the term query returns documents that match the searched term.
- Field type: The match query is used for full-text search, while the term query is used for exact search on fields that are not analyzed or analyzed in a specific way.
- Performance: The term query is generally faster than the match query because it searches for exact terms in the inverted index. In contrast, the match query searches for terms created by analyzing the text.
Conclusion:
Elasticsearch provides several types of queries to search data, including match query and term query. The match query is used for full-text search while the term query is used for exact search on fields that are not analyzed or are not analyzed in a specific way. The match query analyzes the text provided to it and creates a set of terms which it then searches against the inverted index. The term query, on the other hand, does not analyze the text and searches for the exact term in the inverted index. The choice of the query depends on the use case and the type of search required.