Top hits aggregation performance issue

Hi!
i'm trying to aggregate my query by the field "id_b2w" and return only the item with the highest score for each bucket ( buckets are ordered by max score). My query is working, but it is slow. Below is the query I'm using, is it the best way to do it?

  "aggs": {
    "top_hits": {
      "terms": {
        "field": "id_b2w",
        "size": "20",
        "order": {
          "max_score": "desc"
        }
      },
      "aggs": {
        "by_top_hit": {
          "top_hits": {
            "size": "1"
          }
        },
        "max_score": {
          "max": {
            "lang": "expression",
            "script": "_score"
          }
        }
      }
    }
  }

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.