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?
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.
There are two recommended methods to retrieve selected fields from a search query: Use the fields option to extract the values of fields present in the index mapping. Use the _source option if you need to access the original data that was passed at index time.
The lists field is dynamically added as an object field. The second document contains no arrays, but can be indexed into the same fields. The query looks for elasticsearch in the tags field, and matches both documents.
The _source field contains the original JSON document body that was passed at index time. The _source field itself is not indexed (and thus is not searchable), but it is stored so that it can be returned when executing fetch requests, like get or search.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With