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

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

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:

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 :blush:

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?

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

Bonjour Jennifer ! :wink:

Could you provide a full recreation script as described in About the Elasticsearch category. 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 page. It contains also lot of useful information on how to ask for help.

Bonjour David :blush:

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

// My document:

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

My script:

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".

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?


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 :slightly_smiling_face:

1 Like

Yes, because return stops processing immediately :wink:

1 Like

I didn't know :woman_facepalming:t2: but now I know! thanks :upside_down_face: