# Composite Aggregation and Sorting with a non source

**URL:** <https://discuss.elastic.co/t/composite-aggregation-and-sorting-with-a-non-source/131932>\
**Category:** Elasticsearch\
**Created:** [May 15, 2018, 12:30pm UTC](https://discuss.elastic.co/t/composite-aggregation-and-sorting-with-a-non-source/131932 "2018-05-15T12:30:45Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![William\_Chu](https://avatars.discourse-cdn.com/v4/letter/w/f9ae1b/32.png) [@William\_Chu](https://discuss.elastic.co/u/William_Chu)\
**Post date:** [May 15, 2018, 12:30pm UTC](https://discuss.elastic.co/t/composite-aggregation-and-sorting-with-a-non-source/131932/1 "2018-05-15T12:30:45Z")

</div>

Hello,

Using Elasticsearch, I would like to construct a query that buckets my results but also allows for pagination. Based on this requirement, it seems that I need to use a composite aggregation.

However, I also would like to sort the results based on a field that is not one of the sources. It doesn’t seem like composite aggregation currently supports this. Is this correct? If you want to sort by a field, it has to be one of the “sources” in the elasticsearch query

Here’s an example of what I’m trying to do:

I have a giant list of messages from different people that are timestamped of when the message was sent. I would like to bucket the messages by person and have the list of people sorted by the time of the last message sent. If John Smith was the person to send me the most recent message, then his name bucket would appear at the top of the list. The next person in the list would be the person who sent the most recent message after John Smith.

In this example, the composite aggregation source would be the name of the person.

I believe that can’t use terms aggregation because it doesn’t support pagination. Is there a way to make this work with composite aggregation?

---

<div class="post-metadata">

**Author:** ![jimczi](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jimczi/32/47985_2.png) [@jimczi](https://discuss.elastic.co/u/jimczi)\
**Post date:** [May 18, 2018, 9:24am UTC](https://discuss.elastic.co/t/composite-aggregation-and-sorting-with-a-non-source/131932/2 "2018-05-18T09:24:46Z")

</div>

The composite aggregation is a way to paginate over all buckets of a complex aggregation. It doesn't allow custom sort. If you want a list of messages collapsed by user and sorted by most recent you can use field collapsing:  
[https://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-collapse.html](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-collapse.html)

```auto
GET my_index/_search
{
    "collapse" : {
        "field" : "user", 
    },
    "sort": [{"timestamp": "desc" }]
}

```

The query will return the most recent message by user and you can paginate over the results using `from`.

---

<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 15, 2018, 9:24am UTC](https://discuss.elastic.co/t/composite-aggregation-and-sorting-with-a-non-source/131932/3 "2018-06-15T09:24:51Z")

</div>

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