I am trying to figure out a solution for the following scenario.
User enters a search term (say iPhone). A query to fetch the aggregations on categories is executed ordered by count desc. This is to know how many products each category has for the given search term. Once I get this, I need to execute the actual search query for 'iphone' to get the list of matching products but I also need to boost the category values received in my earlier aggregation query.
e.g. 1st aggregation query
GET /product/_search
{
"size" : 0,
"query" : {
"multi_match" : {
"query" : "iphone",
"fields" : [
"identity.name"
],
"operator" : "and",
"type": "most_fields"
}
},
"aggs" : {
"categories": {
"terms": {
"field": "description.categoryName.keyword" ,
"size": 3
}
}
}
}
RESPONSE
"aggregations": {
"categories": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 43,
"buckets": [
{
"key": "iPhones",
"doc_count": 55
},
{
"key": "iPhone 6 & 6S Cases & Screen Protectors",
"doc_count": 38
},
{
"key": "iPhone 7 Cases & Screen Protectors",
"doc_count": 38
}
]
}
}
So now my 2nd query for getting products matching 'iphone' should have the highest boost/weight for category=iPhones followed by category=iPhone 6 & 6S Cases & Screen Protectors and then category=iPhone 7 Cases & Screen Protectors and then the rest of the results
How can I achieve this?