Get top 10 data for each group

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

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

Data Seeded in Index

"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.

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

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