# Sorted Pagination on Composite Aggregation

**URL:** <https://discuss.elastic.co/t/sorted-pagination-on-composite-aggregation/308498>\
**Category:** Elasticsearch\
**Created:** [June 29, 2022, 4:19pm UTC](https://discuss.elastic.co/t/sorted-pagination-on-composite-aggregation/308498 "2022-06-29T16:19:36Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Mohammad\_Musharraf](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mohammad_musharraf/32/107745_2.png) [@Mohammad\_Musharraf](https://discuss.elastic.co/u/Mohammad_Musharraf)\
**Post date:** [June 29, 2022, 4:19pm UTC](https://discuss.elastic.co/t/sorted-pagination-on-composite-aggregation/308498/1 "2022-06-29T16:19:36Z")

</div>

I have ElasticSearch 7.1 documents with following mappings:-

```auto
{
  "event" : {
    "mappings" : {
      "properties" : {
        "Code1" : {
          "type" : "keyword"
        },
        "Code2" : {
          "type" : "keyword"
        },
        "Date1" : {
          "type" : "date"
        },
        "Date2" : {
          "type" : "date"
        },
        "Value" : {
          "type" : "long"
        }
      }
    }
  }
}

```

I want to group the documents by `Code1`, `Code2`, `Date1`, `Date2` into buckets  
together with

`TotalValue` which is sum of `Value` field of all documents in a bucket

and

`Count` which is number of documents in a bucket.

Final Output which I want is like this:-

```auto
{
    {
        "Code1": "ABC",
        "Code2": "XYZ",
        "Date1": "01/01/2022",
        "Date2": "31/01/2022",
        "TotalValue": "100",
        "Count": "3"
    },
    ...
}

```

Also I want, paginated output with sorting on any of the output fields of the bucket, viz. ; `Code1`, `Code2`, `Date1`, `Date2`, `TotalValue`, `Count`.

Using Composite Aggregation, I came up with this query, which is able to do aggregation as reqd with paginated response and sorting on `Code1`, `Code2`, `Date1`, `Date2`

but not able to do proper sorted pagination on `TotalValue`and `Count`(doc\_count) fields.

```auto
GET event/_search
{
  "size":0,
  "aggs": {
      "AggregatedBucket": {
        "composite": {
          "size":"10",
          "sources": [
           {
              "Code1": {
                "terms": {
                  "field": "Code1",
                  "order": "desc"
                }
              }
            },
           {
              "Code2": {
                "terms": {
                  "field": "Code2",
                  "order": "desc"
                }
              }
            },
            {
              "Date1": {
                "terms": {
                  "field": "Date1",
                  "order": "desc"
                }
              }
            },
            {
              "Date2": {
                "terms": {
                  "field": "Date2",
                  "order": "desc"
                }
              }
            }
          ]
        },
        "aggs":{
            "TotalValue":{
              "sum": {
                "field": "Value"
              }
            }
        }
      }
    }
}}

```

Here is the truncated response I am getting

```auto
  "aggregations" : {
    "AggregatedBucket" : {
      "after_key" : {
        "Code1" : "ABC2",
        "Code2" : "XYZ2",
        "Date1" : "02/01/2022",
        "Date2" : "02/02/2022"
      },
      "buckets" : [
        {
          "key" : {
            "Code1" : "ABC1",
            "Code2" : "XYZ1",
            "Date1" : "01/01/2022",
            "Date2" : "01/02/2022"
          },
          "doc_count" : 1,
          "TotalValue" : {
            "value" : 4.0
          }
        },
        {
          "key" : {
            "Code1" : "ABC2",
            "Code2" : "XYZ2",
            "Date1" : "02/01/2022",
            "Date2" : "02/02/2022"
          },
          "doc_count" : 1,
          "TotalValue" : {
            "value" : 3.0
          }
        }
     ]
   }
 }

```

Any alternate way to return my expected response would also be helpful.

---

<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:** [June 29, 2022, 4:19pm UTC](https://discuss.elastic.co/t/sorted-pagination-on-composite-aggregation/308498/2 "2022-06-29T16:19:36Z")

</div>

ElasticSearch 7.1 is [EOL](https://www.elastic.co/support/eol) and no longer supported. Please upgrade ASAP.

(This is an automated response from your friendly Elastic bot. Please report this post if you have any suggestions or concerns :elasticheart: )

---

<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:** [July 27, 2022, 4:19pm UTC](https://discuss.elastic.co/t/sorted-pagination-on-composite-aggregation/308498/3 "2022-07-27T16:19:43Z")

</div>

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