这正是 match_phrase 查询(参见 here)所做的。
它检查术语的位置,在它们的存在之上。
例如,这些文件:
POST test/values
{
"test": "Hello World"
}
POST test/values
{
"test": "Hello nice World"
}
POST test/values
{
"test": "World, I don't say hello"
}
都可以通过基本的match 查询找到:
POST test/_search
{
"query": {
"match": {
"test": "Hello World"
}
}
}
但是使用match_phrase,只会返回第一个文档:
POST test/_search
{
"query": {
"match_phrase": {
"test": "Hello World"
}
}
}
{
...
"hits": {
"total": 1,
"max_score": 2.3953633,
"hits": [
{
"_index": "test",
"_type": "values",
"_id": "qFZAKYOTQh2AuqplLQdHcA",
"_score": 2.3953633,
"_source": {
"test": "Hello World"
}
}
]
}
}
在您的情况下,您希望接受您的条款之间存在一定距离。这可以通过slop 参数来实现,该参数指示您允许您的术语与另一个术语相距多远:
POST test/_search
{
"query": {
"match": {
"test": {
"query": "Hello world",
"slop":1,
"type": "phrase"
}
}
}
}
通过最后一个请求,您也可以找到第二个文档:
{
...
"hits": {
"total": 2,
"max_score": 0.38356602,
"hits": [
{
"_index": "test",
"_type": "values",
"_id": "7mhBJgm5QaO2_aXOrTB_BA",
"_score": 0.38356602,
"_source": {
"test": "Hello World"
}
},
{
"_index": "test",
"_type": "values",
"_id": "VKdUJSZFQNCFrxKk_hWz4A",
"_score": 0.2169777,
"_source": {
"test": "Hello nice World"
}
}
]
}
}
您可以在definitive guide 中找到有关此用例的整章。