1

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

{
    "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"and的文档,"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"}}

谢谢。

4

1 回答 1

2

想到的最简单的映射是

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参数是有限的,因此我们不会返回整个命中,而是只返回匹配的子组。type并且component.id不能在嵌套字段中“看到”,因此我们已明确包含它们。

响应应如下所示: 在此处输入图像描述

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


我不熟悉这种更清洁的方法,但如果你们中的任何一个都这样做,我会很高兴学习它。

于 2020-04-03T13:53:17.700 回答