Return 只有对象中的数组元素包含特定值

Return only elements of an array in an object that contain a certain value

我在弹性搜索索引中找到了以下文档:

{
    "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 参数是有限的,因此我们不会 return 整个 命中,而只是匹配的子组。 typecomponent.id 不能在嵌套字段中 "seen",因此我们已明确包含它们。

响应应该如下所示:

您现在已经准确地拥有了所需的属性,因此稍加 post 处理即可获得所需的格式!


我不熟悉执行此操作的更简洁的方法,但如果你们有人这样做,我很乐意学习它。