Elasticsearch sort terms/top_hits

时间:2016-08-31 17:34:44

标签: sorting elasticsearch

I'm trying to use terms/top_hits aggregations to get best results for each category and then sort them. Currently, my query looks like this:

{
  "aggs": {
    "by_category": {
      "terms": {
        "field": "category_id",
        "size": 3
      },
      "aggs": {
        "top": {
          "top_hits": {
            "size": 1,
            "sort": [
              {
                "rating": "desc"
              }
            ]
          }
        }
      }
    }
  }
}

It works well and documents within each category_id are sorted.

Now, I need to sort those selected hits by other criteria, i.e.

"sort": [
  {
    "price": "asc"
  }
]

At the end I need a hit within each category with the best rating and then I want to sort those best hits by price.

But I don't know where to put this sorting. On the top level it doesn't sort buckets. Inside by_category (terms aggregation) it doesn't work. Inside top_hits I need another sorting.

I saw the solution with _score, but I need sorting by multiple fields.

How to do that? Thanks.

0 个答案:

没有答案