0
votes

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?

1

1 Answers

0
votes

What you're looking for is the Top hits aggregation: https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-metrics-top-hits-aggregation.html

Your current terms aggregation will get you the relevant categories and doc counts. Then you would use the top hits aggregation as a sub-agg to that and it will give you matching hits for your query sorted by score(default), within your categorical buckets.