Match phrase query
The match_phrase query analyzes the text and creates a phrase query out of the analyzed text. For example:
resp = client.search(
query={
"match_phrase": {
"message": "this is a test"
}
},
)
print(resp) response = client.search(
body: {
query: {
match_phrase: {
message: 'this is a test'
}
}
}
)
puts response res, err := es.Search(
es.Search.WithBody(strings.NewReader(`{
"query": {
"match_phrase": {
"message": "this is a test"
}
}
}`)),
es.Search.WithPretty(),
)
fmt.Println(res, err) const response = await client.search({
query: {
match_phrase: {
message: "this is a test",
},
},
});
console.log(response); GET /_search
{
"query": {
"match_phrase": {
"message": "this is a test"
}
}
} Parameters for <field>
-
query -
(Required) Text, number, boolean value or date you wish to find in the provided
<field>. -
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. -
slop - (Optional, integer) Maximum number of positions allowed between matching tokens. Defaults to
0. Transposed terms have a slop of2. -
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.
-
A phrase query matches terms up to a configurable slop (which defaults to 0) in any order. Transposed terms have a slop of 2.
Analyzer in the match phrase query
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:
resp = client.search(
query={
"match_phrase": {
"message": {
"query": "this is a test",
"analyzer": "my_analyzer"
}
}
},
)
print(resp) response = client.search(
body: {
query: {
match_phrase: {
message: {
query: 'this is a test',
analyzer: 'my_analyzer'
}
}
}
}
)
puts response res, err := es.Search(
es.Search.WithBody(strings.NewReader(`{
"query": {
"match_phrase": {
"message": {
"query": "this is a test",
"analyzer": "my_analyzer"
}
}
}
}`)),
es.Search.WithPretty(),
)
fmt.Println(res, err) const response = await client.search({
query: {
match_phrase: {
message: {
query: "this is a test",
analyzer: "my_analyzer",
},
},
},
});
console.log(response); GET /_search
{
"query": {
"match_phrase": {
"message": {
"query": "this is a test",
"analyzer": "my_analyzer"
}
}
}
}
© 2023-2025 Elasticsearch
As of September 2024, Elasticsearch is available under a choice of three licenses: the Server Side Public License (SSPL), the Elastic License, or the AGPLv3 (OSI approved).
Elasticsearch and the Elasticsearch logo are trademarks of Elasticsearch B.V., registered in the U.S. and in other countries.
https://www.elastic.co/guide/en/elasticsearch/reference/8.17/query-dsl-match-query-phrase.html