Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Azure Search: price range - min & max value calculation

Currently, I am trying out Azure Search SDK. Having a strong background working with lucene and bobobrowse, the Azure Search is quite fantastic and has a lot of features from both frameworks out of the box.

The only thing I am puzzling with is to get the minimum and maximum value of a numeric facet item. By intention, I do not want to use the interval parameter nor the value lists:

Azure Facet Navigation

My requirement is to display the price facet with a calculated minimum and maximum value. Following website has such a facet in their facet list:

Price Range

In my existing desktop application (.Net) I successfully used BoboBrowse framework and implemented a Custom-FacetHandler to get the desired results which are shown in the following picture below:

MultiValue Facet Handler Result

Never mind the facet values within these pictures. These are just length, height and other characteristic values of tools.

This is how I build one document for demonstration purposes. There is a price field which is generated dynamically. Because of azure search needs a strongly fixed schema for every index. I update the schema within the indexing process. This works very well.

Price Field

So the question is how can i achieve the required functionality with azure search?

In the elastic search, such a problem could be solved by using Aggregations. Does this feature exist in Azure Search?

like image 485
Sneijder Avatar asked Sep 20 '17 17:09

Sneijder


1 Answers

The $filter parameter supports the lt (less than) and gt (greater than) operators. For example: $filter=price gt 0 and price lt < 100

Sample code:

string PriceFilter(double? fromPrice, double? toPrice)
{
   var filter = "$filter=";
   if(fromPrice.HasValue) 
          filter+="price gt " + fromPrice.Value.ToString() 
                + (toPrice.HasValue ? " and " : "");
   if(toPrice.HasValue) 
          filter+="price lt " + toPrice.Value.ToString();
   return filter;
}

Reference: Build a filter for a range

like image 79
Diego Avatar answered Sep 16 '22 15:09

Diego