Are hashes/objects saved in order?

if I insert this field in this order:
"name_of_field": {"first":1,"second":2,"third":3}

will it always save in the same order as I inserted ?

Based on my test, inserting multiple objects like that can make it save it different order, like:

"name_of_field": {"third":3,"first":1,"second":2}

Elasticsearch does not change the document but JSON serialisation and deserialisation does generally not guarantee that order is preserved.

Is there any way I could enforce an order on a particular hash field ?

To do so you would need to control how your documents are serialised and deserialised which means e.g. not using scripted updates. Which client(s) are you using?

Ruby Elasticsearch gem: https://github.com/elastic/elasticsearch-ruby

Do you want me to send the update query here ?

I will update recommended not relying on the order as it will be more resilient given that ordering is generally not guaranteed.

Alright then,
so the best will probably be to put it under an array, like:

"name_of_field": [{"first":1},{"second":2},{"third":3}]

That sounds like a bad idea. Why do you feel you need to preserve order??

This object will be sent through an API that will send to the browser and it needs to be displayed in a certain order

any suggestion on a better way to deal with this ?

Sort it in the browser?

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