How to get minimum and maximum price in Solr?

How to get minimum and maximum price in Solr?

Showing a price range selector for visitor to narrow down search is a common use case for eCommerce sites. Usually developers try to use facets for this purpose which is not a correct way to do it. Facts for price should only be used if we need to break prices in to multiple ranges for selection like 1 to 100, 101 to 500 and 501 to more.

For getting just the minimum and maximum price one must use the “stats” feature which is enabled by default in Solr. This can be achieved by providing additional parameters “ stats=true&stats.field=price ” in request. Here the “price” is the numeric field in the index, you can change it as per your index/core.

So the response from sample request like localhost:8983/solr/products/select?q=*:*&stats=true&stats.field=price&indent=true&wt=json response will include information for stats as

Here you can see the “min” and “max” values along other stats like “sum”, “mean” etc. Full documentation for this feature can be found here.

By | 2017-04-17T13:21:45+00:00 April 17th, 2017|0 Comments

About the Author:

Waseem is our consultant for Sphinx Search, Elastic and Solr. He is lamp stack backend developer and has 14 years of experience of development. His areas of interest (other than search) are NoSQL and BigData tools.