# Term Aggregation on sorted data

**URL:** <https://discuss.elastic.co/t/term-aggregation-on-sorted-data/303361>\
**Category:** Elasticsearch\
**Created:** [April 27, 2022, 8:33am UTC](https://discuss.elastic.co/t/term-aggregation-on-sorted-data/303361 "2022-04-27T08:33:21Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![dev\_test](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dev_test/32/79287_2.png) [@dev\_test](https://discuss.elastic.co/u/dev_test)\
**Post date:** [April 27, 2022, 8:33am UTC](https://discuss.elastic.co/t/term-aggregation-on-sorted-data/303361/1 "2022-04-27T08:33:21Z")

</div>

Hi,  
i want to run some term aggregations on sorted data and i want these changes to be reflected inside the buckets.

Suppose i have an order\_id field inside my document. I query over the index with size 0, while sort my data in order against order\_id, and after it i'm using term aggregations. The query runs successfully. But the buckets contain data in complete random order.

Order inside aggregations i guess wont help because it will just sort the data already inside the bucket plus the i want order / sorting against order\_id and the field in term aggregation is source\_key

Here is the query i'm trying to write.

```auto
{
  "size": 0,
  "_source": false,
  "query": {
    "bool": {
      "must": [
        {
          "terms": {
            "item_type_number": [
              9,
              10
            ]
          }
        }
      ],
      "must_not": {
        "has_child": {
          "type": "views",
          "query": {
            "bool": {
              "must": [
                {
                  "term": {
                    "user_id": 87
                  }
                }
              ]
            }
          }
        }
      }
    }
  },
  "sort": [
    {
      "order_id": {
        "order": "asc"
      }
    }
  ],
  "aggs": {
    "book": {
      "filter": {
        "bool": {
          "must": [
            {
              "terms": {
                "item_type_number": [
                  9
                ]
              }
            }
          ]
        }
      },
      "aggs": {
        "data": {
          "terms": {
            "field": "source_key",
            "size": 20
          }
        }
      }
    },
    "game": {
      "filter": {
        "bool": {
          "must": [
            {
              "terms": {
                "item_type_number": [
                  10
                ]
              }
            }
          ]
        }
      },
      "aggs": {
        "data": {
          "terms": {
            "field": "source_key",
            "size": 20
          }
        }
      }
    }
  }
}

```

---

<div class="post-metadata">

**Author:** ![RabBit\_BR](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/rabbit_br/32/82261_2.png) [@RabBit\_BR](https://discuss.elastic.co/u/RabBit_BR)\
**Post date:** [April 27, 2022, 1:22pm UTC](https://discuss.elastic.co/t/term-aggregation-on-sorted-data/303361/2 "2022-04-27T13:22:11Z")

</div>

You tried this:

> **[Bucket sort aggregation | Elasticsearch Guide \[8.1\] | Elastic](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-pipeline-bucket-sort-aggregation.html)**

---

<div class="post-metadata">

**Author:** ![dev\_test](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dev_test/32/79287_2.png) [@dev\_test](https://discuss.elastic.co/u/dev_test)\
**Post date:** [April 28, 2022, 6:36am UTC](https://discuss.elastic.co/t/term-aggregation-on-sorted-data/303361/3 "2022-04-28T06:36:58Z")

</div>

what this aggregation does is it sorts the result within the buckets made. What i need is to sort data first and select some documents from the sorted list so in my case bucket sort won't help me.

I used collapse method to group the data and then sorted the results within this grouping.  
Sample

```auto
,
  "collapse": {
    "field": "item_type_number",
    "inner_hits": {
      "name": "some_bucket_name_here",
      "size": 20,
      "sort": [
        {
          "order_id": {
            "order": "desc"
          }
        }
      ]
    }
  }

```

---

<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:** [May 26, 2022, 6:37am UTC](https://discuss.elastic.co/t/term-aggregation-on-sorted-data/303361/4 "2022-05-26T06:37:40Z")

</div>

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