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

eqqqjvef  于 2022-11-02  发布在  ElasticSearch
关注(0)|答案(1)|浏览(157)

我在ElasticSearch索引中找到了以下文档:

{
    "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"
                        }
                    ]
                }
            ]
        }
    ]
}

现在,我正在尝试为该文档确定Map和查询,以便结果只包含我感兴趣的组和measure_name。到目前为止,我可以进行查询,但我将始终检索整个文档,这是不可行的,因为度量数组可能非常大,而大多数情况下我希望得到一个小子集。
例如,我正在搜索包含"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"}}这样的值

  • 谢谢-谢谢
wgmfuz8q

wgmfuz8q1#

想到的最简单的Map是

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在嵌套字段中无法“看到”,因此我们显式地包含了它们。
响应应如下所示:

现在您已经精确地拥有了所需的属性,因此只需进行一些后期处理即可获得所需的格式!
我不熟悉这样做的更干净的方法,但如果你们中的任何一个知道,我很乐意学习它。

相关问题