# Get relevance score from aggs bucket

**URL:** <https://discuss.elastic.co/t/get-relevance-score-from-aggs-bucket/174870>\
**Category:** Elasticsearch\
**Created:** [April 1, 2019, 8:27pm UTC](https://discuss.elastic.co/t/get-relevance-score-from-aggs-bucket/174870 "2019-04-01T20:27:52Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![richbai90](https://avatars.discourse-cdn.com/v4/letter/r/a3d4f5/32.png) [@richbai90](https://discuss.elastic.co/u/richbai90)\
**Post date:** [April 1, 2019, 8:27pm UTC](https://discuss.elastic.co/t/get-relevance-score-from-aggs-bucket/174870/1 "2019-04-01T20:27:52Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![richbai90](https://avatars.discourse-cdn.com/v4/letter/r/a3d4f5/32.png) [@richbai90](https://discuss.elastic.co/u/richbai90)\
**Post date:** [April 2, 2019, 3:20pm UTC](https://discuss.elastic.co/t/get-relevance-score-from-aggs-bucket/174870/2 "2019-04-02T15:20:52Z")

</div>

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
        }
    }
}
```

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [April 30, 2019, 3:21pm UTC](https://discuss.elastic.co/t/get-relevance-score-from-aggs-bucket/174870/3 "2019-04-30T15:21:00Z")

</div>

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