【问题标题】:Elasticsearch aggregation of deep nested type深度嵌套类型的 Elasticsearch 聚合
【发布时间】:2015-08-07 11:42:47
【问题描述】:

之前我问过this的问题。

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

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

"_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 )
        }
    ]
}

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

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

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

【问题讨论】:

    标签: json elasticsearch aggregate


    【解决方案1】:

    这与之前的答案并没有什么不同。您只需稍微修改字段名称以考虑额外的嵌套。除此之外,映射中无需更改任何内容。请注意,此查询仅在不更改映射的情况下工作,因为 responseresults 都是具有单个元素的数组,如果不是这种情况,它将涉及更多,并且需要映射更改和不同的查询。

    查询现在如下所示:

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

    【讨论】:

    • 你又是对的。显然,我仍然无法将这些问题全盘考虑。再次感谢:-)
    • 我想为她的另一个字段聚合什么,比如 :response.results.items.prop.item_property_2 那么我如何在查询中使用它?
    • @SurajDalvi 请创建一个新问题。
    猜你喜欢
    • 1970-01-01
    • 2018-11-17
    • 2021-12-14
    • 2015-11-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多