Aggregation: How to get the empty buckets

Enonic version: 6.8.0
OS: Linux

Since I’m using the aggregates as choices in a html select. I would also like to have the empty buckets listed. Is that possible?

Not seeing any options for it here:

Empty buckets in terms-aggregation? Empty as in is in query-result but filtered?

This is probably only a problem when multiple aggrgates/filters are involved.

So I guess the problem is that the query that gives me the aggregates is filtered.

So I gotta do two queries. A non filtered to get all aggregate values (groups) there is.

Then a filtered one, to get how many entries are remaining in the buckets when they are filtered.
Then I can check which buckets where in the first query which are no longer in the second query.
And add them with 0 entries to the list of buckets.


The data:

Field 1 | Field 2
A       | C          
A       | C
A       | D
B       | E

Lets filter on ‘Field 1’=‘A’

That will give me these aggregates for Field 2:
C (2)
D (1)

What I would like to achive is:
C (2)
D (1)
E (0)

1 Like

I think your solution with two queries is the best solution. It will be illogical to return the buckets that does not match the query. There is no optimization gain either, since the full dataset will have to be considered to get all buckets anyway.

The solution might actually be easy, at least if the field is not an array. Just sum up the count from the buckets and deduct this number from the total hit count, that should be your “empty” buckets.

The sum of the buckets will always match the count…

Anyways it’s much more complicated as there are currently 4 filtered and aggregated fields.
There may be an arbitrary number of filtered and aggregated fields in the future.

Working on something else currently so I’m not deep into it, but I think I will end up with:

  • One actual query to get the filtered data I want to show
  • One query per filter, removing only that filter to make it possible to show count and switch within one filter field.

So that if we have 4 fields: 1,2,3,4
And the query is filtered by all four fields.
And I want to see how many entries there are if I choose something else in Field 4.
I need to do a query with filters 1,2,3 to see the counts for the other aggregates for Field 4.

I will probably no longer need:

  • One query without any filters to get all buckets (groups)

So that would be 5 queries with the following filters enabled:

  • 1,2,3,4 (all filters enabled, the actual data to show)
  • 2,3,4 (without filter for field 1)
  • 1,3,4 (without filter for field 2)
  • 1,2,4 (without filter for field 3)
  • 1,2,3 (without filter for field 4)