Given a specific order of document IDs [1, 4, 2, 5] and some filtering criteria { match: {...} }, what is the most efficient method to ensure that the resulting documents are retrieved in the desired order [1, 4, 2, 5]?
Here is an example of a sample document:
{
"id": <uuid>,
"name": "Some name",
"description": "Some description"
}
I am aware that Elasticsearch stores _id for each document and that the id field within my document aligns with this.
Therefore, I would like to provide an array of IDs and retrieve the results in that specified order after applying all necessary filters.
For instance, suppose I have the following 4 documents:
{
"id": "abcd",
"name": "test1",
"description": "test1"
},
{
"id": "bcde",
"name": "test2",
"description": "test2"
},
{
"id": "cdef",
"name": "test3",
"description": "test3"
}
If I have an array of IDs ["cdef", "abcd", "bcde"], I would like to query Elasticsearch and receive the results in the specified order, meaning the document with the ID "cdef" should appear first in the hits, followed by "abcd" and then "bcde".