gpt4 book ai didi

elasticsearch - Elasticsearch 中的对象数组搜索支持

转载 作者:行者123 更新时间:2023-11-29 02:57:31 27 4
gpt4 key购买 nike

我在 Elasticsearch 中有一个对象数组。

我想在不使用脚本的情况下搜索特定字段值是否出现在数组的前 2 位

假设我的ES数据如下

[
{
"_id": "TestID1",
"data": [
{
"name": "Test1",
"priority": 2
},
{
"name": "Test2",
"priority": 3
},
{
"name": "Test3",
"priority": 4
}
]
},
{
"_id": "TestID2",
"data": [
{
"name": "Test3",
"priority": 2
},
{
"name": "Test9",
"priority": 3
},
{
"name": "Test5",
"priority": 4
},
{
"name": "Test10",
"priority": 5
}
]
},
{
"_id": "TestID3",
"data": [
{
"name": "Test1",
"priority": 2
},
{
"name": "Test2",
"priority": 3
},
{
"name": "Test3",
"priority": 6
}
]
}
]

在这里,我想进行一个查询,在数据数组的前 2 个元素中搜索 _Test3_ ONLY

在这里搜索会返回结果

_id: TestID2的数据

因为只有 TestID2 在数据数组的前 2 位有 Test3

最佳答案

如果不使用脚本,您将无法直接执行此类请求。我能想到的唯一解决方案是创建仅包含前 2 个元素的数组字段的副本。然后您就可以搜索该字段。

您可以添加 ingest pipeline自动修剪您的阵列。

PUT /_ingest/pipeline/top2_elements
{
"description": "Create a top2 field containing only the first two values of an array",
"processors": [
{
"script": {
"source": "ctx.top2 = [ctx.data[0], ctx.data[1]]"
}
}
]
}

关于elasticsearch - Elasticsearch 中的对象数组搜索支持,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46702657/

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