# How to combine a filter AND and a nested aggregation?

**URL:** <https://discuss.elastic.co/t/how-to-combine-a-filter-and-and-a-nested-aggregation/29149>\
**Category:** Elasticsearch\
**Created:** [September 11, 2015, 4:00pm UTC](https://discuss.elastic.co/t/how-to-combine-a-filter-and-and-a-nested-aggregation/29149 "2015-09-11T16:00:03Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![vhj](https://avatars.discourse-cdn.com/v4/letter/v/f14d63/32.png) [@vhj](https://discuss.elastic.co/u/vhj)\
**Post date:** [September 11, 2015, 4:00pm UTC](https://discuss.elastic.co/t/how-to-combine-a-filter-and-and-a-nested-aggregation/29149/1 "2015-09-11T16:00:03Z")

</div>

Hi,

I'm having a lot of trouble using the Java API to get an average aggregation over a nested field. This is the query I'm sending:

```
{
  "aggregations" : {
    "nested" : {
      "nested" : {
        "path" : "hops"
      },
      "aggregations" : {
        "aggregationFilter" : {
          "filter" : {
            "bool" : {
              "must" : {
                "term" : {
                  "hops.adapterName" : "myadapter"
                }
              }
            }
          }
        },
        "average0" : {
          "avg" : {
            "field" : "serverProcessingTime"
          }
        },
        "average1" : {
          "avg" : {
            "field" : "roundTripTime"
          }
        },
        "average2" : {
          "avg" : {
            "field" : "bytesReceived"
          }
        }
      }
    }
  }
}

```

And this the result I get:

```
{
  "hits": {
    "total": 2,
    "hits": [],
    "max_score": 0
  },
  "_shards": {
    "total": 1,
    "failed": 0,
    "successful": 1
  },
  "timed_out": false,
  "took": 182,
  "aggregations": {
    "nested": {
      "aggregationFilter": {
        "doc_count": 1
      },
      "average0": {
        "value": 35
      },
      "doc_count": 2,
      "average2": {
        "value": null
      },
      "average1": {
        "value": 100
      }
    }
  }
}

```

It looks like the filter was applied (doc\_count = 1), but the results of that weren't applied to the average aggregation (doc\_count = 2). I think I need to structure the query slightly differently, but can't figure out how, spent a lot of time with trial and error on this, and no success.

Hoping the community can offer advice.

Many thanks

---

<div class="post-metadata">

**Author:** ![remram](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/remram/32/4077_2.png) [@remram](https://discuss.elastic.co/u/remram)\
**Post date:** [September 14, 2015, 6:22am UTC](https://discuss.elastic.co/t/how-to-combine-a-filter-and-and-a-nested-aggregation/29149/2 "2015-09-14T06:22:13Z")

</div>

Try to put your filter part in the query part and not inside the aggregation.  
Read about scoping here: [https://www.elastic.co/guide/en/elasticsearch/guide/current/\_scoping\_aggregations.html](https://www.elastic.co/guide/en/elasticsearch/guide/current/_scoping_aggregations.html)  
Cheers, Ramy

---

<div class="post-metadata">

**Author:** ![vhj](https://avatars.discourse-cdn.com/v4/letter/v/f14d63/32.png) [@vhj](https://discuss.elastic.co/u/vhj)\
**Post date:** [September 14, 2015, 9:49am UTC](https://discuss.elastic.co/t/how-to-combine-a-filter-and-and-a-nested-aggregation/29149/3 "2015-09-14T09:49:53Z")

</div>

Thanks, I tried using a query and it works!

```
{
  "query" : {
    "nested" : {
      "filter" : {
        "bool" : {
          "must" : {
            "term" : {
              "hops.adapterName" : "myadapter"
            }
          }
        }
      },
      "path" : "hops"
    }
  },
  "aggregations" : {
    "nested" : {
      "nested" : {
        "path" : "hops"
      },
      "aggregations" : {
        "average0" : {
          "avg" : {
            "field" : "serverProcessingTime"
          }
        },
        "average1" : {
          "avg" : {
            "field" : "roundTripTime"
          }
        },
        "average2" : {
          "avg" : {
            "field" : "bytesReceived"
          }
        }
      }
    }
  }
}
```

---

<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, 11:50pm UTC](https://discuss.elastic.co/t/how-to-combine-a-filter-and-and-a-nested-aggregation/29149/4 "2017-07-05T23:50:31Z")

</div>


