# Sorting data by "\_id" behavior

**URL:** <https://discuss.elastic.co/t/sorting-data-by-id-behavior/173908>\
**Category:** Elasticsearch\
**Created:** [March 26, 2019, 11:18am UTC](https://discuss.elastic.co/t/sorting-data-by-id-behavior/173908 "2019-03-26T11:18:09Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![maddysqlpro](https://avatars.discourse-cdn.com/v4/letter/m/e9bcb4/32.png) [@maddysqlpro](https://discuss.elastic.co/u/maddysqlpro)\
**Post date:** [March 26, 2019, 11:18am UTC](https://discuss.elastic.co/t/sorting-data-by-id-behavior/173908/1 "2019-03-26T11:18:09Z")

</div>

Hi,

I want to know how ES behaves when i sort the result based on \_id.  
Does it return the docs in the order it is inserted into the ES?

My requirement is to retrieve the docs in the order its inserted.  
Is there a way to achieve this without using a custom field.

Thanks.

---

<div class="post-metadata">

**Author:** ![DavidTurner](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/davidturner/32/22453_2.png) [@DavidTurner](https://discuss.elastic.co/u/DavidTurner)\
**Post date:** [March 26, 2019, 11:24am UTC](https://discuss.elastic.co/t/sorting-data-by-id-behavior/173908/2 "2019-03-26T11:24:37Z")

</div>

No, `_id` does not correspond with insertion order.

More generally, if you have two concurrent indexing requests then it requires coordination to put them into a specific order, and this can be quite time-consuming. Elasticsearch tries to avoid doing this kind of cross-request coordination as much as possible.

If you want to put all your documents into a specific order you will need to do so yourself. If you can tolerate a small amount of disorder then perhaps you can just use timestamps.

---

<div class="post-metadata">

**Author:** ![maddysqlpro](https://avatars.discourse-cdn.com/v4/letter/m/e9bcb4/32.png) [@maddysqlpro](https://discuss.elastic.co/u/maddysqlpro)\
**Post date:** [March 26, 2019, 11:28am UTC](https://discuss.elastic.co/t/sorting-data-by-id-behavior/173908/3 "2019-03-26T11:28:45Z")

</div>

Thank you for the quick reply.  
Regarding timestamp, if i understand correctly, we need to have a custom timestamp field, and sort based on timestamp. correct?

---

<div class="post-metadata">

**Author:** ![DavidTurner](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/davidturner/32/22453_2.png) [@DavidTurner](https://discuss.elastic.co/u/DavidTurner)\
**Post date:** [March 26, 2019, 11:29am UTC](https://discuss.elastic.co/t/sorting-data-by-id-behavior/173908/4 "2019-03-26T11:29:02Z")

</div>

Yes, that's right.

---

<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:** [April 23, 2019, 11:29am UTC](https://discuss.elastic.co/t/sorting-data-by-id-behavior/173908/5 "2019-04-23T11:29:15Z")

</div>

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