How to combine a filter AND and a nested aggregation?

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

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
Cheers, Ramy

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"
          }
        }
      }
    }
  }
}