Reputation: 1110
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
Reputation: 1110
I figured out another option as work around. The idea is to sort the facet by value.
Take price as example:
"fields": [
{ "name": "price", "type": "Edm.Double", "facetable": true }
{ "name": "price_copy", "type": "Edm.Double", "facetable": true}
]
"value": [
{
"@search.action": "upload",
"price": 19.99,
"price_copy": 19.99
}]
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
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