# Manipulating scope in an elasticsearch aggregation?

**URL:** <https://discuss.elastic.co/t/manipulating-scope-in-an-elasticsearch-aggregation/153616>\
**Category:** Elasticsearch\
**Created:** [October 23, 2018, 1:40pm UTC](https://discuss.elastic.co/t/manipulating-scope-in-an-elasticsearch-aggregation/153616 "2018-10-23T13:40:20Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Elizabeth\_Haubert](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/elizabeth_haubert/32/34355_2.png) [@Elizabeth\_Haubert](https://discuss.elastic.co/u/Elizabeth_Haubert)\
**Post date:** [October 23, 2018, 1:40pm UTC](https://discuss.elastic.co/t/manipulating-scope-in-an-elasticsearch-aggregation/153616/1 "2018-10-23T13:40:21Z")

</div>

Hello,

Running into something with the implementation of a multi-select check box and aggregations. The use  
case is:

- User makes a text-based query
- UX has a list of categories, of which the user may select one or more to filter the results of subsequent queries.
- Ideally, the categories displayed would be all categories matching the text-based query, but not constrained by which values have been selected for filtering.

As I understand it, the aggregation bucket domain is either the search context, or it is the global context, potentially with additional filters applied in either case. Is there a corresponding aggregation to say that it is the search context, but without certain filters applied?

More concretely, say we have a food index, with fruit: apples, bananas, plantains, and grapes. and dairy: cheese, milk, yogurt.

I'd like an aggregation that has counts for fruit:apples, bananas, plantains, and grapes, while allowing users to browse just the apples and bananas.

```
{
	"query": {
		"bool": {
			"must": {
				"term": {
					"food_group": "fruit"
				}
			},
			"filter": {
				"terms": {
					"food_name": ["apple", "banana"]
				}
			}
		},
		"aggs": {
			"kinds_o_fruit": {
				"terms": {
					"field": "food_name"
				}
			}
		}
	}
}

```

As-is, "kinds\_o\_fruit" will return all foods with food\_group: fruit, and food\_name apple or banana. If food\_name is a multifield, then there might be additional values - apples, bananas, and plantains would make sense here.

As I understand it, the only way to get an aggregation that has "kinds\_o\_fruit" with all kinds of fruit would be to nest it in a global aggregation with the original match query as a filter:

```
{
	"query": {
		"bool": {
			"must": {
				"term": {
					"food_group": "fruit"
				}
			},
			"filter": {
				"terms": {
					"food_name": ["apple", "banana"]
				}
			}
		},
		"aggs": {
			"all_food": {
				"global": {},
				"aggs": {
					"kinds_o_fruit": {
						"filter": {
							"term": {
								"food_group": "fruit"
							}
						},
						"aggs": {
							"title_filtered": {
								"terms": {
									"field": "title.raw"
								}
							}
						}
					}
				}
			}
		}
	}
}

```

As long as the initial text query is a simple "terms" query, this is not unreasonable. But if the initial query is something more elaborate, then the performance and maintenance complexity could quickly get out of hand.

Is there a way perhaps with named queries to specify the context as documents associated with the initial "must" clause? Ideally, I'd like documents in the query domain, but without named filters applied.

---

<div class="post-metadata">

**Author:** ![polyfractal](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/polyfractal/32/48162_2.png) [@polyfractal](https://discuss.elastic.co/u/polyfractal)\
**Post date:** [October 29, 2018, 11:54am UTC](https://discuss.elastic.co/t/manipulating-scope-in-an-elasticsearch-aggregation/153616/2 "2018-10-29T11:54:22Z")

</div>

Still working through the examples, but I think this is a case where the [post\_filter](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-post-filter.html) will help.

The post\_filter is a filtering operation that is performed after the aggregation has been computed. So that would allow your aggregation to show `apples, bananas, plantains, grapes` (aggregating on all the fruit category basically), then a `post_filter` is applied which further restricts the search hits down to `apples, bananas`. The post\_filter is only applied to search hits, not aggregations, so it can be used as a final "phase" for filtering what the user sees without touching the aggs.

I _think_ that will help achieve what you're looking for, and keep the query/aggs reasonably simple. Lemme know if that helps! 🙂

---

<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:** [November 26, 2018, 11:56am UTC](https://discuss.elastic.co/t/manipulating-scope-in-an-elasticsearch-aggregation/153616/3 "2018-11-26T11:56:13Z")

</div>

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