2013-08-28 19:24:34 -04:00
|
|
|
[[query-dsl-match-query]]
|
2015-06-03 19:59:22 -04:00
|
|
|
=== Match Query
|
2013-08-28 19:24:34 -04:00
|
|
|
|
2015-07-05 12:24:06 -04:00
|
|
|
|
|
|
|
A family of `match` queries that accepts text/numerics/dates, analyzes
|
|
|
|
them, and constructs a query. For example:
|
2013-08-28 19:24:34 -04:00
|
|
|
|
|
|
|
[source,js]
|
|
|
|
--------------------------------------------------
|
|
|
|
{
|
|
|
|
"match" : {
|
|
|
|
"message" : "this is a test"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
--------------------------------------------------
|
|
|
|
|
|
|
|
Note, `message` is the name of a field, you can substitute the name of
|
|
|
|
any field (including `_all`) instead.
|
|
|
|
|
2015-06-03 19:59:22 -04:00
|
|
|
There are three types of `match` query: `boolean`, `phrase`, and `phrase_prefix`:
|
2013-08-28 19:24:34 -04:00
|
|
|
|
2015-05-24 11:57:34 -04:00
|
|
|
[[query-dsl-match-query-boolean]]
|
|
|
|
==== boolean
|
2013-08-28 19:24:34 -04:00
|
|
|
|
|
|
|
The default `match` query is of type `boolean`. It means that the text
|
|
|
|
provided is analyzed and the analysis process constructs a boolean query
|
|
|
|
from the provided text. The `operator` flag can be set to `or` or `and`
|
|
|
|
to control the boolean clauses (defaults to `or`). The minimum number of
|
2014-05-14 05:58:46 -04:00
|
|
|
optional `should` clauses to match can be set using the
|
2013-08-28 19:24:34 -04:00
|
|
|
<<query-dsl-minimum-should-match,`minimum_should_match`>>
|
|
|
|
parameter.
|
|
|
|
|
|
|
|
The `analyzer` can be set to control which analyzer will perform the
|
2014-05-14 05:58:46 -04:00
|
|
|
analysis process on the text. It defaults to the field explicit mapping
|
2013-08-28 19:24:34 -04:00
|
|
|
definition, or the default search analyzer.
|
|
|
|
|
2015-05-24 11:57:34 -04:00
|
|
|
The `lenient` parameter can be set to `true` to ignore exceptions caused by
|
|
|
|
data-type mismatches, such as trying to query a numeric field with a text
|
|
|
|
query string. Defaults to `false`.
|
|
|
|
|
|
|
|
[[query-dsl-match-query-fuzziness]]
|
2015-06-04 07:16:32 -04:00
|
|
|
===== Fuzziness
|
2015-05-24 11:57:34 -04:00
|
|
|
|
2014-01-02 10:45:24 -05:00
|
|
|
`fuzziness` allows _fuzzy matching_ based on the type of field being queried.
|
|
|
|
See <<fuzziness>> for allowed settings.
|
|
|
|
|
|
|
|
The `prefix_length` and
|
2013-08-28 19:24:34 -04:00
|
|
|
`max_expansions` can be set in this case to control the fuzzy process.
|
2015-07-08 10:20:58 -04:00
|
|
|
If the fuzzy option is set the query will use `top_terms_blended_freqs_${max_expansions}`
|
2013-08-28 19:24:34 -04:00
|
|
|
as its <<query-dsl-multi-term-rewrite,rewrite
|
2014-04-23 15:05:14 -04:00
|
|
|
method>> the `fuzzy_rewrite` parameter allows to control how the query will get
|
2013-08-28 19:24:34 -04:00
|
|
|
rewritten.
|
|
|
|
|
|
|
|
Here is an example when providing additional parameters (note the slight
|
|
|
|
change in structure, `message` is the field name):
|
|
|
|
|
|
|
|
[source,js]
|
|
|
|
--------------------------------------------------
|
|
|
|
{
|
|
|
|
"match" : {
|
|
|
|
"message" : {
|
|
|
|
"query" : "this is a test",
|
|
|
|
"operator" : "and"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
--------------------------------------------------
|
|
|
|
|
2015-05-24 11:57:34 -04:00
|
|
|
[[query-dsl-match-query-zero]]
|
|
|
|
===== Zero terms query
|
2013-08-28 19:24:34 -04:00
|
|
|
If the analyzer used removes all tokens in a query like a `stop` filter
|
|
|
|
does, the default behavior is to match no documents at all. In order to
|
|
|
|
change 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"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
--------------------------------------------------
|
|
|
|
|
2015-05-24 11:57:34 -04:00
|
|
|
[[query-dsl-match-query-cutoff]]
|
|
|
|
===== Cutoff frequency
|
|
|
|
|
2013-09-03 15:27:49 -04:00
|
|
|
The match query supports a `cutoff_frequency` that allows
|
2013-08-28 19:24:34 -04:00
|
|
|
specifying an absolute or relative document frequency where high
|
2014-12-15 13:36:32 -05:00
|
|
|
frequency terms are moved into an optional subquery and are only scored
|
|
|
|
if 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`
|
2013-08-28 19:24:34 -04:00
|
|
|
operator match.
|
|
|
|
|
|
|
|
This query allows handling `stopwords` dynamically at runtime, is domain
|
2015-04-23 08:09:03 -04:00
|
|
|
independent and doesn't require a stopword file. It prevents scoring /
|
2014-12-15 13:36:32 -05:00
|
|
|
iterating high frequency terms and only takes the terms into account if a
|
2015-04-23 08:09:03 -04:00
|
|
|
more significant / lower frequency term matches a document. Yet, if all
|
|
|
|
of the query terms are above the given `cutoff_frequency` the query is
|
2013-08-28 19:24:34 -04:00
|
|
|
automatically transformed into a pure conjunction (`and`) query to
|
|
|
|
ensure fast execution.
|
|
|
|
|
2015-04-07 04:12:39 -04:00
|
|
|
The `cutoff_frequency` can either be relative to the total number of
|
|
|
|
documents if in the range `[0..1)` or absolute if greater or equal to
|
2013-08-28 19:24:34 -04:00
|
|
|
`1.0`.
|
|
|
|
|
2015-04-23 08:09:03 -04:00
|
|
|
Here is an example showing a query composed of stopwords exclusively:
|
2013-08-28 19:24:34 -04:00
|
|
|
|
|
|
|
[source,js]
|
|
|
|
--------------------------------------------------
|
|
|
|
{
|
|
|
|
"match" : {
|
|
|
|
"message" : {
|
|
|
|
"query" : "to be or not to be",
|
|
|
|
"cutoff_frequency" : 0.001
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
--------------------------------------------------
|
|
|
|
|
2015-04-07 04:12:39 -04:00
|
|
|
|
|
|
|
IMPORTANT: The `cutoff_frequency` option operates on a per-shard-level. This means
|
|
|
|
that when trying it out on test indexes with low document numbers you
|
|
|
|
should follow the advice in {defguide}/relevance-is-broken.html[Relevance is broken].
|
|
|
|
|
2015-05-24 11:57:34 -04:00
|
|
|
[[query-dsl-match-query-phrase]]
|
|
|
|
==== phrase
|
2013-08-28 19:24:34 -04:00
|
|
|
|
|
|
|
The `match_phrase` query analyzes the text and creates a `phrase` query
|
|
|
|
out 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 be
|
|
|
|
used in the following manner:
|
|
|
|
|
|
|
|
[source,js]
|
|
|
|
--------------------------------------------------
|
|
|
|
{
|
|
|
|
"match" : {
|
|
|
|
"message" : {
|
|
|
|
"query" : "this is a test",
|
|
|
|
"type" : "phrase"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
--------------------------------------------------
|
|
|
|
|
2014-01-19 15:29:08 -05:00
|
|
|
A phrase query matches terms up to a configurable `slop`
|
|
|
|
(which defaults to 0) in any order. Transposed terms have a slop of 2.
|
2013-08-28 19:24:34 -04:00
|
|
|
|
|
|
|
The `analyzer` can be set to control which analyzer will perform the
|
|
|
|
analysis process on the text. It default to the field explicit mapping
|
|
|
|
definition, or the default search analyzer, for example:
|
|
|
|
|
|
|
|
[source,js]
|
|
|
|
--------------------------------------------------
|
|
|
|
{
|
|
|
|
"match_phrase" : {
|
|
|
|
"message" : {
|
|
|
|
"query" : "this is a test",
|
|
|
|
"analyzer" : "my_analyzer"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
--------------------------------------------------
|
|
|
|
|
2015-05-24 11:57:34 -04:00
|
|
|
[[query-dsl-match-query-phrase-prefix]]
|
2015-06-03 19:59:22 -04:00
|
|
|
==== match_phrase_prefix
|
2013-08-28 19:24:34 -04:00
|
|
|
|
|
|
|
The `match_phrase_prefix` is the same as `match_phrase`, except that it
|
|
|
|
allows 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 also
|
|
|
|
accepts a `max_expansions` parameter that can control to how many
|
|
|
|
prefixes the last term will be expanded. It is highly recommended to set
|
|
|
|
it 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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
--------------------------------------------------
|
|
|
|
|
2015-05-24 11:57:34 -04:00
|
|
|
.Comparison to query_string / field
|
|
|
|
**************************************************
|
2013-08-28 19:24:34 -04:00
|
|
|
|
|
|
|
The match family of queries does not go through a "query parsing"
|
|
|
|
process. It does not support field name prefixes, wildcard characters,
|
2014-08-13 17:05:09 -04:00
|
|
|
or other "advanced" features. For this reason, chances of it failing are
|
2013-08-28 19:24:34 -04:00
|
|
|
very small / non existent, and it provides an excellent behavior when it
|
|
|
|
comes to just analyze and run that text as a query behavior (which is
|
|
|
|
usually what a text search box does). Also, the `phrase_prefix` type can
|
|
|
|
provide a great "as you type" behavior to automatically load search
|
|
|
|
results.
|
2013-10-16 04:53:25 -04:00
|
|
|
|
2015-05-24 11:57:34 -04:00
|
|
|
**************************************************
|