# Custom sorting with an Elasticsearch query?

**URL:** <https://discuss.elastic.co/t/custom-sorting-with-an-elasticsearch-query/254229>\
**Category:** Elasticsearch\
**Created:** [November 4, 2020, 8:48am UTC](https://discuss.elastic.co/t/custom-sorting-with-an-elasticsearch-query/254229 "2020-11-04T08:48:19Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![kumar.kapil](https://avatars.discourse-cdn.com/v4/letter/k/b4bc9f/32.png) [@kumar.kapil](https://discuss.elastic.co/u/kumar.kapil)\
**Post date:** [November 4, 2020, 8:48am UTC](https://discuss.elastic.co/t/custom-sorting-with-an-elasticsearch-query/254229/1 "2020-11-04T08:48:20Z")

</div>

I have some documents indexed on Elasticsearch, looking like these samples:

```auto
{'id': 1, 'user_group_id': 1, 'is_popular': false, 'added_at': '2020-10-05T10:27:59.214Z'}
{'id': 2, 'user_group_id': 2, 'is_popular': false, 'added_at': '2020-10-05T10:27:59.214Z'}
{'id': 3, 'user_group_id': 1, 'is_popular': false, 'added_at': '2020-10-10T10:27:59.214Z'}
{'id': 4, 'user_group_id': 2, 'is_popular': false, 'added_at': '2020-10-15T10:27:59.214Z'}
{'id': 5, 'user_group_id': 1, 'is_popular': false, 'added_at': '2020-10-15T10:27:59.214Z'}
{'id': 6, 'user_group_id': 2, 'is_popular': true, 'added_at': '2020-10-15T10:27:59.214Z'}
{'id': 7, 'user_group_id': 2, 'is_popular': true, 'added_at': '2020-10-15T10:27:59.214Z'}

```

I want to build a query that gets top 2 users have user\_group\_id = 1 and descending to added\_at date time on top and then all users have is\_popular = true and descending to added\_at date time and then added\_at date time in descending order user.

So, for the examples shown above, the results would be something like:

```auto
{'id': 5, 'user_group_id': 1, 'is_popular': false, 'added_at': '2020-10-15T10:27:59.214Z'}
{'id': 3, 'user_group_id': 1, 'is_popular': false, 'added_at': '2020-10-10T10:27:59.214Z'}
{'id': 6, 'user_group_id': 2, 'is_popular': true, 'added_at': '2020-10-15T10:27:59.214Z'}
{'id': 7, 'user_group_id': 2, 'is_popular': true, 'added_at': '2020-10-15T10:27:59.214Z'}
{'id': 4, 'user_group_id': 2, 'is_popular': false, 'added_at': '2020-10-15T10:27:59.214Z'}
{'id': 2, 'user_group_id': 2, 'is_popular': false, 'added_at': '2020-10-05T10:27:59.214Z'}
{'id': 1, 'user_group_id': 1, 'is_popular': false, 'added_at': '2020-10-05T10:27:59.214Z'}

```

Is it possible in Elasticsearch?

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [November 4, 2020, 11:16am UTC](https://discuss.elastic.co/t/custom-sorting-with-an-elasticsearch-query/254229/2 "2020-11-04T11:16:20Z")

</div>

I don't think you can produce exactly that output.

You can do:

> top 2 users have user\_group\_id = 1 and descending to added\_at date time on top

Using

> **[Top hits aggregation | Elasticsearch Guide \[8.11\] | Elastic](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-metrics-top-hits-aggregation.html)**

> all users have is\_popular = true and descending to added\_at date time

You can use a [`term` query](https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-term-query.html) and [sort on](https://www.elastic.co/guide/en/elasticsearch/reference/current/sort-search-results.html) `added_at`.

> added\_at date time in descending order user

I guess you want to exclude the previous results so I'd probably use a `bool` query with a `must_not` clause to exclude `is_popular = true` or just filter using again a `term` query on `is_popular` with value `false`.  
And sort again as saw previously.

> Is it possible in Elasticsearch?

I don't think it's doable with one single request. Which means that you probably will have to assemble the results yourself on the client side.

This could help may be:

> **[Multi search API | Elasticsearch Guide \[7.9\] | Elastic](https://www.elastic.co/guide/en/elasticsearch/reference/7.9/search-multi-search.html)**

---

<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:** [December 2, 2020, 11:16am UTC](https://discuss.elastic.co/t/custom-sorting-with-an-elasticsearch-query/254229/3 "2020-12-02T11:16:21Z")

</div>

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