| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238 | [[query-dsl-match-query]]=== Match QueryA family of `match` queries that accepts text/numerics/dates, analyzesthem, and constructs a query. For example:[source,js]--------------------------------------------------{    "match" : {        "message" : "this is a test"    }}--------------------------------------------------Note, `message` is the name of a field, you can substitute the name ofany field (including `_all`) instead.There are three types of `match` query: `boolean`, `phrase`, and `phrase_prefix`:[[query-dsl-match-query-boolean]]==== booleanThe default `match` query is of type `boolean`. It means that the textprovided is analyzed and the analysis process constructs a boolean queryfrom the provided text. The `operator` flag can be set to `or` or `and`to control the boolean clauses (defaults to `or`). The minimum number ofoptional `should` clauses to match can be set using the<<query-dsl-minimum-should-match,`minimum_should_match`>>parameter.The `analyzer` can be set to control which analyzer will perform theanalysis process on the text. It defaults to the field explicit mappingdefinition, or the default search analyzer.The `lenient` parameter can be set to `true` to ignore exceptions caused bydata-type mismatches,  such as trying to query a numeric field with a textquery string. Defaults to `false`.[[query-dsl-match-query-fuzziness]]===== Fuzziness`fuzziness` allows _fuzzy matching_ based on the type of field being queried.See <<fuzziness>> for allowed settings.The `prefix_length` and`max_expansions` can be set in this case to control the fuzzy process.If the fuzzy option is set the query will use `top_terms_blended_freqs_${max_expansions}`as its <<query-dsl-multi-term-rewrite,rewritemethod>> the `fuzzy_rewrite` parameter allows to control how the query will getrewritten.Here is an example when providing additional parameters (note the slightchange in structure, `message` is the field name):[source,js]--------------------------------------------------{    "match" : {        "message" : {            "query" : "this is a test",            "operator" : "and"        }    }}--------------------------------------------------[[query-dsl-match-query-zero]]===== Zero terms queryIf the analyzer used removes all tokens in a query like a `stop` filterdoes, the default behavior is to match no documents at all. In order tochange that the `zero_terms_query` option can be used, which accepts`none` (default) and `all` which corresponds to a `match_all` query.[source,js]--------------------------------------------------{    "match" : {        "message" : {            "query" : "to be or not to be",            "operator" : "and",            "zero_terms_query": "all"        }    }}--------------------------------------------------[[query-dsl-match-query-cutoff]]===== Cutoff frequencyThe match query supports a `cutoff_frequency` that allowsspecifying an absolute or relative document frequency where highfrequency terms are moved into an optional subquery and are only scoredif one of the low frequency (below the cutoff) terms in the case of an`or` operator or all of the low frequency terms in the case of an `and`operator match.This query allows handling `stopwords` dynamically at runtime, is domainindependent and doesn't require a stopword file. It prevents scoring /iterating high frequency terms and only takes the terms into account if amore significant / lower frequency term matches a document. Yet, if allof the query terms are above the given `cutoff_frequency` the query isautomatically transformed into a pure conjunction (`and`) query toensure fast execution.The `cutoff_frequency` can either be relative to the total number ofdocuments if in the range `[0..1)` or absolute if greater or equal to`1.0`.Here is an example showing a query composed of stopwords exclusively:[source,js]--------------------------------------------------{    "match" : {        "message" : {            "query" : "to be or not to be",            "cutoff_frequency" : 0.001        }    }}--------------------------------------------------IMPORTANT: The `cutoff_frequency` option operates on a per-shard-level. This meansthat when trying it out on test indexes with low document numbers youshould follow the advice in {defguide}/relevance-is-broken.html[Relevance is broken].[[query-dsl-match-query-phrase]]==== phraseThe `match_phrase` query analyzes the text and creates a `phrase` queryout of the analyzed text. For example:[source,js]--------------------------------------------------{    "match_phrase" : {        "message" : "this is a test"    }}--------------------------------------------------Since `match_phrase` is only a `type` of a `match` query, it can also beused in the following manner:[source,js]--------------------------------------------------{    "match" : {        "message" : {            "query" : "this is a test",            "type" : "phrase"        }    }}--------------------------------------------------A phrase query matches terms up to a configurable `slop`(which defaults to 0) in any order. Transposed terms have a slop of 2.The `analyzer` can be set to control which analyzer will perform theanalysis process on the text. It defaults to the field explicit mappingdefinition, or the default search analyzer, for example:[source,js]--------------------------------------------------{    "match_phrase" : {        "message" : {            "query" : "this is a test",            "analyzer" : "my_analyzer"        }    }}--------------------------------------------------[[query-dsl-match-query-phrase-prefix]]==== match_phrase_prefixThe `match_phrase_prefix` is the same as `match_phrase`, except that itallows for prefix matches on the last term in the text. For example:[source,js]--------------------------------------------------{    "match_phrase_prefix" : {        "message" : "this is a test"    }}--------------------------------------------------Or:[source,js]--------------------------------------------------{    "match" : {        "message" : {            "query" : "this is a test",            "type" : "phrase_prefix"        }    }}--------------------------------------------------It accepts the same parameters as the phrase type. In addition, it alsoaccepts a `max_expansions` parameter that can control to how manyprefixes the last term will be expanded. It is highly recommended to setit to an acceptable value to control the execution time of the query.For example:[source,js]--------------------------------------------------{    "match_phrase_prefix" : {        "message" : {            "query" : "this is a test",            "max_expansions" : 10        }    }}--------------------------------------------------.Comparison to query_string / field**************************************************The match family of queries does not go through a "query parsing"process. It does not support field name prefixes, wildcard characters,or other "advanced" features. For this reason, chances of it failing arevery small / non existent, and it provides an excellent behavior when itcomes to just analyze and run that text as a query behavior (which isusually what a text search box does). Also, the `phrase_prefix` type canprovide a great "as you type" behavior to automatically load searchresults.**************************************************
 |