Using Distinct and displaying the lowest price of the grouped results

Hi there,

We have loads of variants for our products and we are using distinct on a custom key to display only one and then let the user configure it afterwoods on the product page.

Each of these variants can carry a different price depending on the options.
Using distinct we are able to display the right variation of the product in the search results.

But we would like to display the starting price as the price range for the variations can be quite high and discouraging.

Product A
price: 100
color: red
key: prod_A

Product B
price: 1000
color: green
key: prod_A

Product C
price: 500
color: green
key: prod_A

Currently, on search result page we have:
Product A - $ 100

If we filter on color using green value, we have:
Product B - $ 1000

What we would like to have:
Product A - starting from $ 100

Using green filter:
Product B - starting from $ 500

Is there a way to get the min value of a numeric value from a distinct result set?
From an SQL point of view I would write it:
SELECT name, min(price) from products group by key;

Hmmmm… there’s no equivalent to min in with Algolia indices. It would make more sense to pre-calculate the minimum price and inject in as an additional attribute in your records.

Alternately you could calculate it on the fly in as you’re painting the results, but that seems like a lot of overhead in the browser.

I wonder if there’s something you could do running the query against a replica that’s ranked with the price in ascending order, so you can just grab the first record?

Hi Chuck, this would definitly work, using 2 queries per page instead of one but achieving the goal.
Thank you for this clever idea

1 Like