While a phrase query simply excludes documents which don’t contain the exact
query phrase, a proximity query — a phrase query where slop
is greater
than 0
— incorporates the proximity of the query terms into the final
relevance _score
. By setting a high slop
value like 50
or 100
, you can
exclude documents where the words are really too far apart, but give a higher
score to documents where the words are closer together.
The following proximity query for "quick dog"
matches both documents which
contain the words "quick"
and "dog"
, but gives a higher score to the
document where the words are nearer to each other:
POST /my_index/my_type/_search
{
"query": {
"match_phrase": {
"title": {
"query": "quick dog",
"slop": 50 (1)
}
}
}
}
-
Note the high
slop
value
{
"hits": [
{
"_id": "3",
"_score": 0.75, (1)
"_source": {
"title": "The quick brown fox jumps over the quick dog"
}
},
{
"_id": "2",
"_score": 0.28347334, (2)
"_source": {
"title": "The quick brown fox jumps over the lazy dog"
}
}
]
}
-
Higher score because
quick
anddog
are close together. -
Lower score because
quick
anddog
are further apart.