Hudson Dunice
Hudson Dunice

Reputation: 1

Unable to loop through array field ES 6.1

I'm facing a problem in ElasticSearch 6.1 that I cannot solve and I don't know why. I have read the docs several times and maybe I'm missing something.

I have a scripted query that needs to do some calculation before decides if a record is available or not.

Here is the following script: https://gist.github.com/dunice/a3a8a431140ec004fdc6969f77356fdf

What I'm doing is trying to loop though an array field with the following source:

  "unavailability": [
    {
      "starts_at": "2018-11-27T18:00:00+00:00",
      "local_ends_at": "2018-11-27T15:04:00",
      "local_starts_at": "2018-11-27T13:00:00",
      "ends_at": "2018-11-27T20:04:00+00:00"
    },
    {
      "starts_at": "2018-12-04T18:00:00+00:00",
      "local_ends_at": "2018-12-04T15:04:00",
      "local_starts_at": "2018-12-04T13:00:00",
      "ends_at": "2018-12-04T20:04:00+00:00"
    },
  ]

When the script is executed it throws the error: No field found for [unavailability] in mapping with types [aircraft]

Is there any clue to make it work?

Thanks

UPDATE

Query: https://gist.github.com/dunice/3ccd7d83ca6ddaa63c11013b84e659aa

UPDATE 2

Mapping: https://gist.github.com/dunice/f8caee114bbd917115a21b8b9175a439

Data example: https://gist.github.com/dunice/8ad0602bc282b4ca19bce8ae849117ad

Upvotes: 0

Views: 428

Answers (2)

Hudson Dunice
Hudson Dunice

Reputation: 1

I solve my use-case in a different approach.

Instead having a field as array of object like unavailability was I decided to create two fields as array of datetime: unavailable_from unavailable_to

My script walks through the first field then checks the second with the same position.

UPDATE

The direct access to _source is disabled by default: https://github.com/elastic/elasticsearch/issues/17558

Upvotes: 0

Val
Val

Reputation: 217314

You cannot access an array present in the source document via doc_values (i.e. doc). You need to directly access the source document via the _source variable instead, like this:

for(int i = 0; i < params._source['unavailability'].length; i++) {

Note that depending on your ES version, you might want to try ctx._source or just _source instead of params._source

Upvotes: 1

Related Questions