可以使用Span Near Query来完成。我不确定您的实验出了什么问题,以及您所说的“包装”是什么意思。我只能猜测,也许您指定了 "in_order":"false" 而您的查询只是忽略了术语的顺序。你能举个例子吗?
为避免查询跨越多个项目,您需要使用“position_offset_gap”属性来增加映射中项目之间的“间隙”。这是一个例子:
curl -XDELETE "localhost:9200/slop-test"
echo
curl -XPUT "localhost:9200/slop-test" -d '{
"settings" : {
"index" : {
"number_of_shards" : 1,
"number_of_replicas" : 0
}
},
"mappings" : {
"doc" : {
"properties" : {
"items" : {
"type" : "string",
"position_offset_gap": 100
}
}
}
}
}'
echo
curl -XPUT "localhost:9200/slop-test/doc/1" -d '{
"items":
[
"ONE BLAH BLAH BLAH TWO BLAH BLAH BLAH THREE",
"FOUR BLAH BLAH BLAH FIVE BLAH BLAH BLAH SIX"
]
}'
curl -XPOST "localhost:9200/slop-test/_refresh"
echo
curl "localhost:9200/slop-test/_search?pretty=true" -d '{
"query" : {
"span_near" : {
"clauses" : [
{ "span_term" : { "items" : "one" } },
{ "span_term" : { "items" : "two" } },
{ "span_term" : { "items" : "three" } }
],
"slop" : 40,
"in_order" : true
}
}
}'
echo
curl "localhost:9200/slop-test/_search?pretty=true" -d '{
"query" : {
"span_near" : {
"clauses" : [
{ "span_term" : { "items" : "one" } },
{ "span_term" : { "items" : "two" } },
{ "span_term" : { "items" : "six" } }
],
"slop" : 40,
"in_order" : true
}
}
}'
echo