# Can I Scope Aggregations to Ignore Certain Query Parameters?

**URL:** <https://discuss.elastic.co/t/can-i-scope-aggregations-to-ignore-certain-query-parameters/114984>\
**Category:** Elasticsearch\
**Created:** [January 11, 2018, 12:29am UTC](https://discuss.elastic.co/t/can-i-scope-aggregations-to-ignore-certain-query-parameters/114984 "2018-01-11T00:29:58Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![wgottschalk](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/wgottschalk/32/26457_2.png) [@wgottschalk](https://discuss.elastic.co/u/wgottschalk)\
**Post date:** [January 11, 2018, 12:29am UTC](https://discuss.elastic.co/t/can-i-scope-aggregations-to-ignore-certain-query-parameters/114984/1 "2018-01-11T00:29:58Z")

</div>

I'm using Elasticsearch 5.5  
I'm trying to index movies by name and location.

```
{
  "mappings": {
    "movie": {
      "genre": {
        "type": "keyword"
      },
      "geolocation": {
        "type": "geo_point"
      }
    }
  }
}

```

A user performs a search by providing a location and I'll send back the search results within a bounding box and also provided aggregation stats about the genres playing in their neighborhood.

However, I also want to provide a way to filter by location and genre BUT I still want to return ALL genres in that particular location.

The below query returns the proper search results but the aggregations will only return "Action", but I still want all genres.

```
{
  "size": 0,
  "query": {
    "bool": {
      "filter": [{
        "geo_bounding_box": {
          "geolocation": {
            "top_left": {
              "lat": 40.73,
              "lon": -74.1
            },
            "bottom_right": {
              "lat": 40.717,
              "lon": -73.99
            }
          }
        }
      },
      {
        "bool": {
          "should": [
            {"term": {"genre.raw": "Action"}}  
          ]
        }
      }
      ]
    }
  },
  "aggs": {
    "cuisines": {
      "terms": {
        "field": "cuisines.raw",
        "size": 10
      }
    }
  }
}

```

I've considered using Global Aggregations, but that will return all aggregations of genres in my entire index and not a specific bounding box. Is there a way to scope my aggregation to ignore certain query parameters?

---

<div class="post-metadata">

**Author:** ![val](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/val/32/138203_2.png) [@val](https://discuss.elastic.co/u/val)\
**Post date:** [January 11, 2018, 4:15am UTC](https://discuss.elastic.co/t/can-i-scope-aggregations-to-ignore-certain-query-parameters/114984/2 "2018-01-11T04:15:27Z")

</div>

This is a good use case to leverage [post filter](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-post-filter.html). you just need to change your query to:

```
{
  "size": 0,
  "query": {
    "bool": {
      "filter": [
        {
          "geo_bounding_box": {
            "geolocation": {
              "top_left": {
                "lat": 40.73,
                "lon": -74.1
              },
              "bottom_right": {
                "lat": 40.717,
                "lon": -73.99
              }
            }
          }
        }
      ]
    }
  },
  "post_filter": {
    "term": {
      "genre.raw": "Action"
    }
  },
  "aggs": {
    "cuisines": {
      "terms": {
        "field": "cuisines.raw",
        "size": 10
      }
    }
  }
}

```

This will return results within the given bounding box, compute aggregations on that result set and then filter the returned documents to only the ones with the given genre.

---

<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:** [February 8, 2018, 4:15am UTC](https://discuss.elastic.co/t/can-i-scope-aggregations-to-ignore-certain-query-parameters/114984/3 "2018-02-08T04:15:46Z")

</div>

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