Alexey Tigarev
Alexey Tigarev

Reputation: 1945

Does Elasticsearch keep an order of multi-value fields?

Does Elasticsearch keep an order of multi-value fields?

I.e. if I've put following values into fields:

{
    "values": ["one", "two", "three"],
    "values_original": ["1", "2", "3"]
}

(Given that fields are not analyzed)

Can I be sure that the contents of lists will always be returned in the same order I put it there?

In the example above, I want to make sure that "one" on first position in "values" will always correspond to "1" in "values_original" etc.

I could keep it also as nested objects, i.e.

{
    "values": [
        {"original": "1", "new": "one"}, 
        {"original":"2", "new":"two"},
        {"original":"3","new":"three"}
    ]
}

but I want to avoid the overhead.

If it is guaranteed that order of values in multi-value field is preserved, then my approach of keeping two parallel multi-valued fields will work.

Upvotes: 4

Views: 5328

Answers (2)

Alexey Tigarev
Alexey Tigarev

Reputation: 1945

I found out the answer.

Yes, I can rely on Elasticsearch to keep an order of values in multivalue field within a document. (However, when I am performing a search, there is no information available to Elasticsearch about at what position certain term was).

According to documentation:

When you get a document back from Elasticsearch, any arrays will be in the same order as when you indexed the document. The _source field that you get back contains exactly the same JSON document that you indexed.

However, arrays are indexed—made searchable—as multivalue fields, which are unordered. At search time, you can’t refer to “the first element” or “the last element.” Rather, think of an array as a bag of values.

https://www.elastic.co/guide/en/elasticsearch/guide/current/complex-core-fields.html#_multivalue_fields

Upvotes: 9

Daniel Schneiter
Daniel Schneiter

Reputation: 1996

Elasticsearch - The Definitive Guide says the following:

When you get a document back from Elasticsearch, any arrays will be in the same order as when you indexed the document. The _source field that you get back contains exactly the same JSON document that you indexed.

However, arrays are indexed—made searchable—as multivalue fields, which are unordered. At search time, you can’t refer to “the first element” or “the last element.” Rather, think of an array as a bag of values.

So it seems that for stored fields order is preserved, for indexed fields it's not.

Upvotes: 4

Related Questions