Painless does not return array elements in the same order they were inserted

(Zaid Amir) #1


I am trying to write a script to calculate the euclidean distance for some points stored as an array. Each array consists of 128 Double values.

The field is currently indexed and stored as such:

"my_array": [0.176, 0.165, 0.987, 0.67, ...]

When I use Painless to read and loop the array using doc['my_array'].values; it returns the array sorted for some reason. So instead of the above I get the array as:[0.165, 0.176, 0.67, 0.987, ...]

What other options do I have to get the array in the same order?

I am using ES 5.2

(Adrien Grand) #2

Values are indeed always returned in order. If you need to maintain order, one potential trick is to encode your values in such a way that they will be in order, for instance if all values are between 0 and 1, you could add the index to all values at index time, ie. [0.176, 0.165, 0.987, 0.67, ...] becomes [0.176, 1.165, 2.987, 3.67, ...] and then subtract it at search time.

(Zaid Amir) #3

Yeah, problem is my values are between -1 and 1 so for now I have mapped the array as keyword then splitting and parsing each double entry

(system) #4

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