gpt4 book ai didi

json - Elasticsearch 短语搜索

转载 作者:行者123 更新时间:2023-11-29 02:53:47 25 4
gpt4 key购买 nike

我在 Elasticsearch 中有一个文档,看起来像...

{
"items":
[
"ONE BLAH BLAH BLAH TWO BLAH BLAH BLAH THREE",
"FOUR BLAH BLAH BLAH FIVE BLAH BLAH BLAH SIX"
]
}

我希望能够使用诸如...的短语查询来搜索此文档

{
"match_phrase" : {
"items" : "ONE TWO THREE"
}
}

因此无论中间的单词如何,它都会匹配。单词也需要按此顺序排列。我意识到这可以通过 slop 属性来实现,但是当我试验它时,如果 slop 超过我正在搜索的内容之间的单词,它似乎会换行,因为这是不确定数量的单词我认为不合适。此外,我只需要搜索数组中的每个项目,所以...

{
"match_phrase" : {
"items" : "ONE TWO SIX"
}
}

不会匹配此文档,因为 SIX 在数组中与 ONETWO 不同。

所以我的问题是,这是否可以通过 elasticsearch 实现,还是我必须创建一个对象数组并使用嵌套查询来搜索它们?

最佳答案

可以使用 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

关于json - Elasticsearch 短语搜索,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12567722/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com