# Get top 10 data for each group

**URL:** https://discuss.elastic.co/t/get-top-10-data-for-each-group/327349
**Category:** Elasticsearch
**Tags:** kql-kibana-query-language, eql-elastic-query-language
**Created:** [March 9, 2023, 11:25am UTC](https://discuss.elastic.co/t/get-top-10-data-for-each-group/327349 "2023-03-09T11:25:39Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![Shishir\_Kumar2](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/shishir_kumar2/32/118251_2.png) [@Shishir\_Kumar2](https://discuss.elastic.co/u/Shishir_Kumar2)
#### Post date: [March 9, 2023, 11:25am UTC](https://discuss.elastic.co/t/get-top-10-data-for-each-group/327349/1 "2023-03-09T11:25:39Z")

</div>

Requirement is to get top 10 data for each group. I created below index and tried using few combination of aggregation query but it does not get desired result.

Index Definition

```auto
PUT /poc_agg
{
  "settings": {
    "number_of_shards": 1
  },
  "mappings": {
    "properties": {
      "retailer_id": { "type": "keyword" },
      "brand": { "type": "keyword" },
      "title": { "type": "text" }
    }
  }
}

```

Data Seeded in Index

```auto
"retailer_id" "brand" "title" 
10000, "Nike" "Sports T Shirt Small Black"
10001, "Nike" "Sports T Shirt Medium Black"
10002, "Nike" "Sports T Shirt Large Black"
10010, "Nike" "OnePlus Nord CE 2 Lite 5G"
10011, "Nike" "OnePlus Nord 2T 5G (Jade Fog, 8GB RAM, 128GB Storage)"
10012, "Nike" "OnePlus Nord 2T 5G (Jade Fog, 12GB RAM, 256GB Storage)"

```

Expecting to get top 2 data for Nike and One Plus, but result always return first 2 records of nike only. Even if change size as 4 data returned are 3 of Nike and 1 of Oneplus.

Please suggest how to get desired output.

```auto
GET /poc_agg/_search
{
  "aggs": {
    "distict_brand": {
      "terms": {
        "field": "brand"
      }
    }
  }, 
  "_source": ["retailer_id","brand","title"],
  "size": 2
}

```

---

<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 6, 2023, 11:26am UTC](https://discuss.elastic.co/t/get-top-10-data-for-each-group/327349/2 "2023-04-06T11:26:12Z")

</div>

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