Swiftorial Logo
Home
Swift Lessons
Matchups
CodeSnaps
Tutorials
Career
Resources

Match Query in Elasticsearch

Introduction

The Match Query is one of the most commonly used queries in Elasticsearch. It is used to search for documents that match a specified text, number, date, or boolean value. The Match Query analyzes the text before performing the search, making it suitable for full-text search.

Basic Usage

The simplest form of the Match Query searches for a specified term in a given field. Below is an example of a basic Match Query:

{ "query": { "match": { "field_name": "search_term" } } }

In this query, field_name is the field you want to search, and search_term is the term you are looking for.

Example

Consider an index of articles. To find articles containing the word "Elasticsearch" in the title, you can use the following Match Query:

{ "query": { "match": { "title": "Elasticsearch" } } }

This query searches the title field for the term "Elasticsearch".

Operator Parameter

The Match Query supports an operator parameter to control how multiple terms are matched. The default operator is OR, but it can be changed to AND to require all terms to be present in the field.

{ "query": { "match": { "title": { "query": "full text search", "operator": "and" } } } }

In this example, both "full", "text", and "search" must be present in the title field for a document to be considered a match.

Minimum Should Match

The minimum_should_match parameter specifies the minimum number of terms that need to match for a document to be considered a match.

{ "query": { "match": { "title": { "query": "Elasticsearch tutorial", "minimum_should_match": 2 } } } }

In this example, at least two of the terms ("Elasticsearch", "tutorial") must be present in the title field.

Fuzziness

The fuzziness parameter allows for matches with a certain degree of fuzziness, which is useful for handling typos or misspellings.

{ "query": { "match": { "title": { "query": "Elasticsearch", "fuzziness": "AUTO" } } } }

In this example, the query will match terms that are close in spelling to "Elasticsearch". The AUTO setting adjusts the fuzziness based on the length of the term.

Phrase Matching

The Match Query can also handle phrase matching by setting the type parameter to phrase. This ensures that the terms appear in the specified order.

{ "query": { "match": { "title": { "query": "full text search", "type": "phrase" } } } }

In this example, the query will only match documents where the title field contains the exact phrase "full text search".

Boosting

The boost parameter allows you to increase the relevance score of a query. This can be useful when you want to prioritize certain matches over others.

{ "query": { "match": { "title": { "query": "Elasticsearch", "boost": 2.0 } } } }

In this example, matches in the title field for the term "Elasticsearch" will have their relevance score multiplied by 2.0.

Conclusion

The Match Query is a versatile and powerful tool for searching text, numbers, dates, and boolean values in Elasticsearch. By understanding its various parameters and options, you can fine-tune your searches to get the most relevant results. Whether you're performing basic term searches or complex phrase matching with boosting and fuzziness, the Match Query provides a robust solution for full-text search in Elasticsearch.