# Paging and sorting results after a term aggregation by doc fields

**URL:** <https://discuss.elastic.co/t/paging-and-sorting-results-after-a-term-aggregation-by-doc-fields/244526>\
**Category:** Elasticsearch\
**Created:** [August 11, 2020, 9:50am UTC](https://discuss.elastic.co/t/paging-and-sorting-results-after-a-term-aggregation-by-doc-fields/244526 "2020-08-11T09:50:38Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Walter\_Chicca](https://avatars.discourse-cdn.com/v4/letter/w/c67d28/32.png) [@Walter\_Chicca](https://discuss.elastic.co/u/Walter_Chicca)\
**Post date:** [August 11, 2020, 9:50am UTC](https://discuss.elastic.co/t/paging-and-sorting-results-after-a-term-aggregation-by-doc-fields/244526/1 "2020-08-11T09:50:38Z")

</div>

Hi everyone,  
what i'm trying to do is sorting the results from a term aggregation using the fields in the starting document, the final goal is to be able sorting and paging a datatable server side because there're too many records to load in a single call. i tried with bucket\_sort aggregation but it allows only the pagination but not to sort by fields  
elastic version 7.0.1

here is the query that i'm using:

```auto
{
"size": 0,
"query": {
    "bool": {
    
        "should": [
            {
                "term": {
                    "check_field__one.keyword": {
                        "value": "00010",
                        "boost": 1.0
                    }
                }
            },
            {
                "term": {
                    "check_field__two.keyword": {
                        "value": "00010",
                        "boost": 1.0
                    }
                }
            }
        ],
        "adjust_pure_negative": true,
        "minimum_should_match": "1",
        "boost": 1.0
    }
},

"aggregations": {
    "by_ID": {
        "terms": {
            "field": "_ID.keyword",
            "size": 10000,
            "min_doc_count": 1,
            "shard_min_doc_count": 0,
            "show_term_doc_count_error": false,
            "order": [
                {
                    "_count": "desc"
                },
                {
                    "_key": "asc"
                }
            ]
        },
        "aggregations": {
            "min_START_DATE": {
                "min": {
                    "field": "TMS_START_DATE"
                }
            },
            "max_END_DATE": {
                "max": {
                    "field": "TMS_END_DATE",
                    "missing": 9223372036854775807
                }
            },
            
            "top_START_DATE": {
                "top_hits": {
                    "from": 0,
                    "size": 1,
                    "version": false,
                    "seq_no_primary_term": false,
                    "explain": false,
                    "_source": {
                        "includes": [
                           ...fields to show in table
                        ],
                        "excludes": []
                    },
                    "sort": [
                        {
                            "TMS_START_DATE": {
                                "order": "desc"
                            }
                        }
                    ]
                }
            },
           
            // that stage allows the pagination but not the sort by doc fields
            "sort": {
                "bucket_sort": {
                    "sort": [],
                    "from": 0,
                    "size": 10,
                    "gap_policy": "SKIP"
                }
            }
        }
    }
  }
}

```

Thanks in advance

---

<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:** [September 8, 2020, 9:50am UTC](https://discuss.elastic.co/t/paging-and-sorting-results-after-a-term-aggregation-by-doc-fields/244526/2 "2020-09-08T09:50:40Z")

</div>

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