# Return the top k hits from each bucket after aggregation?

**URL:** <https://discuss.elastic.co/t/return-the-top-k-hits-from-each-bucket-after-aggregation/262687>\
**Category:** Elasticsearch\
**Created:** [January 29, 2021, 8:41pm UTC](https://discuss.elastic.co/t/return-the-top-k-hits-from-each-bucket-after-aggregation/262687 "2021-01-29T20:41:30Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![sabetai](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/sabetai/32/82900_2.png) [@sabetai](https://discuss.elastic.co/u/sabetai)\
**Post date:** [January 29, 2021, 8:41pm UTC](https://discuss.elastic.co/t/return-the-top-k-hits-from-each-bucket-after-aggregation/262687/1 "2021-01-29T20:41:30Z")

</div>

I'd like the top k hits from each index to be returned, I'm using the following query:

```auto
GET /index-*/_search
{
  "query": {
    "multi_match": {
      "query": "word1 word2",
      "type": "phrase",
      "fields": ["field1", "field2]
    }
  },
  "aggs": {
    "byindex": {
      "terms": {
        "field": "_index",
        "size" : 3
      }
    },
    "aggs": {
      "top_hits": {
        "size": 1
      }
    }
  }
} 

```

similar to [this thread](https://discuss.elastic.co/t/query-top-5-from-multiple-indices/85777/2), but I'm given the top hit from **all** buckets rather than from **each** bucket. What's wrong with my query?

---

<div class="post-metadata">

**Author:** ![mayya](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mayya/32/83147_2.png) [@mayya](https://discuss.elastic.co/u/mayya)\
**Post date:** [January 29, 2021, 11:44pm UTC](https://discuss.elastic.co/t/return-the-top-k-hits-from-each-bucket-after-aggregation/262687/2 "2021-01-29T23:44:21Z")

</div>

For this, your `top_hits ` agg should be under `byindex` agg like this:

```auto
 "aggs": {
    "byindex": {
      "terms": {
        "field": "_index",
        "size": 3
      },
      "aggs": {
        "my_top_hits": {
          "top_hits": {
            "size": 1
          }
        }
      }
    }
  }

```

---

<div class="post-metadata">

**Author:** ![sabetai](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/sabetai/32/82900_2.png) [@sabetai](https://discuss.elastic.co/u/sabetai)\
**Post date:** [January 30, 2021, 12:25am UTC](https://discuss.elastic.co/t/return-the-top-k-hits-from-each-bucket-after-aggregation/262687/3 "2021-01-30T00:25:17Z")

</div>

Makes sense for it to be nested inside. Works, thanks!!!

---

<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:** [February 27, 2021, 12:25am UTC](https://discuss.elastic.co/t/return-the-top-k-hits-from-each-bucket-after-aggregation/262687/4 "2021-02-27T00:25:25Z")

</div>

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