Get relevance score from aggs bucket

Give a query like the one below, how can I get the max relevance score from each bucket?

GET /omni_filters/_search/
{
  "aggs" : {
        "uniqueHits" : {
          "terms" : {
          "field" : "command"
        }
      }
    },
  "query" : {
    "bool": {
      "must": [
        {
          "match": {
      "request": "requests ? opened"
    }
        }
      ],
      "should" : [{
        "term" : {
          "request" : "customer"
        }
      }]
    }
  }
}

The goal is to create a search bar that can take an input like this: "All the requests I opened yesterday" and return a list of relevant elasticsearch queries. I do this by indexing a large number of requests and their corresponding elasticsearch commands, or similar DSL that can be translated to elasticsearch. The only problem is that I may have many more ways to say one command than to say another, so I need to group the results by the command term, and then get the most relevant score from each bucket.

Is that possible?

Found the answer, the top_hits sub-aggregate does this exactly, so to get the top hit from each bucket I would add the following under my uniqueHits bucket

"aggs": {
    "topHit" : {
        "top_hits" : {
            "size" : 1
        }
    }
}

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