# Error in scripted field, working with arrays of objects

**URL:** <https://discuss.elastic.co/t/error-in-scripted-field-working-with-arrays-of-objects/265944>\
**Category:** Kibana\
**Created:** [March 2, 2021, 11:33am UTC](https://discuss.elastic.co/t/error-in-scripted-field-working-with-arrays-of-objects/265944 "2021-03-02T11:33:42Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Casper\_Thrane](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/casper_thrane/32/49777_2.png) [@Casper\_Thrane](https://discuss.elastic.co/u/Casper_Thrane)\
**Post date:** [March 2, 2021, 11:33am UTC](https://discuss.elastic.co/t/error-in-scripted-field-working-with-arrays-of-objects/265944/1 "2021-03-02T11:33:42Z")

</div>

Hi

I think I have found an error, in the way Kibana/Scripted Fields handles arrays of objects. My example is this.

My ES Data:

```
{"@timestamp":"2021-02-28T22:59:38.786Z", "message":"ExclusionsDeparturesArrivalsExeption: Flights from CPH to ISB is not allowed.","additional":{"searchQuery":{"flights":[{"departure":"CPH","arrival":"ISB","departureDateTime":"30.03.2021 07:00:00"},{"departure":"ISB","arrival":"CPH","departureDateTime":"06.04.2021 07:00:00"}]}}}
{"@timestamp":"2021-02-28T22:59:42.761Z", "message":"ExclusionsDeparturesArrivalsExeption: Flights from CPH to RIX is not allowed.","additional":{"searchQuery":{"flights":[{"departure":"CPH","arrival":"RIX","departureDateTime":"04.03.2021 08:00:00"},{"departure":"RIX","arrival":"CPH","departureDateTime":"06.05.2021 07:00:00"}]}}}

```

If I create a scripted fields like this:

```
if (!doc.containsKey("additional.searchQuery.flights.departure") || doc["additional.searchQuery.flights.departure"].empty) {
    return null;
}

def tmp = params._source["additional"].searchQuery.flights.get(0);

tmp.departure + "->" + tmp.arrival;

```

It will return the correct values.

```
"CPH->RIX"
"CPH->ISB"

```

The problem with this, is I cannot use it as a filter in a dashboard, because of params doesn't contain \_source when used as filter.

If I do this instead, which in my world should give the same result:

```
if (!doc.containsKey("additional.searchQuery.flights.departure") || doc["additional.searchQuery.flights.departure"].empty) {
    return null;
}

doc["additional.searchQuery.flights.departure"][0] + "->" + doc["additional.searchQuery.flights.arrival"][0]

```

I get this result:

```
"CPH->CPH"
"CPH->CPH"

```

If i use arrival[1] instead I get the correct result. That must be an error?

Another problem with this, is if i use it as a filter, I will still get results containing parts of the string from the scripted field. It doesn't make a keyword match, it is more like a text match.

Br  
Casper

---

<div class="post-metadata">

**Author:** ![Marius\_Dragomir](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/marius_dragomir/32/42087_2.png) [@Marius\_Dragomir](https://discuss.elastic.co/u/Marius_Dragomir)\
**Post date:** [March 5, 2021, 12:48pm UTC](https://discuss.elastic.co/t/error-in-scripted-field-working-with-arrays-of-objects/265944/2 "2021-03-05T12:48:07Z")

</div>

Do you have your data saved as an array or as nested data type?

---

<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:** [April 2, 2021, 12:48pm UTC](https://discuss.elastic.co/t/error-in-scripted-field-working-with-arrays-of-objects/265944/3 "2021-04-02T12:48:27Z")

</div>

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