- Elasticsearch Guide: other versions:
- Getting Started
- Setup
- Breaking changes
- Breaking changes in 2.0
- Removed features
- Network changes
- Multiple
path.data
striping - Mapping changes
- CRUD and routing changes
- Query DSL changes
- Search changes
- Aggregation changes
- Parent/Child changes
- Scripting changes
- Index API changes
- Snapshot and Restore changes
- Plugin and packaging changes
- Setting changes
- Stats, info, and
cat
changes - Java API changes
- Breaking changes in 2.0
- API Conventions
- Document APIs
- Search APIs
- Aggregations
- Metrics Aggregations
- Bucket Aggregations
- Children Aggregation
- Date Histogram Aggregation
- Date Range Aggregation
- Filter Aggregation
- Filters Aggregation
- Geo Distance Aggregation
- GeoHash grid Aggregation
- Global Aggregation
- Histogram Aggregation
- IPv4 Range Aggregation
- Missing Aggregation
- Nested Aggregation
- Range Aggregation
- Reverse nested Aggregation
- Sampler Aggregation
- Significant Terms Aggregation
- Terms Aggregation
- Pipeline Aggregations
- Caching heavy aggregations
- Returning only aggregation results
- Aggregation Metadata
- Indices APIs
- Create Index
- Delete Index
- Get Index
- Indices Exists
- Open / Close Index API
- Put Mapping
- Get Mapping
- Get Field Mapping
- Types Exists
- Index Aliases
- Update Indices Settings
- Get Settings
- Analyze
- Index Templates
- Warmers
- Shadow replica indices
- Indices Stats
- Indices Segments
- Indices Recovery
- Indices Shard Stores
- Clear Cache
- Flush
- Refresh
- Optimize
- Upgrade
- cat APIs
- Cluster APIs
- Query DSL
- Mapping
- Field datatypes
- Meta-Fields
- Mapping parameters
analyzer
boost
coerce
copy_to
doc_values
dynamic
enabled
fielddata
format
geohash
geohash_precision
geohash_prefix
ignore_above
ignore_malformed
include_in_all
index
index_options
lat_lon
fields
norms
null_value
position_increment_gap
precision_step
properties
search_analyzer
similarity
store
term_vector
- Dynamic Mapping
- Transform
- Analysis
- Analyzers
- Tokenizers
- Token Filters
- Standard Token Filter
- ASCII Folding Token Filter
- Length Token Filter
- Lowercase Token Filter
- Uppercase Token Filter
- NGram Token Filter
- Edge NGram Token Filter
- Porter Stem Token Filter
- Shingle Token Filter
- Stop Token Filter
- Word Delimiter Token Filter
- Stemmer Token Filter
- Stemmer Override Token Filter
- Keyword Marker Token Filter
- Keyword Repeat Token Filter
- KStem Token Filter
- Snowball Token Filter
- Phonetic Token Filter
- Synonym Token Filter
- Compound Word Token Filter
- Reverse Token Filter
- Elision Token Filter
- Truncate Token Filter
- Unique Token Filter
- Pattern Capture Token Filter
- Pattern Replace Token Filter
- Trim Token Filter
- Limit Token Count Token Filter
- Hunspell Token Filter
- Common Grams Token Filter
- Normalization Token Filter
- CJK Width Token Filter
- CJK Bigram Token Filter
- Delimited Payload Token Filter
- Keep Words Token Filter
- Keep Types Token Filter
- Classic Token Filter
- Apostrophe Token Filter
- Character Filters
- ICU Analysis Plugin
- Modules
- Index Modules
- Testing
- Glossary of terms
- Release Notes
WARNING: Version 2.0 of Elasticsearch has passed its EOL date.
This documentation is no longer being maintained and may be removed. If you are running this version, we strongly advise you to upgrade. For the latest information, see the current release documentation.
Match Query
editMatch Query
editA family of match
queries that accepts text/numerics/dates, analyzes
them, and constructs a query. For example:
{ "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.
There are three types of match
query: boolean
, phrase
, and phrase_prefix
:
boolean
editThe 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
optional should
clauses to match can be set using the
minimum_should_match
parameter.
The analyzer
can be set to control which analyzer will perform the
analysis process on the text. It defaults to the field explicit mapping
definition, or the default search analyzer.
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
.
Fuzziness
editfuzziness
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 rewrite
method the fuzzy_rewrite
parameter allows to control how the query will get
rewritten.
Here is an example when providing additional parameters (note the slight
change in structure, message
is the field name):
{ "match" : { "message" : { "query" : "this is a test", "operator" : "and" } } }
Zero terms query
editIf 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.
{ "match" : { "message" : { "query" : "to be or not to be", "operator" : "and", "zero_terms_query": "all" } } }
Cutoff frequency
editThe match query supports a cutoff_frequency
that allows
specifying an absolute or relative document frequency where high
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
operator match.
This query allows handling stopwords
dynamically at runtime, is domain
independent and doesn’t require a stopword file. It prevents scoring /
iterating high frequency terms and only takes the terms into account if a
more significant / lower frequency term matches a document. Yet, if all
of the query terms are above the given cutoff_frequency
the query is
automatically transformed into a pure conjunction (and
) query to
ensure fast execution.
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
1.0
.
Here is an example showing a query composed of stopwords exclusively:
{ "match" : { "message" : { "query" : "to be or not to be", "cutoff_frequency" : 0.001 } } }
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 Relevance is broken.
phrase
editThe match_phrase
query analyzes the text and creates a phrase
query
out of the analyzed text. For example:
{ "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:
{ "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 the
analysis process on the text. It defaults to the field explicit mapping
definition, or the default search analyzer, for example:
{ "match_phrase" : { "message" : { "query" : "this is a test", "analyzer" : "my_analyzer" } } }
match_phrase_prefix
editThe 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:
{ "match_phrase_prefix" : { "message" : "this is a test" } }
Or:
{ "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:
{ "match_phrase_prefix" : { "message" : { "query" : "this is a test", "max_expansions" : 10 } } }