gpt4 book ai didi

python-3.x - Elasticsearch按数组中的出现或顺序搜索/过滤

转载 作者:行者123 更新时间:2023-12-03 01:20:04 24 4
gpt4 key购买 nike

我的索引中有一个数据字段,

I want only doc 2 as result i.e logically where b comes before a in the array field data.



doc 1:
data = ['a','b','t','k','p']

文件2:
data = ['p','b','i','o','a']

目前,我正在尝试在[a,b]上使用条款,然后在另一个代码段中检查顺序。
请提出更好的建议。

最佳答案

我的理解是,唯一的方法就是使用Span Queries,但是它不适用于值数组。

您将需要将值连接到单个text字段中,并以whitespace作为分隔符,重新整理文档并在该字段上使用Span Near查询:

请找到以下映射,样本文档,查询和响应:

对应:

PUT my_test_index
{
"mappings": {
"properties": {
"data":{
"type": "text"
}
}
}
}

样本文件:
POST my_test_index/_doc/1
{
"data": "a b"
}

POST my_test_index/_doc/2
{
"data": "b a"
}

跨度查询:
POST my_test_index/_search
{
"query": {
"span_near" : {
"clauses" : [
{ "span_term" : { "data" : "a" } },
{ "span_term" : { "data" : "b" } }
],
"slop" : 0, <--- This means only `a b` would return but `a c b` won't.
"in_order" : true <--- This means a should come first and the b
}
}
}

注意 斜率 controls the maximum number of intervening unmatched positions permitted.
响应:
{
"took" : 0,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 1,
"relation" : "eq"
},
"max_score" : 0.36464313,
"hits" : [
{
"_index" : "my_test_index",
"_type" : "_doc",
"_id" : "1",
"_score" : 0.36464313,
"_source" : {
"data" : "a b"
}
}
]
}
}

让我知道这是否有帮助!

关于python-3.x - Elasticsearch按数组中的出现或顺序搜索/过滤,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60623939/

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