Elasticsearch 是否保持多值字段的顺序?

Does Elasticsearch keep an order of multi-value fields?

Elasticsearch 是否保持多值字段的顺序?

即如果我将以下值放入字段:

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

(假设字段未分析)

我能确定列表的内容总是按照我放在那里的相同顺序返回吗?

在上面的示例中,我想确保 "values" 中第一个位置的 "one" 始终对应于 "values_original" 等中的“1”

我也可以将它保留为嵌套对象,即

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

但我想避免开销。

如果保证保留多值字段中值的顺序,那么我保持两个并行多值字段的方法就可以了。

我找到了答案。

是的,我可以依靠 Elasticsearch 来保持文档中多值字段中值的顺序。 (但是,当我执行搜索时,Elasticsearch 没有关于某个术语在什么位置的可用信息)。

根据文档:

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

Elasticsearch - The Definitive Guide 表示如下:

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.

所以似乎对于存储字段顺序是保留的,对于索引字段它不是。