Sorting data by "_id" behavior


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.


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.

1 Like

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?

Yes, that's right.

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