如何在Elasticsearch中聚合深层嵌套文档-使用递归构建

时间:2019-04-10 19:20:24

标签: elasticsearch elasticsearch-aggregation

我的文档结构看起来像

{
    "orderNo": "123O",
    "tradeNo": "567T",
    "orderConsideration": 10,
    "children": [{
        "orderNo": "124O",
        "tradeNo": "566T",
        "orderConsideration": 15,
        "children": [{
            "orderNo": "126O",
            "tradeNo": "565T",
            "orderConsideration": 20,
            "children": [{
                "orderNo": "127O",
                "tradeNo": "564T",
                "orderConsideration": 25,
                "children": [{
                    "orderNo": "128O",
                    "tradeNo": "563T",
                    "orderConsideration": 30,
                    "children": []
                }]
            }]
        }]
    }]
}

我可以发送类似这样的搜索请求:

GET /data_nested_test/_search
{
    "size": 0,
    "aggs": {
        "group_by_nested": {
            "nested": {
                "path": "children"
            },
            "aggs": {
                "sum_by_consideration": {
                    "sum": {
                        "field": "children.orderConsideration"
                    }
                }
            }
        }
    }
}

但是这只会在路径的第一层上匹配,是否有必要为每个层创建嵌套的聚合(可能是10层甚至更多),以使弹性沿着嵌套的路径移动文档直到到达末尾并收集找到的每个字段?

所以上面的答案是

 100

谢谢!

0 个答案:

没有答案