Calculate result count with selectable filters


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 1 (40)
  • city 2 (40)


  • 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?

(Mark Harwood) #2

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:

	must : [
		query-string : [user input],
			min_should_match: 2
			should: [
						  should: [
								 color: blue,
						  should: [
								 size: L
						  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.


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:

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.

(Mark Harwood) #4

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

(system) #5

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