Elasticsearch aggregation: sum of last children per parent

Having a parent-children structure in Elasticsearch representing child order elements with order_revision , I want to generate a price histogram that displays the sum of quantity .

 { "_type": "order", "_id": "1063220887", "_score": 1, "_source": { "order_id": "1063220887", "product_id": "10446350", "timestamp": 1462713302000 } } { "_type": "order_revision", "_id": "10234234", "_parent": "1063220887", "_source": { "price": 9, "quantity": 3, "revision": 361, "timestamp": 1462712196000 } } 

The following aggregation basically works, but returns the sum of all existing revisions.

  { "aggs": { "orders": { "filter": { "has_parent": { "parent_type": "order" } }, "aggs": { "quantity_per_price": { "histogram": { "field": "price", "interval": 1 } "aggs": { "sum": {"field": quantity"} } } } } } } 

In the final version, it should only return the sum of the quantity fields for the latest version (with the highest / newest timestamp ) of each order. I'm not quite sure how to come up with an aggregation that groups by order_id and selects only the very last child, and I'm not sure that this structure of the parent and child elements is the best way to model this data.

+5
source share
1 answer

The simplest version is that the document will be marked with the most recent revision ( "latest": true ). Then it becomes a simple problem to add a query or filter aggregation to filter only the latest versions.

0
source

Source: https://habr.com/ru/post/1261019/


All Articles