The core of ES (Elasticsearch) is full-text search, and the most commonly used query method in practical business is the multi_match syntax. This syntax has many parameters, and if not used properly, it can not only fail to yield desired results but also affect performance. Therefore, this article provides a detailed explanation of the parameters used in multi_match.
Basic Syntax
The multi_match query, as the name implies, allows querying across multiple fields. The basic syntax is as follows:
GET /_search{ "query": { "multi_match" : { "query": "this is a test", "fields": [ "title^2", "*_name" ] } }}
-
The query is the search phrase input by the user.
-
The fields array has a maximum length of 1024 by default and allows setting relevance weights for individual fields; wildcards are supported. If fields is empty, all fields that support term queries configured during the mapping phase will be combined for the query.
Other Parameters
-
The type defines the internal query method and scoring method.
| Value | Description |
|---|---|
| best_fields | Matches and retrieves documents based on the highest score from individual fields. Although this is the default value, it is not recommended for large datasets as it may degrade query performance. |
| most_fields | Matches and combines scores from all fields into a final score. |
| cross_fields | Combines all fields into a single large field for matching. All fields must use the same analyzer. |
| phrase | Matches based on phrase matching, with a default slop of 0, executing an exact phrase match. Thus, even if minimum_should_match is set, it will have no effect; it takes the highest field score. |
| phrase_prefix | Matches based on phrase prefix; the default slop is 0, taking the highest field score. |
| bool_prefix | Matches based on boolean prefix. |
It is important to note that when best_fields, most_fields are used with operator or minimum_should_match, documents will only appear if the user input must fully satisfy any single field. For example:
GET /_search{ "_source": ["title","desc"], "query": { "multi_match": { "query": "高端婚礼邀请函", "fields": ["title","desc"], "operator": "and" } }}
The matching logic is: (title:高端 + title:婚礼 + title:邀请函) || (desc:高端 + desc:婚礼 + desc:邀请函). This can be adjusted as follows to improve hit rates:
{ "_source": ["title","desc"], "query": { "multi_match": { "query": "高端婚礼邀请函", "fields": ["title","desc"], "operator": "and", "type": "cross_fields" } }}
# This method combines all fields into a single large field for matching, significantly improving results in exact matches and increasing the number of matched items.
-
The tie_breaker value ranges from 0-1. When the type uses the default value best_fields, the tie_breaker changes the default score calculation method to best_field_score + tie_breaker * other_field_score.
-
The analyzer determines which analyzer is used for tokenizing user search input. The default is the analyzer specified during the mapping phase. If the analyzer set is inconsistent with the analyzer used during indexing and the operator is and, it may result in an empty result set for phrases that should match exactly.
-
The fuzziness specifies the degree of fuzziness, supporting numbers or auto.
-
The prefix_length specifies the length of the prefix that remains unchanged when using fuzzy queries.
-
The lenient parameter determines whether to ignore documents when a query error occurs; the default is false.
-
The operator defines the logical relationship for matching, with a default of or. When set to and, it indicates that all input terms must match exactly.
-
The minimum_should_match parameter applies only when the operator is or. It supports numbers, percentages, or mixed configurations, such as:
# When the length of the term >3, one fuzziness is allowed; >6 allows two; more than 10 allows half to be fuzzy.
"minimum_should_match": "3<-1 6<-2 10<50%"
-
The zero_terms_query determines whether to return documents when all user input terms are stop words. The default is none, meaning no data is returned. When set to all, the query will be rewritten to match_all.
-
The auto_generate_synonyms_phrase_query determines whether to enable synonym queries, with the default set to true.
# For example, if in analysis/synonym.txt, ‘可爱’ (cute) is set to synonyms ‘粉色’ (pink) and ‘卡通’ (cartoon), the phrase ‘可爱风格’ (cute style) will be rewritten as(可爱 OR (粉色 AND 卡通))风格