# Combine aggregations from two nested objects

**URL:** <https://discuss.elastic.co/t/combine-aggregations-from-two-nested-objects/77501>\
**Category:** Elasticsearch\
**Created:** [March 6, 2017, 12:53pm UTC](https://discuss.elastic.co/t/combine-aggregations-from-two-nested-objects/77501 "2017-03-06T12:53:40Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![esbenp](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/esbenp/32/16116_2.png) [@esbenp](https://discuss.elastic.co/u/esbenp)\
**Post date:** [March 6, 2017, 12:53pm UTC](https://discuss.elastic.co/t/combine-aggregations-from-two-nested-objects/77501/1 "2017-03-06T12:53:40Z")

</div>

How can I create an combined aggregation in Elasticsearch using multiple nested objects?

What I want to achieve is the SUM of quantity grouped by country in a mapping that looks like this

```
curl -XPUT 'localhost:9200' -d '{
    "index": "test",
    "type": "orders",
    "body": {
        "orders": {
            "order_number": {
                "type": "string",
                "index": "not_analyzed"
            },
            "buyer": {
                "type": "nested",
                "properties": {
                    "country": {
                        "type": "string",
                        "index": "not_analyzed"
                    }
                }
            },
            "products": {
                "type": "nested",
                "properties": {
                    "name": {
                        "type": "string",
                        "index": "not_analyzed"
                    },
                    "quantity": {
                        "type": "string",
                        "index": "not_analyzed"
                    }
                }
            }
        }
    }
}'

```

Using this test data

```
curl -XPOST 'localhost:9200/_bulk' -d '
    {"index": {"_index": "test", "_type": "orders", "_id": 1}}
    {"order_number": "1", "buyer": {"country": "US"}, "products": [{"name": "A product", "quantity": 10}]}
    {"index": {"_index": "test", "_type": "orders", "_id": 2}}
    {"order_number": "2", "buyer": {"country": "DE"}, "products": [{"name": "A product", "quantity": 10}]}
    {"index": {"_index": "test", "_type": "orders", "_id": 3}}
    {"order_number": "3", "buyer": {"country": "US"}, "products": [{"name": "A product", "quantity": 10}]}
'

```

The result should be that there was sold 10 "A product" in DE and 20 in US. How can I create this aggregation when the properties in question are on separate nested objects?

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [April 3, 2017, 12:53pm UTC](https://discuss.elastic.co/t/combine-aggregations-from-two-nested-objects/77501/2 "2017-04-03T12:53:43Z")

</div>

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