7
votes

i need to access document values stored in arrays from script. The order of the items in the array is important.

using doc['...'] to retrieve the array will mix up the order :-(

suppose a simple document like this

{
    "ar":[5,4,3,2,1]
}

retrieved using this Query:

{
  "query":{
    "match_all":{}
  },
  "script_fields": {
    "values": {
      "script": {
        "inline":"return doc['ar']"
      }
    }
  }
}

will return the array in reversed(sorted) order: [1,2,3,4,5] is there a way to prevent this behavior?

i can not resort to using _source because i need this in a "has_child" query, which does not support _source.

any ideas?

1
did your array output always coming in reversed order ? - tom
No, allways sorted smallest to largest value:-( - Holger Will
What's your ElasticSearch version? I try it in my 2.3.3 version, the ar‘s values is in the reverse order, [5,4,3,2,1]. - Jimmy Zhang
I'm on 5.4.0... - Holger Will
upgraded to 5.4.2, same problem... - Holger Will

1 Answers

3
votes

Need to know how Elasticsearch indexed an array field.

similar question

To make field searchable, array field will be indexed in order, and you can not get the first value in script like doc['ar'][0]

if you want the origin array with order, you can use _source to get it like params._source['ar'], the results will be [5,4,3,2,1], but very slow than use doc