# Multi GET and sort

**URL:** <https://discuss.elastic.co/t/multi-get-and-sort/165616>\
**Category:** Elasticsearch\
**Created:** [January 24, 2019, 1:46pm UTC](https://discuss.elastic.co/t/multi-get-and-sort/165616 "2019-01-24T13:46:43Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![tombee74](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/tombee74/32/54223_2.png) [@tombee74](https://discuss.elastic.co/u/tombee74)\
**Post date:** [January 24, 2019, 1:46pm UTC](https://discuss.elastic.co/t/multi-get-and-sort/165616/1 "2019-01-24T13:46:43Z")

</div>

Hello,

I want to retrieve a list of documents of the same type based on document IDs - Multi GET API works great.

Now, I would like to sort the documents based on a specific field of that type.  
Is it possible to sort the list using MGET API on ES side? Or, do I need to sort it on the client side?

I'm using Java High Level REST Client.

Thank you.

---

<div class="post-metadata">

**Author:** ![Tosh](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/tosh/32/29269_2.png) [@Tosh](https://discuss.elastic.co/u/Tosh)\
**Post date:** [January 25, 2019, 6:19am UTC](https://discuss.elastic.co/t/multi-get-and-sort/165616/2 "2019-01-25T06:19:34Z")

</div>

Hi ,

You can do below :

**endpoint:**  
[http://localhost:9200/index\_name/\_docType/\_search](http://localhost:9200/index_name/_docType/_search)

**query :**

```
{  
   "size":100,
   "timeout":"500ms",
   "query":{  
    "match_all":{}
   },
   "sort":[  
      {  
         "fieldname":{  
            "order":"desc"
         }
      }
   ]
} 

```

**fieldname** : is the name of the field you want to perform sorting on .  
**size** : number of documents you want to fetch in results .

In the query section you can have different combination of queries for simplicity i've put match\_all , it will fetch all documents .

---

<div class="post-metadata">

**Author:** ![tombee74](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/tombee74/32/54223_2.png) [@tombee74](https://discuss.elastic.co/u/tombee74)\
**Post date:** [January 26, 2019, 2:54pm UTC](https://discuss.elastic.co/t/multi-get-and-sort/165616/3 "2019-01-26T14:54:52Z")

</div>

Thanks for reply. I'm not sure if I can use this when I have a list of document IDs on the input.

Let me rephrase my question. I would like to retrieve a list of documents (of the same type) when I have a list of document IDs. And, the result list would be sorted by some field.

I know I can retrieve the list using MGET. But I do not know how to sort it (on ES).

---

<div class="post-metadata">

**Author:** ![abdon](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/abdon/32/9195_2.png) [@abdon](https://discuss.elastic.co/u/abdon)\
**Post date:** [January 27, 2019, 1:54pm UTC](https://discuss.elastic.co/t/multi-get-and-sort/165616/4 "2019-01-27T13:54:56Z")

</div>

Instead of a multiget, you can use the [`ids` query](https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-ids-query.html). That query can be combined with a [`sort` clause](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-sort.html) to sort the documents however you want those to be sorted.

---

<div class="post-metadata">

**Author:** ![tombee74](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/tombee74/32/54223_2.png) [@tombee74](https://discuss.elastic.co/u/tombee74)\
**Post date:** [January 27, 2019, 7:55pm UTC](https://discuss.elastic.co/t/multi-get-and-sort/165616/5 "2019-01-27T19:55:20Z")

</div>

Understand. Thank you!

---

<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 24, 2019, 8:00pm UTC](https://discuss.elastic.co/t/multi-get-and-sort/165616/6 "2019-02-24T20:00:01Z")

</div>

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