Full-Text Queries: Match Query
Learn about the match query and the logic behind it.
Match query
The match query in Elasticsearch is a full-text search query that is used to find and retrieve documents based on a specific term or phrase within a field. It is one of the most commonly used queries in Elasticsearch, and it can be used for a wide range of use cases, as shown below.
- Simple search: The match query can be used to perform a simple text search across multiple fields in a document. For example, we might use it to search for all documents that contain the word “hiking” in the
description
field or to find all documents that contain the phrase “red wine” in thetitle
field.
- Cross-field search: The match query can be used to search for a term or phrase across multiple fields and to give more weight to certain fields. For example, we might use it to search for documents that contain the phrase “red wine” in the
title
field, but also give a higher weight to documents that contain the phrase in thedescription
field.
- Multilingual search: The match query supports multilingual search, which allows us to search for terms in multiple languages within the same index. This can be useful for applications that need to support multiple languages, such as e-commerce websites or news portals.
How does a match query work?
The match query in Elasticsearch performs a full-text search by employing an analyzer to analyze the search text. This analysis generates a list of tokens representing the search text’s individual terms. These tokens are then matched against the data stored in the inverted index of the searched field. To illustrate, let’s consider a user searching for the phrase “What is a match query in Elasticsearch?”. The match query takes this query string and submits it to the specified analyzer. As a result, the analyzer produces tokens, such as “what”, “match,” “query,” and “elasticsearch.” These tokens are subsequently compared to the data stored in the inverted index, enabling Elasticsearch to identify relevant matches based on the search criteria.
Note: The match query is commonly used with the
text
field because it is analyzed by a defined analyzer before being stored in ...