How to aggregate a deep nested document - built with recursion

I have a document structure which looks something like

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

And I can send a search request something like this:

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

But this will only match on the first level of the path, is there a way short of having to create a nested aggregation for every level (it could be 10 deep or maybe more) for elastic to follow the path of the nested document until it reaches the end and collect each field that it finds?

So the answer above would be 100?

Thanks!

This is a very interesting question. From my understanding with aggs is that it operates over a collection of documents in the index, not over multi-level within a document as described in the question. Would like to hear from Elastic on this.

However, my suggestion to you is to break this document into multiple documents with a "common field" so you can aggs data over a collection of documents having the same value defined in the "common field"

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.