Youxu
Youxu

Reputation: 1110

How to implement min max facet (similar to stats aggregation in Elasticsearch) in azure search?

Our index has price field, and I want to dynamically get the min/max price of all products for a given search phrase. This feature could be implemented with stats aggregation in Elasticsearch, but how to implement it using Azure search facet?

Upvotes: 0

Views: 789

Answers (3)

Youxu
Youxu

Reputation: 1110

I figured out another option as work around. The idea is to sort the facet by value.

Take price as example:

  1. Define two fields for price in index when creating the index
    "fields": [
        { "name": "price", "type": "Edm.Double", "facetable": true }
        { "name": "price_copy", "type": "Edm.Double", "facetable": true}
    ]
    
  2. Put same value to both price and price_copy field when upload the documents
  "value": [  
    {  
      "@search.action": "upload",  
      "price": 19.99,
      "price_copy": 19.99
    }]
  1. Send facet request
    facet=price,sort:value,count:1&facet=price_copy,sort:-value,count:1

The response will be something like this:

"@search.facets": {
    "price": [
        {
            "count": 45,
            "value": 0
        }
    ],
    "price_copy": [
        {
            "count": 20,
            "value": 2899.99
        }
    ]
}

Then we can pick up the min/max value of the price from price and price_copy facet respectively. I think this solution is better, as it could get the more accurate min/max value compared with the bucket range workaround as Liam suggested.

Any thought?

Upvotes: 2

Liam Cavanagh - MSFT
Liam Cavanagh - MSFT

Reputation: 1464

Fully agree with Joel as the correct answer, however another option would be to facet over the price and get all the potential prices and then on client side take the min and max. Obviously that would not be nearly as performant as Joel's option and might be unrealistic if you have a significant number of different prices. In that case, I think you could do bucket ranges of prices (for example count of $0-$10, $10-20, etc...) to find the max and min bucket price range which would decrease the number of facets that would come back.

Upvotes: 0

Joey Cai
Joey Cai

Reputation: 20127

Azure Search does support range facets. What's not included is min/max values in responses.

You could vote up this feedback to promote the feature that expose functionality equivalent to Elasticsearch aggregations to be achieved.

Upvotes: 1

Related Questions