# Unable to loop through array field ES 6.1

**URL:** <https://discuss.elastic.co/t/unable-to-loop-through-array-field-es-6-1/116669>\
**Category:** Elasticsearch\
**Created:** [January 23, 2018, 12:57pm UTC](https://discuss.elastic.co/t/unable-to-loop-through-array-field-es-6-1/116669 "2018-01-23T12:57:16Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Hudson\_Dunice](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/hudson_dunice/32/26839_2.png) [@Hudson\_Dunice](https://discuss.elastic.co/u/Hudson_Dunice)\
**Post date:** [January 23, 2018, 12:57pm UTC](https://discuss.elastic.co/t/unable-to-loop-through-array-field-es-6-1/116669/1 "2018-01-23T12:57:16Z")

</div>

Hey guys,

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**  
Here is the query:

> <https://gist.github.com/dunice/3ccd7d83ca6ddaa63c11013b84e659aa>

Mapping:

> <https://gist.github.com/dunice/f8caee114bbd917115a21b8b9175a439>

Data example:

> <https://gist.github.com/dunice/8ad0602bc282b4ca19bce8ae849117ad>

---

<div class="post-metadata">

**Author:** ![val](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/val/32/138203_2.png) [@val](https://discuss.elastic.co/u/val)\
**Post date:** [January 23, 2018, 2:49pm UTC](https://discuss.elastic.co/t/unable-to-loop-through-array-field-es-6-1/116669/2 "2018-01-23T14:49:02Z")

</div>

I didn't see this was cross-posted, but here's my take:

> <https://stackoverflow.com/questions/48402418/unable-to-loop-through-array-field-es-6-1/48404481#48404481>

---

<div class="post-metadata">

**Author:** ![Hudson\_Dunice](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/hudson_dunice/32/26839_2.png) [@Hudson\_Dunice](https://discuss.elastic.co/u/Hudson_Dunice)\
**Post date:** [January 23, 2018, 2:58pm UTC](https://discuss.elastic.co/t/unable-to-loop-through-array-field-es-6-1/116669/3 "2018-01-23T14:58:21Z")

</div>

Hi @val

Thanks for the fast response.

As I previously answered your comment at StackOverflow, I already tried that approach but it throws **Variable [\_source] is not defined.**.

😕

---

<div class="post-metadata">

**Author:** ![val](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/val/32/138203_2.png) [@val](https://discuss.elastic.co/u/val)\
**Post date:** [January 23, 2018, 2:59pm UTC](https://discuss.elastic.co/t/unable-to-loop-through-array-field-es-6-1/116669/4 "2018-01-23T14:59:52Z")

</div>

See my second comment 😉

---

<div class="post-metadata">

**Author:** ![Hudson\_Dunice](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/hudson_dunice/32/26839_2.png) [@Hudson\_Dunice](https://discuss.elastic.co/u/Hudson_Dunice)\
**Post date:** [January 23, 2018, 10:09pm UTC](https://discuss.elastic.co/t/unable-to-loop-through-array-field-es-6-1/116669/5 "2018-01-23T22:09:59Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![val](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/val/32/138203_2.png) [@val](https://discuss.elastic.co/u/val)\
**Post date:** [January 24, 2018, 5:49am UTC](https://discuss.elastic.co/t/unable-to-loop-through-array-field-es-6-1/116669/6 "2018-01-24T05:49:07Z")

</div>

Hmmm, it might well be [this issue](https://github.com/elastic/elasticsearch/issues/17558) that prevents you from getting access to the source field.  
Glad you figured another solution.

---

<div class="post-metadata">

**Author:** ![Hudson\_Dunice](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/hudson_dunice/32/26839_2.png) [@Hudson\_Dunice](https://discuss.elastic.co/u/Hudson_Dunice)\
**Post date:** [January 24, 2018, 1:23pm UTC](https://discuss.elastic.co/t/unable-to-loop-through-array-field-es-6-1/116669/7 "2018-01-24T13:23:29Z")

</div>

Oh... sad. 😕

Thanks for the support and the link @val .

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [February 21, 2018, 1:23pm UTC](https://discuss.elastic.co/t/unable-to-loop-through-array-field-es-6-1/116669/8 "2018-02-21T13:23:45Z")

</div>

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