json - 深度嵌套类型的 Elasticsearch 聚合

标签 json elasticsearch aggregate

以前我问过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 )
        }
    ]
}

所以我保留了关键属性 keyword , items , 和 item_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"
            }
        }
    }
}

最佳答案

这与之前的答案并没有什么不同。您只需稍微修改字段名称以考虑额外的嵌套。除此之外,映射中无需更改任何内容。请注意,此查询无需更改映射即可工作,只是因为 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
            } ]
          }
        }
      } ]
    }
  }
}

关于json - 深度嵌套类型的 Elasticsearch 聚合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31864722/

相关文章:

javascript - 检查属性是否未定义

javascript - 如何合并这些父/子 javascript 对象?

ruby-on-rails - Rails & Memcached : Optimizing multiple fetches

java - ElasticSearch 内存不足

elasticsearch - Elasticsearch 突出显示不起作用

r - 按组选择前 N 个值

java - 将 javascript 数组传递给 java servlet

elasticsearch - Elasticsearch-如何在升级后删除卡住的持久设置

python - 通过保持所有字符列完整进行分组

Python Pandas : Sort and group by, 然后对第二列的两个连续行求和以获得第三列的特定值