Range facets with count

Good morning.

I’m using ruby with algolia rails integration. We’re migrating from ElasticSearch.
Is there a range facet in algolia ? I would love to get statistics based on price ranges.
In ES there is something like that:

{
    "aggs" : {
        "price_ranges" : {
            "range" : {
                "field" : "price",
                "ranges" : [
                    { "to" : 100.0 },
                    { "from" : 100.0, "to" : 200.0 },
                    { "from" : 200.0 }
                ]
            }
        }
    }
}

And for each range they return the amount of documents.

Hi there!

It’s possible to filter the results based on the price range. We have a section in the documentation explaining how to filter on price range, with a snippet in ruby as an example. One way you could get the amount of documents per desired price range is to perform a search for the price range you want. The response will contain the amount of records.

Let me know if this works for you!

Cheers, Devin.

Hi !
Please correct me if I’m wrong.
If I would like to get doc count for each range I mention below I should do 9 requests with each range ?

ranges: [
  { from: 1, to: 1_000_000 },
  { from: 1_000_000, to: 2_000_000 },
  { from: 2_000_000, to: 3_000_000 },
  { from: 3_000_000, to: 5_000_000 },
  { from: 5_000_000, to: 7_500_000 },
  { from: 7_500_000, to: 10_000_000 },
  { from: 10_000_000, to: 15_000_000 },
  { from: 15_000_000, to: 20_000_000 },
  { from: 20_000_000 }
]

Hi!

That’s correct, you would have to do a request for each of the ranges you want to fetch the counts for.

You could prevent adding these extra calls by adding another attribute to your records specifying to which group it belongs. For example, you could add a price_range_group attribute to each of your records, and define it as an attribute for faceting.

Cheers!

1 Like