# How to query an elasticsearch aggregation with a term and sum on different nested objects?

**URL:** <https://discuss.elastic.co/t/how-to-query-an-elasticsearch-aggregation-with-a-term-and-sum-on-different-nested-objects/57044>\
**Category:** Elasticsearch\
**Created:** [August 2, 2016, 5:23pm UTC](https://discuss.elastic.co/t/how-to-query-an-elasticsearch-aggregation-with-a-term-and-sum-on-different-nested-objects/57044 "2016-08-02T17:23:27Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![strife25](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/strife25/32/11164_2.png) [@strife25](https://discuss.elastic.co/u/strife25)\
**Post date:** [August 2, 2016, 5:23pm UTC](https://discuss.elastic.co/t/how-to-query-an-elasticsearch-aggregation-with-a-term-and-sum-on-different-nested-objects/57044/1 "2016-08-02T17:23:27Z")

</div>

Hello,

I tried posting my question to StackOverflow and haven't had any luck with responses. For the full information breakdown on my situation, I would greatly appreciate any help with my SO question:

> <https://stackoverflow.com/questions/38699762/how-to-query-an-elasticsearch-aggregation-with-a-term-and-sum-on-different-neste>

For more information, I am using Elasticsearch 1.5 and am basically trying to perform multiple aggregations against a set of nested objects in a document.

I am currently indexing documents with the following structure:

```
{
    "metadata": {
        "tenant": "home",
        "timestamp": "2016-03-24T23:59:38Z"
    },
    "value": {
        { "key": "foo", "int_value": 100 },
        { "key": "bar", "str_value": "taco" }
    }
}

```

in my mapping, `value` is set to be a `nested` type.

This is my query:

```
{
  "query": {
    "match_all": {}
  },
  "aggs": {
    "filters": {
      "filter": {
        "bool": {
          "must": [
            {
              "term": {
                "metadata.org": "gw"
              }
            }
          ]
        }
      },
      "aggs": {
        "intervals": {
          "date_histogram": {
            "field": "metadata.timestamp",
            "interval": "1d",
            "min_doc_count": 0,
            "format": "yyyy-MM-dd'T'HH:mm:ss'Z'"
          },
          "aggs": {
            "nested_type": {
              "nested": {
                "path": "value"
              },
              "aggs": {
                "key_filter": {
                  "filter": {
                    "term": {
                      "value.key": "bar"
                    }
                  },
                  "aggs": {
                    "groupBy": {
                      "terms": {
                        "field": "value.str_value"
                      },
                      "aggs": {
                        "other_nested": {
                          "reverse_nested": {
                            "path": "value"
                          },
                          "aggs": {
                            "key_filter": {
                              "filter": {
                                "term": {
                                  "value.key": "foo"
                                }
                              },
                              "aggs": {
                                "amount_sum": {
                                  "sum": {
                                    "field": "value.int_value"
                                  }
                                }
                              }
                            }
                          }
                        }
                      }
                    }
                  }
                }
              }
            }
          }
        }
      }
    }
  }
}

```

This is my response:

```
{
  "took": 1,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "failed": 0
  },
  "hits": {
    "total": 7,
    "max_score": 0.0,
    "hits": []
  },
  "aggregations": {
    "filters": {
      "doc_count": 2,
      "intervals": {
        "buckets": [
          {
            "key_as_string": "2016-03-24T00:00:00Z",
            "key": 1458777600000,
            "doc_count": 2,
            "nested_type": {
              "doc_count": 5,
              "key_filter": {
                "doc_count": 2,
                "groupBy": {
                  "doc_count_error_upper_bound": 0,
                  "sum_other_doc_count": 0,
                  "buckets": [
                    {
                      "key": "taco",
                      "doc_count": 1,
                      "other_nested": {
                        "doc_count": 1,
                        "key_filter": {
                          "doc_count": 1,
                          "amount_sum": {
                            "value": 0.0 // <----- I expect this value to be 100.0
                          }
                        }
                      }
                    }
                  ]
                }
              }
            }
          }
        ]
      }
    }
  }
}

```

Would anyone have any idea on how to get this type of query to work?

---

<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:** [July 5, 2017, 10:30pm UTC](https://discuss.elastic.co/t/how-to-query-an-elasticsearch-aggregation-with-a-term-and-sum-on-different-nested-objects/57044/2 "2017-07-05T22:30:38Z")

</div>


