# Sorting search results into buckets, and then sorting again

**URL:** <https://discuss.elastic.co/t/sorting-search-results-into-buckets-and-then-sorting-again/116780>\
**Category:** Elasticsearch\
**Created:** [January 24, 2018, 1:53am UTC](https://discuss.elastic.co/t/sorting-search-results-into-buckets-and-then-sorting-again/116780 "2018-01-24T01:53:43Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![jswiesner](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jswiesner/32/26744_2.png) [@jswiesner](https://discuss.elastic.co/u/jswiesner)\
**Post date:** [January 24, 2018, 1:53am UTC](https://discuss.elastic.co/t/sorting-search-results-into-buckets-and-then-sorting-again/116780/1 "2018-01-24T01:53:43Z")

</div>

Hello,

I am trying to figure out a way to assemble a query to perform two levels of sorting. My documents have two fields that I'm trying to sort on:

```
{
   "user": {
          "type": "keyword"
    },
   "date": {
          "type": "long"
    },
}

```

I would like my search results to be returned in such a way that all documents with the same **user** field are adjacent to one another. So in a UI all the documents pertaining to one user show consecutively. Furthermore, I want to sort these buckets of user-aggregated documents according to the date timestamp, so that the bucket with the oldest date value shows up on the top of the search results.

I've been able to achieve this using an aggregation as follows:

```
{
   "size":0,
   "aggs":{
      "by_user":{
         "terms":{
            "field":"user",
            "order":{
               "oldest_date":"asc"
            }
         },
         "aggs":{
            "by_date":{
               "top_hits":{
                  "sort":[
                     {
                        "date":{
                           "order":"asc"
                        }
                     }
                  ]
               }
            },
            "oldest_date":{
               "min":{
                  "script":{
                     "inline":"doc['date'].value",
                     "lang":"painless"
                  }
               }
            }
         }
      }
   }
}

```

This works great, but the problem is I **also** need true pagination. Is there any way I can do this form of hierarchical sorting using just the sort option in my query so I can still use the pagination features?

Thanks in advance for the help!

---

<div class="post-metadata">

**Author:** ![jswiesner](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jswiesner/32/26744_2.png) [@jswiesner](https://discuss.elastic.co/u/jswiesner)\
**Post date:** [January 25, 2018, 5:52pm UTC](https://discuss.elastic.co/t/sorting-search-results-into-buckets-and-then-sorting-again/116780/2 "2018-01-25T17:52:21Z")

</div>

Does anyone have some ideas on this?

---

<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:** [February 22, 2018, 5:52pm UTC](https://discuss.elastic.co/t/sorting-search-results-into-buckets-and-then-sorting-again/116780/3 "2018-02-22T17:52:21Z")

</div>

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