# Calculate result count with selectable filters

**URL:** https://discuss.elastic.co/t/calculate-result-count-with-selectable-filters/98208
**Category:** Elasticsearch
**Created:** [August 24, 2017, 10:27am UTC](https://discuss.elastic.co/t/calculate-result-count-with-selectable-filters/98208 "2017-08-24T10:27:30Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![habtim](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/habtim/32/11895_2.png) [@habtim](https://discuss.elastic.co/u/habtim)
#### Post date: [August 24, 2017, 10:27am UTC](https://discuss.elastic.co/t/calculate-result-count-with-selectable-filters/98208/1 "2017-08-24T10:27:30Z")

</div>

Hi everyone,

What im currently trying to get to work is the following:

We have an results page with on the left some areas with selectable terms for filtering, for example:

search query = test  
total results = 80

City

- city 1 (40)
- city 2 (40)

Type

- post (20)
- page (60)

But when at least one of the filters is active, i want to calculate per filter what the new result count will be.

Anyone knows how to do this?

---

<div class="post-metadata">

### Author: ![Mark\_Harwood](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mark_harwood/32/10538_2.png) [@Mark\_Harwood](https://discuss.elastic.co/u/Mark_Harwood)
#### Post date: [August 24, 2017, 12:32pm UTC](https://discuss.elastic.co/t/calculate-result-count-with-selectable-filters/98208/2 "2017-08-24T12:32:09Z")

</div>

If I understand the problem correctly this is what I call the "disappearing checkboxes" problem.

When you have multiple filtering dimensions to contend with e.g. brand, size and colour my advice is to group user selections into separate bool clauses as follows:

```
bool
	must : [
		query-string : [user input],
		bool
			min_should_match: 2
			should: [
				  bool:
						  should: [
								 color: blue,
								 color:green
				  bool:
						  should: [
								 size: L
				  bool:
						  should: [
								 brand: X,
								 brand: Y
			 ]
  ]

```

The min\_should\_match clause should be one less than the number of dimensions (in this case size/colour/brand). This ensures matches that hit a dimension in the agg tree always satisfy all other category selections.

While this fixes the aggregation results it does mean that hits may contain docs that match only 2 out of 3 top-level filter clauses. The top results are likely to be the ones that tick all the boxes but you can see the problem. To fix that you'd need to add a `post_filter` for the hits that insists on all 3 out of 3 filter clauses matching.

This is a general pattern that should work regardless of how many filtering dimensions you have.

---

<div class="post-metadata">

### Author: ![habtim](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/habtim/32/11895_2.png) [@habtim](https://discuss.elastic.co/u/habtim)
#### Post date: [August 24, 2017, 2:32pm UTC](https://discuss.elastic.co/t/calculate-result-count-with-selectable-filters/98208/3 "2017-08-24T14:32:23Z")

</div>

I'am not sure if this is what I mean, I might not explained it very well.

I will show you by live current situation: [https://kiesuwcursus.nl/juridisch/?s=&rechtsgebied=5](https://kiesuwcursus.nl/juridisch/?s=&rechtsgebied=5)

Now the second filter section you see: Amsterdam (165), but when you click it it actually is (28) because the it has to calculate in combination with the first selected filter.

Current situation: all terms show the amount of counts they are in posts/pages  
Current situation: all terms should show the amount of counts they are in posts/pages, but should accumulate the total count with other active filters.

So what I'm trying to achieve is show the user the results which will be shown if the clickable filter will be activated.

Thank you for helping me.

---

<div class="post-metadata">

### Author: ![Mark\_Harwood](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mark_harwood/32/10538_2.png) [@Mark\_Harwood](https://discuss.elastic.co/u/Mark_Harwood)
#### Post date: [August 24, 2017, 2:43pm UTC](https://discuss.elastic.co/t/calculate-result-count-with-selectable-filters/98208/4 "2017-08-24T14:43:57Z")

</div>

So to check the logic:

- You have multiple facets (for want of a better word). e.g. City, type etc
- Selections within a facet e,g City are multi-choice and logically ORed.
- Selections across facets are ANDed so in Lucene-speak `(type:1 OR type:2) AND (city:A OR city:B)`

If that is true then the recipe I provided is how to show correct counts for the choices inside each facet.  
The counts for city A and city B will be regardless of any current selections in the city facet and respectful of matching all other selections in all other facets e.g. types 1 or 2. The converse is true for the type counts which will be irrespective of any type selections and respectful of all other facet choices e.g city A or B

---

<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: [September 21, 2017, 2:44pm UTC](https://discuss.elastic.co/t/calculate-result-count-with-selectable-filters/98208/5 "2017-09-21T14:44:42Z")

</div>

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