# Provide order of ids and retrieve docs in the provided order

**URL:** https://discuss.elastic.co/t/provide-order-of-ids-and-retrieve-docs-in-the-provided-order/282535
**Category:** Elasticsearch
**Created:** [August 26, 2021, 5:41am UTC](https://discuss.elastic.co/t/provide-order-of-ids-and-retrieve-docs-in-the-provided-order/282535 "2021-08-26T05:41:12Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![voldemort02](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/voldemort02/32/93404_2.png) [@voldemort02](https://discuss.elastic.co/u/voldemort02)
#### Post date: [August 26, 2021, 5:41am UTC](https://discuss.elastic.co/t/provide-order-of-ids-and-retrieve-docs-in-the-provided-order/282535/1 "2021-08-26T05:41:12Z")

</div>

I have I have order of ids, [1, 4, 2, 5] and some filters { match: {...} } and I want the resulting documents to be returned in the order [1, 4, 2, 5]. What would be the best way to achieve this?

Sample document:

```auto
{
    "id": <uuid>,
    "name": "Some name",
    "description": "Some description"
}

```

Now I am aware that ES stores \_id for each document and \_id for es doc and id field inside my doc are same.

So I want to provide array of ids and get the results in that order after all the filtering.

For example I have 4 documents

```auto
{
    "id": "abcd",
    "name": "test1",
    "description": "test1"
},
{
    "id": "bcde",
    "name": "test2",
    "description": "test2"
},
{
    "id": "cdef",
    "name": "test3",
    "description": "test3"
}

```

now i have array of ids ["cdef", "abcd", "bcde"] so I want to query es and get results in the above specified order meaning doc with `cdef` has to be first in the hits then doc with `abcd` and then doc with `bcde`

---

<div class="post-metadata">

### Author: ![warkolm](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/warkolm/32/39224_2.png) [@warkolm](https://discuss.elastic.co/u/warkolm)
#### Post date: [August 26, 2021, 5:48am UTC](https://discuss.elastic.co/t/provide-order-of-ids-and-retrieve-docs-in-the-provided-order/282535/2 "2021-08-26T05:48:33Z")

</div>

Elasticsearch will not guarantee the order like that, you can only do sorts.

---

<div class="post-metadata">

### Author: ![voldemort02](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/voldemort02/32/93404_2.png) [@voldemort02](https://discuss.elastic.co/u/voldemort02)
#### Post date: [August 26, 2021, 5:52am UTC](https://discuss.elastic.co/t/provide-order-of-ids-and-retrieve-docs-in-the-provided-order/282535/3 "2021-08-26T05:52:17Z")

</div>

Yes by default ES won't guarantee that but is there a way using using custom function for sorting? I found some snippets using painless script but not sure from docs how can I achieve behavior I want?

---

<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: [September 23, 2021, 5:52am UTC](https://discuss.elastic.co/t/provide-order-of-ids-and-retrieve-docs-in-the-provided-order/282535/4 "2021-09-23T05:52:41Z")

</div>

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