Use a match_phrase query when search results must contain a group of words in a specified field and order. Unlike a match query, it treats the input as a phrase rather than independently matching its terms. section 10.6 section 2.3.6
Listing 2.12 Searching for books with an exact phrase
GET books/_search
{
"query": {
"match_phrase": { #1
"synopsis": "must-have book for every Java programmer" #2
}
}
}
Search the synopsis field for the complete phrase “must-have book for every Java programmer”; the example returns Effective Java. section 2.3.6
Adjust the query when real user input differs from the stored phrase:
- Set slop to a positive integer when words are missing or not in the original order. A slop of 1 permits one ignored word; the default is 0. section 10.6 section 2.3.7
- Combine match_phrase with other leaf queries inside a bool must clause to require both phrase and field criteria. section 2.5.2
- Add a highlight object at the same level as query when returned text should emphasize matching words in the selected field. section 2.3.6
Listing 10.11 A match_phrase query with slop that drops one word
GET books/_search
{
"query": {
"match_phrase": {
"synopsis": { #1
"query": "book every Java programmer", #2
"slop": 1 #3
}
}
}
}
Search “book every Java programmer” while setting slop to 1, allowing the omitted word “for.” section 10.6
