# Java REST API - No way to perform filters on aggregations?

**URL:** <https://discuss.elastic.co/t/java-rest-api-no-way-to-perform-filters-on-aggregations/141658>\
**Category:** Elasticsearch\
**Created:** [July 25, 2018, 8:01pm UTC](https://discuss.elastic.co/t/java-rest-api-no-way-to-perform-filters-on-aggregations/141658 "2018-07-25T20:01:19Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![rarestg](https://avatars.discourse-cdn.com/v4/letter/r/c6cbf5/32.png) [@rarestg](https://discuss.elastic.co/u/rarestg)\
**Post date:** [July 25, 2018, 8:01pm UTC](https://discuss.elastic.co/t/java-rest-api-no-way-to-perform-filters-on-aggregations/141658/1 "2018-07-25T20:01:19Z")

</div>

I'm trying to do two things:

- Filter results with a geo-bounding box
- Filter results within a timeframe
- Query for an aggregation, the average of a certain data field

I've been able to do this with the following POST:

```
  curl -XGET "http://localhost:9200/data/_search" -H 'Content-Type: application/json' -d'
  {
    "size": 0,
    "_source": {
      "excludes": []
    },
    "aggs": {
      "filter_agg": {
        "filter": {
          "geo_bounding_box": {
            "place_lat": {
              "top_left": {
                "lat": 37.47
                "lon": -122.41
              },
              "bottom_right": {
                "lat": 37.27,
                "lon": -121.72
              }
            }
          }
        },
        "aggs": {
          "2": {
            "geohash_grid": {
              "field": "place_lat",
              "precision": 6
            },
            "aggs": {
              "1": {
                "avg": {
                  "field": "coordinates"
                }
              },
              "3": {
                "geo_centroid": {
                  "field": "coordinates"
                }
              }
            }
          }
        }
      }
    },
    "stored_fields": [
      "*"
    ],
    "script_fields": {},
    "docvalue_fields": [
      "begin_time",
      "dropoff_time",
      "request_time"
    ],
    "query": {
      "bool": {
        "must": [
          {
            "range": {
              "request_time": {
                "gte": 1530427399999,
                "lte": 1530428399999,
                "format": "epoch_millis"
              }
            }
          }
        ],
        "filter": [
          {
            "match_all": {}
          }
        ],
        "should": [],
        "must_not": []
      }
    }
  }'

```

But I can't seem to do this using the Java High-Level REST Api.

I can create a `geohashGrid` aggregation, and sub-aggregation based on whatever field I'm interested in. However I'm not sure how to add the two filters. The following hangs on `client.search()`:

```
    // Build the bounding box filter
    GeoBoundingBoxQueryBuilder queryBuilder = QueryBuilders
            .geoBoundingBoxQuery("bounding_box")
            .setCorners(topLeft, bottomRight);

    // Aggregate on a field
    GeoGridAggregationBuilder aggregation = AggregationBuilders
            .geohashGrid("by_region")
            .field("coordinates")
            .precision(6);
    aggregation.subAggregation(AggregationBuilders
            .avg("my_data")
            .field("my_data"));

```

Help is absolutely appreciated. Thank you!

---

<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:** [August 22, 2018, 8:01pm UTC](https://discuss.elastic.co/t/java-rest-api-no-way-to-perform-filters-on-aggregations/141658/2 "2018-08-22T20:01:29Z")

</div>

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