# Scripted field - script doesn't runs all the keys in my document

**URL:** <https://discuss.elastic.co/t/scripted-field-script-doesnt-runs-all-the-keys-in-my-document/355585>\
**Category:** Kibana\
**Created:** [March 18, 2024, 9:43am UTC](https://discuss.elastic.co/t/scripted-field-script-doesnt-runs-all-the-keys-in-my-document/355585 "2024-03-18T09:43:40Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![JenniferL](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jenniferl/32/132732_2.png) [@JenniferL](https://discuss.elastic.co/u/JenniferL)\
**Post date:** [March 18, 2024, 9:43am UTC](https://discuss.elastic.co/t/scripted-field-script-doesnt-runs-all-the-keys-in-my-document/355585/1 "2024-03-18T09:43:40Z")

</div>

Hey All! I'm new here, I hope I will be clear on my request 🤓

So I'm creating a scripted field on my index Pattern. I have multiple documents and in each documents there are multiple keys.

What I want is: if the key's value, for the first key, is different from "Conforme", the script should gives me this return:"Esthetique", and if the value is "Conforme" the return is "null". Then, whatever was the result for the first key, I want the script runs also the second key, and same: if the value is different from "Conforme" the script should returns "Erreur non observee en BQ precedente", otherwise, return "null".

Unfortunately in my case when the script is running a document, if the first key's value is different from "Conforme", the script returns "Esthetique" and then stopped here, it doesn't runs the second key.

I can't find where is my error.

This is the script:

```auto
def esthetiqueValue = null;
if (doc.containsKey('2_categorical.E5|SE2|esthétique.keyword') && doc['2_categorical.E5|SE2|esthétique.keyword'].size() > 0) {
    esthetiqueValue = doc['2_categorical.E5|SE2|esthétique.keyword'].value;
    if (esthetiqueValue != null && esthetiqueValue != 'Conforme') {
        return 'Esthetique';
    }
}

def erreurBqPrecedenteValue = null;
if (doc.containsKey('2_categorical.E5|SE2|erreur non observée en bq précédente.keyword') && doc['2_categorical.E5|SE2|erreur non observée en bq précédente.keyword'].size() > 0) {
    erreurBqPrecedenteValue = doc['2_categorical.E5|SE2|erreur non observée en bq précédente.keyword'].value;
    if (erreurBqPrecedenteValue != null && erreurBqPrecedenteValue != 'Conforme') {
        return 'Erreur non observee en BQ precedente';
    }
}

return null;

```

Thanks in advance if you have any suggestion 😊

---

<div class="post-metadata">

**Author:** ![piotrprz](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/piotrprz/32/128141_2.png) [@piotrprz](https://discuss.elastic.co/u/piotrprz)\
**Post date:** [March 18, 2024, 10:11am UTC](https://discuss.elastic.co/t/scripted-field-script-doesnt-runs-all-the-keys-in-my-document/355585/2 "2024-03-18T10:11:36Z")

</div>

Hmmm... I'd say this behaviour is exactly encoded in your logic.  
Using keyword `return` stops any further processing, just like in any programming language which has `return`: this stops execution of function/method.

Could you also submit a document for which you expect this work differently?

---

<div class="post-metadata">

**Author:** ![JenniferL](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jenniferl/32/132732_2.png) [@JenniferL](https://discuss.elastic.co/u/JenniferL)\
**Post date:** [March 18, 2024, 10:24am UTC](https://discuss.elastic.co/t/scripted-field-script-doesnt-runs-all-the-keys-in-my-document/355585/3 "2024-03-18T10:24:13Z")

</div>

The full document is very heavy, but I can share just the part which is about the script:

 ![Capture1](https://us1.discourse-cdn.com/elastic/original/3X/b/d/bd2e59bed543291774f9c793eecc84d47d88e80f.png)

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [March 18, 2024, 11:15am UTC](https://discuss.elastic.co/t/scripted-field-script-doesnt-runs-all-the-keys-in-my-document/355585/4 "2024-03-18T11:15:28Z")

</div>

Bonjour Jennifer ! 😉

Could you provide a full recreation script as described in [About the Elasticsearch category](https://discuss.elastic.co/t/about-the-elasticsearch-category/21). It will help to better understand what you are doing. Please, try to keep the example as simple as possible.

A full reproduction script is something anyone can **copy and paste in Kibana dev console** , click on the run button to reproduce your use case. It will help readers to understand, reproduce and if needed fix your problem. It will also most likely help to get a faster answer.

Have a look at the [Elastic Stack and Solutions Help · Forums and Slack | Elastic](https://elastic.co/community/help) page. It contains also lot of useful information on how to ask for help.

---

<div class="post-metadata">

**Author:** ![JenniferL](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jenniferl/32/132732_2.png) [@JenniferL](https://discuss.elastic.co/u/JenniferL)\
**Post date:** [March 18, 2024, 12:43pm UTC](https://discuss.elastic.co/t/scripted-field-script-doesnt-runs-all-the-keys-in-my-document/355585/5 "2024-03-18T12:43:19Z")

</div>

Bonjour David 😊

Ok so I'm gonna try to right it in a simple way:

```auto
// My document:

{
  "Weather_Paris": ["Cloudy"],
  "Weather_Lyon": ["Rainy"]
}

```

My script:

```auto
def WeatherParisValue = null;
if (doc.containsKey('Weather_Paris') && doc['Weather_Paris'].size() > 0) {
    WeatherParisValue = doc['Weather_Paris'].value;
    if (WeatherParisValue != null && WeatherParisValue != 'Sun') {
        return 'OtherParis';
    }
}

def WeatherLyonValue = null;
if (doc.containsKey('Weather_Lyon') && doc['Weather_Lyon'].size() > 0) {
    WeatherLyonValue = Weather_Lyon'].value;
    if (WeatherLyonValue != null && WeatherLyonValue != 'Sun') {
        return 'OtherLyon';
    }
}

return null;

```

I would like to have as an answer for this document: "OtherParis, OtherLyon"  
but instead I juste have "OtherParis".

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [March 18, 2024, 1:01pm UTC](https://discuss.elastic.co/t/scripted-field-script-doesnt-runs-all-the-keys-in-my-document/355585/6 "2024-03-18T13:01:25Z")

</div>

What would help a lot would be to have a simple script we can copy and paste in the Kibana dev console.  
Then we can iterate from there.

Could you provide that please?

Merci

---

<div class="post-metadata">

**Author:** ![JenniferL](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jenniferl/32/132732_2.png) [@JenniferL](https://discuss.elastic.co/u/JenniferL)\
**Post date:** [March 18, 2024, 3:21pm UTC](https://discuss.elastic.co/t/scripted-field-script-doesnt-runs-all-the-keys-in-my-document/355585/7 "2024-03-18T15:21:07Z")

</div>

I finally found the solution by myself: I had to initialize a variable `def result = []` as an empty list to store the multiple values from the different keys and then I put the keyword `return` only at the end of my script. Thanks anyway 🙂

---

<div class="post-metadata">

**Author:** ![piotrprz](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/piotrprz/32/128141_2.png) [@piotrprz](https://discuss.elastic.co/u/piotrprz)\
**Post date:** [March 19, 2024, 8:55am UTC](https://discuss.elastic.co/t/scripted-field-script-doesnt-runs-all-the-keys-in-my-document/355585/8 "2024-03-19T08:55:28Z")

</div>

Yes, because `return` stops processing immediately 😉

---

<div class="post-metadata">

**Author:** ![JenniferL](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jenniferl/32/132732_2.png) [@JenniferL](https://discuss.elastic.co/u/JenniferL)\
**Post date:** [March 22, 2024, 7:53am UTC](https://discuss.elastic.co/t/scripted-field-script-doesnt-runs-all-the-keys-in-my-document/355585/9 "2024-03-22T07:53:42Z")

</div>

I didn't know 🤦🏻‍♀️ but now I know! thanks 🙃

---

<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 19, 2024, 7:53am UTC](https://discuss.elastic.co/t/scripted-field-script-doesnt-runs-all-the-keys-in-my-document/355585/10 "2024-04-19T07:53:51Z")

</div>

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