Multi GET and sort

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.

Hi ,

You can do below :

endpoint:
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 .

1 Like

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).

Instead of a multiget, you can use the ids query. That query can be combined with a sort clause to sort the documents however you want those to be sorted.

Understand. Thank you!

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