How to apply aggregations to collapsed/grouped results?

In the documentation for field collapsing it states: The collapsing is applied to the top hits only and does not affect aggregations.

My question:

On my eCommerce store I want to only include the first item in each group (grouped by item_id) in the final results. At the same time I don't want to lose my aggregations (little numbers next to attributes that indicate how many items with that attribute are found).

Here is a little example:

Suppose I make a search for items and only 25 show up. This is the result for the color aggregation that I currently get:

  • black (65)
  • green (32)
  • white (13)

And I want it to be:

  • black (14)
  • green (6)
  • white (5)

The numbers should amount to the total number (25) the user actually sees on the page.

How could I achieve that with Elasticsearch? I have tried both Grouping (Top Hits) and Field Collapsing and both don't seem to fit my use case. Solr does it almost by default with its Grouping functionality.

1 Like

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