# Composite aggregation query with bucket\_sort does not work properly

**URL:** <https://discuss.elastic.co/t/composite-aggregation-query-with-bucket-sort-does-not-work-properly/311055>\
**Category:** Elasticsearch\
**Created:** [July 31, 2022, 10:51am UTC](https://discuss.elastic.co/t/composite-aggregation-query-with-bucket-sort-does-not-work-properly/311055 "2022-07-31T10:51:07Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![fabiobozzo](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/fabiobozzo/32/105550_2.png) [@fabiobozzo](https://discuss.elastic.co/u/fabiobozzo)\
**Post date:** [July 31, 2022, 10:51am UTC](https://discuss.elastic.co/t/composite-aggregation-query-with-bucket-sort-does-not-work-properly/311055/1 "2022-07-31T10:51:07Z")

</div>

I have an index to store financial transactions:

```auto
{
  "mappings": {
    "_doc": {
      "properties": {
        "amount": {
          "type": "long"
        },
        "currencyCode": {
          "type": "keyword"
        },
        "merchantId": {
          "type": "keyword"
        },
        "merchantName": {
          "type": "text"
        },
        "partnerId": {
          "type": "keyword"
        },
        "transactionDate": {
          "type": "date"
        },
        "userId": {
          "type": "keyword"
        }
      }
    }
  }
}

```

Here's my query:

```auto
GET /transactions/_search
{
  "aggs": {
    "date_merchant": {
      "aggs": {
        "amount": {
          "sum": {
            "field": "amount"
          }
        },
        "amount_sort": {
          "bucket_sort": {
            "sort": [
              {
                "amount": {
                  "order": "desc"
                }
              }
            ]
          }
        },
        "top_hit": {
          "top_hits": {
            "_source": {
              "includes": [
                "merchantName",
                "currencyCode"
              ]
            },
            "size": 1
          }
        }
      },
      "composite": {
        "size": 1,
        "sources": [
          {
            "date": {
              "date_histogram": {
                "calendar_interval": "day",
                "field": "transactionDate"
              }
            }
          },
          {
            "merchant": {
              "terms": {
                "field": "merchantId"
              }
            }
          }
        ]
      }
    }
  },
  "query": {
    "bool": {
      "filter": [
        {
          "term": {
            "userId": "AAA"
          }
        },
        {
          "term": {
            "partnerId": "BBB"
          }
        },
        {
          "range": {
            "transactionDate": {
              "gte": "2022-07-01"
            }
          }
        },
        {
          "term": {
            "currencyCode": "EUR"
          }
        }
      ]
    }
  },
  "size": 0
}

```

Please note the `"size": 1` in the composite aggregation.

If I change it to `3` (based on my data)... I get different results!

That means the bucket\_sort operation doesn't work on the whole list of buckets, but just on the returned ones (if it's just one, that means it's not going to be sorted at all!)

How can I sort on ALL the buckets instead?

---

<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:** [August 28, 2022, 10:52am UTC](https://discuss.elastic.co/t/composite-aggregation-query-with-bucket-sort-does-not-work-properly/311055/2 "2022-08-28T10:52:03Z")

</div>

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