Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

elasticsearch doc['...'] Arrays and order

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?

like image 473
Holger Will Avatar asked Jun 24 '17 23:06

Holger Will


People also ask

Does Elasticsearch preserve order?

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.

How do I capture a specific field in Elasticsearch?

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.

Can we store list in Elasticsearch?

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.

What is Elasticsearch document source?

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.


1 Answers

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

like image 110
dddd1919 Avatar answered Oct 06 '22 09:10

dddd1919