gpt4 book ai didi

elasticsearch - 仅返回对象中包含特定值的数组元素

转载 作者:行者123 更新时间:2023-12-02 22:54:09 24 4
gpt4 key购买 nike

我在 flex 搜索索引中有以下文档:

{
"type": "foo",
"components": [{
"id": "1234123", ,
"data_collections": [{
"date_time": "2020-03-02T08:14:48+00:00",
"group": "1",
"group_description": "group1",
"measures": [{
"measure_name": "MEASURE_1",
"actual": "23.34"
}, {
"measure_name": "MEASURE_2",
"actual": "5"
}, {
"measure_name": "MEASURE_3",
"actual": "string_message"
}, {
"measure_name": "MEASURE_4",
"actual": "another_string"
}
]
},
{
"date_time": "2020-03-03T08:14:48+00:00",
"group": "2",
"group_description": "group2",
"measures": [{
"measure_name": "MEASURE_1",
"actual": "23.34"
}, {
"measure_name": "MEASURE_4",
"actual": "foo"
}, {
"measure_name": "MEASURE_5",
"actual": "bar"
}, {
"measure_name": "MEASURE_6",
"actual": "4"
}
]
}
]
}
]
}

现在,我正在尝试查找此文档的映射和查询,以便结果仅包含我所插入的组和measure_names。到目前为止,我仍然可以进行查询,但是我将始终检索整个文档。这是不可行的,因为一系列的测量值可能很大,而且在大多数情况下,我想要一个小的子集。

例如,我正在搜索带有 "group": "1""measure_name": "MEASURE_"的文档,而我想要获得的结果如下所示:
{
"_id": "oiqwueou8931283u12",
"_source": {
"type": "foo",
"components": [{
"id": "1234123", ,
"data_collections": [{
"date_time": "2020-03-02T08:14:48+00:00",
"group": "1",
"group_description": "group1",
"measures": [{
"measure_name": "MEASURE_1",
"actual": "23.34"
}
]
}
]
}
]
}
}

我认为最接近我要寻找的是 source参数,但是据我所知,没有办法过滤像 {"measure_name": {"value": "MEASURE_1"}}这样的值

谢谢。

最佳答案

我想到的最简单的映射是

PUT timo
{
"mappings": {
"properties": {
"components": {
"type": "nested",
"properties": {
"data_collections": {
"type": "nested",
"properties": {
"measures": {
"type": "nested"
}
}
}
}
}
}
}
}

并且搜索查询将是
GET timo/_search
{
"_source": ["inner_hits", "type", "components.id"],
"query": {
"bool": {
"must": [
{
"nested": {
"path": "components.data_collections",
"query": {
"term": {
"components.data_collections.group.keyword": {
"value": "1"
}
}
},
"inner_hits": {}
}
},
{
"nested": {
"path": "components.data_collections.measures",
"query": {
"term": {
"components.data_collections.measures.measure_name.keyword": {
"value": "MEASURE_1"
}
}
},
"inner_hits": {}
}
}
]
}
}
}

注意每个子查询下的 inner_hits 参数,并且 _source参数是有限的,因此我们不返回整个匹配,而仅返回匹配的子组。在嵌套字段中无法“看到” typecomponent.id,因此我们已明确包含它们。

响应应如下所示:
enter image description here

现在,您已经精确地拥有了所需的属性,因此,进行一些后期处理即可获得所需的格式!

我不熟悉使用更干净的方法来执行此操作,但是如果大家都愿意,我将很高兴学习它。

关于elasticsearch - 仅返回对象中包含特定值的数组元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61012625/

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