Returns documents that match a provided text, number, date or boolean value. The provided text is analyzed before matching.
The match query is the standard query for performing a full-text search,
including options for fuzzy matching.
response = client.search(
body: {
query: {
match: {
message: {
query: 'this is a test'
}
}
}
}
)
puts response
res, err := es.Search(
es.Search.WithBody(strings.NewReader(`{
"query": {
"match": {
"message": {
"query": "this is a test"
}
}
}
}`)),
es.Search.WithPretty(),
)
fmt.Println(res, err)
GET /_search
{
"query": {
"match": {
"message": {
"query": "this is a test"
}
}
}
}
-
query -
(Required) Text, number, boolean value or date you wish to find in the provided
<field>.The
matchquery analyzes any provided text before performing a search. This means thematchquery can searchtextfields for analyzed tokens rather than an exact term. -
analyzer -
(Optional, string) Analyzer used to convert the text in the
queryvalue into tokens. Defaults to the index-time analyzer mapped for the<field>. If no analyzer is mapped, the index’s default analyzer is used. -
auto_generate_synonyms_phrase_query -
(Optional, Boolean) If
true, match phrase queries are automatically created for multi-term synonyms. Defaults totrue.See Use synonyms with match query for an example.
-
fuzziness - (Optional, string) Maximum edit distance allowed for matching. See Fuzziness for valid values and more information. See Fuzziness in the match query for an example.
-
max_expansions -
(Optional, integer) Maximum number of terms to which the query will
expand. Defaults to
50. -
prefix_length -
(Optional, integer) Number of beginning characters left unchanged for fuzzy
matching. Defaults to
0. -
fuzzy_transpositions -
(Optional, Boolean) If
true, edits for fuzzy matching include transpositions of two adjacent characters (ab → ba). Defaults totrue. -
fuzzy_rewrite -
(Optional, string) Method used to rewrite the query. See the
rewriteparameter for valid values and more information.If the
fuzzinessparameter is not0, thematchquery uses afuzzy_rewritemethod oftop_terms_blended_freqs_${max_expansions}by default. -
lenient -
(Optional, Boolean) If
true, format-based errors, such as providing a textqueryvalue for a numeric field, are ignored. Defaults tofalse. -
operator -
(Optional, string) Boolean logic used to interpret text in the
queryvalue. Valid values are:-
OR(Default) -
For example, a
queryvalue ofcapital of Hungaryis interpreted ascapital OR of OR Hungary. -
AND -
For example, a
queryvalue ofcapital of Hungaryis interpreted ascapital AND of AND Hungary.
-
-
minimum_should_match -
(Optional, string) Minimum number of clauses that must match for a document to be returned. See the
minimum_should_matchparameter for valid values and more information. -
zero_terms_query -
(Optional, string) Indicates whether no documents are returned if the
analyzerremoves all tokens, such as when using astopfilter. Valid values are:-
none(Default) -
No documents are returned if the
analyzerremoves all tokens. -
all -
Returns all documents, similar to a
match_allquery.
See Zero terms query for an example.
-
You can simplify the match query syntax by combining the <field> and query
parameters. For example:
response = client.search(
body: {
query: {
match: {
message: 'this is a test'
}
}
}
)
puts response
res, err := es.Search(
es.Search.WithBody(strings.NewReader(`{
"query": {
"match": {
"message": "this is a test"
}
}
}`)),
es.Search.WithPretty(),
)
fmt.Println(res, err)
GET /_search
{
"query": {
"match": {
"message": "this is a test"
}
}
}
The 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 parameter 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.
Here is an example with the operator parameter:
response = client.search(
body: {
query: {
match: {
message: {
query: 'this is a test',
operator: 'and'
}
}
}
}
)
puts response
res, err := es.Search(
es.Search.WithBody(strings.NewReader(`{
"query": {
"match": {
"message": {
"query": "this is a test",
"operator": "and"
}
}
}
}`)),
es.Search.WithPretty(),
)
fmt.Println(res, err)
GET /_search
{
"query": {
"match": {
"message": {
"query": "this is a test",
"operator": "and"
}
}
}
}
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 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.
Fuzzy transpositions (ab → ba) are allowed by default but can be disabled
by setting fuzzy_transpositions to false.
Fuzzy matching is not applied to terms with synonyms or in cases where the analysis process produces multiple tokens at the same position. Under the hood these terms are expanded to a special synonym query that blends term frequencies, which does not support fuzzy expansion.
response = client.search(
body: {
query: {
match: {
message: {
query: 'this is a testt',
fuzziness: 'AUTO'
}
}
}
}
)
puts response
res, err := es.Search(
es.Search.WithBody(strings.NewReader(`{
"query": {
"match": {
"message": {
"query": "this is a testt",
"fuzziness": "AUTO"
}
}
}
}`)),
es.Search.WithPretty(),
)
fmt.Println(res, err)
GET /_search
{
"query": {
"match": {
"message": {
"query": "this is a testt",
"fuzziness": "AUTO"
}
}
}
}
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.
response = client.search(
body: {
query: {
match: {
message: {
query: 'to be or not to be',
operator: 'and',
zero_terms_query: 'all'
}
}
}
}
)
puts response
res, err := es.Search(
es.Search.WithBody(strings.NewReader(`{
"query": {
"match": {
"message": {
"query": "to be or not to be",
"operator": "and",
"zero_terms_query": "all"
}
}
}
}`)),
es.Search.WithPretty(),
)
fmt.Println(res, err)
GET /_search
{
"query": {
"match": {
"message": {
"query": "to be or not to be",
"operator": "and",
"zero_terms_query": "all"
}
}
}
}
The match query supports multi-terms synonym expansion with the synonym_graph token filter. When this filter is used, the parser creates a phrase query for each multi-terms synonyms.
For example, the following synonym: "ny, new york" would produce:
(ny OR ("new york"))
It is also possible to match multi terms synonyms with conjunctions instead:
$params = [
'body' => [
'query' => [
'match' => [
'message' => [
'query' => 'ny city',
'auto_generate_synonyms_phrase_query' => false,
],
],
],
],
];
$response = $client->search($params);
var searchResponse = client.Search<object>(s => s
.AllIndices()
.Query(q => q
.Match(m => m
.Field("message")
.Query("ny city")
.AutoGenerateSynonymsPhraseQuery(false)
)
)
);
resp = client.search(
body={
"query": {
"match": {
"message": {
"query": "ny city",
"auto_generate_synonyms_phrase_query": False,
}
}
}
},
)
print(resp)
response = client.search(
body: {
query: {
match: {
message: {
query: 'ny city',
auto_generate_synonyms_phrase_query: false
}
}
}
}
)
puts response
res, err := es.Search(
es.Search.WithBody(strings.NewReader(`{
"query": {
"match": {
"message": {
"query": "ny city",
"auto_generate_synonyms_phrase_query": false
}
}
}
}`)),
es.Search.WithPretty(),
)
fmt.Println(res, err)
const response = await client.search({
body: {
query: {
match: {
message: {
query: 'ny city',
auto_generate_synonyms_phrase_query: false
}
}
}
}
})
console.log(response)
GET /_search
{
"query": {
"match" : {
"message": {
"query" : "ny city",
"auto_generate_synonyms_phrase_query" : false
}
}
}
}
The example above creates a boolean query:
(ny OR (new AND york)) city
that matches documents with the term ny or the conjunction new AND york.
By default the parameter auto_generate_synonyms_phrase_query is set to true.