深层嵌套类型的Elasticsearch聚合

2024-04-14

之前我问过this https://stackoverflow.com/questions/31841542/elasticsearch-metric-aggregation-number-of-elements-in-array/31846457?noredirect=1#comment51642542_31846457问题。

示例文档是一个简化的文档。这对我了解非嵌套类型与嵌套类型的聚合差异很有帮助。然而,简化隐藏了进一步的复杂性,所以我必须在这里扩展这个问题。

所以我的实际文件更接近以下内容:

"_source": {
    "keyword": "my keyword",
    "response": [
        {
            "results": [
                {
                    "items": [
                        {
                            "prop": [
                                {
                                    "item_property_1": ["A"],
                                }
                            ]
                            ( ... other properties )
                        },
                        {
                            "prop": [
                                {
                                    "item_property_1": ["B"],
                                }
                            ]
                            ( ... other properties )
                        },
                        ( ... other items )
                    ]
                }
            ],
            ( ... other properties )
        }
    ]
}

所以我保留了关键的属性keyword, items, and item_property_1,但隐藏了许多其他使情况变得复杂的事情。首先,请注意,与引用的问题相比,有很多额外的嵌套:在根和“items”之间,以及“items”和“item_property_1”之间。此外,还请注意属性response and results都是具有单个元素的数组。这很奇怪,但事实就是这样:-)

现在,这个问题与上面引用的问题不同的原因是我尝试了接受的答案(它确实适用于那里的示例),但在这里不起作用。也就是说,如果我使用以下映射:

"items": {
    "type":"nested",
    "properties": {
        "prop": {
            "properties": {
                "item_property_1": {
                    "type": "string",
                    "index": "not_analyzed"
                },
            }
        }
    }
}

那么聚合不起作用。它返回零命中。

我稍后将进行编辑并提供一个随时可用的示例批量插入。

编辑: 好吧,下面我展示了三个查询,分别是:映射、批量插入和聚合(零命中)

映射(与"type":"nested"如之前回答的问题所示)

PUT /test2/_mapping/test3
{
    "test3": {
        "properties": {
            "keyword": {
                "type": "string",
                "index": "not_analyzed"
            },
            "response": {
                "properties": {
                    "results": {
                        "properties": {
                            "items": {
                                "type": "nested",
                                "properties": {
                                    "prop": {
                                        "properties": {
                                            "item_property_1": {
                                                "type": "string",
                                                "index": "not_analyzed"
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}

批量投放:

PUT /test2/test3/_bulk
{ "index": {}}
{    "keyword": "my keyword",    "response": [        {            "results": [                {                    "items": [                        {                            "prop": [                                {"item_property_1": ["A"]}                            ]                        },                        {                            "prop": [                                {"item_property_1": ["B"]}                            ]                        },                        {                            "prop": [                                {"item_property_1": ["A"]}                            ]                        }                    ]                }            ]        }    ]}
{ "index": {}}
{    "keyword": "different keyword",    "response": [        {            "results": [                {                    "items": [                        {                            "prop": [                                {"item_property_1": ["A"]}                            ]                        },                        {                            "prop": [                                {"item_property_1": ["C"]}                            ]                        }                    ]                }            ]        }    ]}

聚合(零命中):

POST /test2/test3/_search
{
    "size":0,
    "aggregations": {
        "item_property_1_count": {
            "terms":{
                "field":"item_property_1"
            }
        }
    }
}

这与之前的答案并没有什么不同。您所需要的只是稍微修改字段名称以考虑额外的嵌套。除此之外,映射中不需要进行任何更改。请注意,此查询无需更改映射即可工作,只是因为response and results都是具有单个元素的数组,如果不是这种情况,则会更加复杂,并且需要映射更改和不同的查询。

现在的查询如下所示:

{
  "size": 0,
  "aggregations": {
    "by_keyword": {
      "terms": {
        "field": "keyword"
      },
      "aggs": {
        "prop_1_count": {
          "nested": {
            "path": "response.results.items"
          },
          "aggs": {
            "prop_1": {
              "terms": {
                "field": "response.results.items.prop.item_property_1"
              }
            }
          }
        }
      }
    }
  }
}

结果:

{
  ...
  "aggregations" : {
    "by_keyword" : {
      "doc_count_error_upper_bound" : 0,
      "sum_other_doc_count" : 0,
      "buckets" : [ {
        "key" : "different keyword",
        "doc_count" : 1,
        "prop_1_count" : {
          "doc_count" : 2,
          "prop_1" : {
            "doc_count_error_upper_bound" : 0,
            "sum_other_doc_count" : 0,
            "buckets" : [ {
              "key" : "A",
              "doc_count" : 1
            }, {
              "key" : "C",
              "doc_count" : 1
            } ]
          }
        }
      }, {
        "key" : "my keyword",
        "doc_count" : 1,
        "prop_1_count" : {
          "doc_count" : 3,
          "prop_1" : {
            "doc_count_error_upper_bound" : 0,
            "sum_other_doc_count" : 0,
            "buckets" : [ {
              "key" : "A",
              "doc_count" : 2
            }, {
              "key" : "B",
              "doc_count" : 1
            } ]
          }
        }
      } ]
    }
  }
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

深层嵌套类型的Elasticsearch聚合 的相关文章

随机推荐