# How to check multiple values using if else condition in field and create a new scripted field?

**URL:** https://discuss.elastic.co/t/how-to-check-multiple-values-using-if-else-condition-in-field-and-create-a-new-scripted-field/231967
**Category:** Kibana
**Created:** [May 11, 2020, 9:11am UTC](https://discuss.elastic.co/t/how-to-check-multiple-values-using-if-else-condition-in-field-and-create-a-new-scripted-field/231967 "2020-05-11T09:11:06Z")
**Posts on this page:** 17
**Page:** 1

<div class="post-metadata">

### Author: ![Emna1](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/emna1/32/64354_2.png) [@Emna1](https://discuss.elastic.co/u/Emna1)
#### Post date: [May 11, 2020, 9:11am UTC](https://discuss.elastic.co/t/how-to-check-multiple-values-using-if-else-condition-in-field-and-create-a-new-scripted-field/231967/1 "2020-05-11T09:11:06Z")

</div>

Hi, i want to use this code or transform it in kibana in scripted field ?how can i do that, any help please?

> Blockquote  
> filter {  
> if [Duration] \>= 360 {  
> mutate {  
> add\_field =\> { "MT" =\> "50" }  
> }  
> } else if [Duration] \< 360 and [Duration] \>= 100 {  
> mutate {  
> add\_field =\> { "MT" =\> "25" }  
> }  
> } else {  
> mutate {  
> add\_field =\> { "MT" =\> "15" }  
> }  
> }  
> }

> Blockquote

---

<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: [May 11, 2020, 5:26pm UTC](https://discuss.elastic.co/t/how-to-check-multiple-values-using-if-else-condition-in-field-and-create-a-new-scripted-field/231967/2 "2020-05-11T17:26:54Z")

</div>

You can check for doc["duration"].value with basic if/else and use "return 50" or 25 or 15 based on the case.

---

<div class="post-metadata">

### Author: ![Emna1](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/emna1/32/64354_2.png) [@Emna1](https://discuss.elastic.co/u/Emna1)
#### Post date: [May 12, 2020, 12:12pm UTC](https://discuss.elastic.co/t/how-to-check-multiple-values-using-if-else-condition-in-field-and-create-a-new-scripted-field/231967/3 "2020-05-12T12:12:33Z")

</div>

hi,i try it but it doesn't work

> Blockquote

if doc['Duration'].value \>= 360 {  
return 50 ;

} else if doc['Duration'].value \< 360 and doc['Duration'].value \>= 100 {  
return 25;

} else if doc['Duration'].value \< 100 and doc['Duration'].value \>= 50 {  
return 15 ;

} else {  
return 0 ;  
}

---

<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: [May 12, 2020, 12:50pm UTC](https://discuss.elastic.co/t/how-to-check-multiple-values-using-if-else-condition-in-field-and-create-a-new-scripted-field/231967/4 "2020-05-12T12:50:48Z")

</div>

you need to use && instead of `and`.

---

<div class="post-metadata">

### Author: ![Emna1](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/emna1/32/64354_2.png) [@Emna1](https://discuss.elastic.co/u/Emna1)
#### Post date: [May 12, 2020, 4:04pm UTC](https://discuss.elastic.co/t/how-to-check-multiple-values-using-if-else-condition-in-field-and-create-a-new-scripted-field/231967/5 "2020-05-12T16:04:19Z")

</div>

i change and per && but it doesn't work!!

---

<div class="post-metadata">

### Author: ![flash1293](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/flash1293/32/41227_2.png) [@flash1293](https://discuss.elastic.co/u/flash1293)
#### Post date: [May 12, 2020, 4:11pm UTC](https://discuss.elastic.co/t/how-to-check-multiple-values-using-if-else-condition-in-field-and-create-a-new-scripted-field/231967/6 "2020-05-12T16:11:21Z")

</div>

It looks like you also need parentheses around the conditions:

```auto
if (doc['Duration'].value >= 360) {
return 50 ;

} else (if doc['Duration'].value < 360 && doc['Duration'].value >= 100) {
return 25;

} else if (doc['Duration'].value < 100 && doc['Duration'].value >= 50) {
return 15 ;
} else {
return 0 ;
}

```

---

<div class="post-metadata">

### Author: ![Emna1](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/emna1/32/64354_2.png) [@Emna1](https://discuss.elastic.co/u/Emna1)
#### Post date: [May 13, 2020, 7:32am UTC](https://discuss.elastic.co/t/how-to-check-multiple-values-using-if-else-condition-in-field-and-create-a-new-scripted-field/231967/7 "2020-05-13T07:32:28Z")

</div>

Hi, i try that also but the scripted field doesn't created 😔

---

<div class="post-metadata">

### Author: ![flash1293](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/flash1293/32/41227_2.png) [@flash1293](https://discuss.elastic.co/u/flash1293)
#### Post date: [May 13, 2020, 9:03am UTC](https://discuss.elastic.co/t/how-to-check-multiple-values-using-if-else-condition-in-field-and-create-a-new-scripted-field/231967/8 "2020-05-13T09:03:09Z")

</div>

Could you be more specific? What exactly did you try and what exactly happens?

---

<div class="post-metadata">

### Author: ![Emna1](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/emna1/32/64354_2.png) [@Emna1](https://discuss.elastic.co/u/Emna1)
#### Post date: [May 13, 2020, 10:04am UTC](https://discuss.elastic.co/t/how-to-check-multiple-values-using-if-else-condition-in-field-and-create-a-new-scripted-field/231967/9 "2020-05-13T10:04:52Z")

</div>

I try this code;

> Blockquote

if( doc['Duration'].value \>= 360){  
return 50 ;

} else if (doc['Duration'].value \< 360 && doc['Duration'].value \>= 100) {  
return 25;

} else if ( doc['Duration].value \< 100 && doc['Duration'].value \>= 50 ) {  
return 15 ;

}else if ( doc['Duration'].value \< 50 && doc['Duration'].value \>= 10) {  
return 5;

```
} else {
return 0;

```

## }

Script is invalid. View script preview for details

---

<div class="post-metadata">

### Author: ![flash1293](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/flash1293/32/41227_2.png) [@flash1293](https://discuss.elastic.co/u/flash1293)
#### Post date: [May 13, 2020, 11:26am UTC](https://discuss.elastic.co/t/how-to-check-multiple-values-using-if-else-condition-in-field-and-create-a-new-scripted-field/231967/10 "2020-05-13T11:26:59Z")

</div>

If you are viewing the script preview, what does it tell you?

---

<div class="post-metadata">

### Author: ![Emna1](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/emna1/32/64354_2.png) [@Emna1](https://discuss.elastic.co/u/Emna1)
#### Post date: [May 13, 2020, 12:25pm UTC](https://discuss.elastic.co/t/how-to-check-multiple-values-using-if-else-condition-in-field-and-create-a-new-scripted-field/231967/11 "2020-05-13T12:25:39Z")

</div>

i didn't interstand , i add a scripted field in index and i didn' change anything;  
type : number  
format: default  
Popularity : 0  
i add the script and when i clic in button create field

> Blockquote

Script is invalid. View script preview for details

Access fields with `doc['some_field'].value` .

Get help with the syntax and preview the results of your script.

---

<div class="post-metadata">

### Author: ![flash1293](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/flash1293/32/41227_2.png) [@flash1293](https://discuss.elastic.co/u/flash1293)
#### Post date: [May 13, 2020, 1:17pm UTC](https://discuss.elastic.co/t/how-to-check-multiple-values-using-if-else-condition-in-field-and-create-a-new-scripted-field/231967/12 "2020-05-13T13:17:01Z")

</div>

"Get help with the syntax and preview the results of your script" is a link. If you click it, it will open a flyout with two tabs (Syntax and "Preview results"). Click "Preview results" and an error message should be shown.

---

<div class="post-metadata">

### Author: ![Emna1](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/emna1/32/64354_2.png) [@Emna1](https://discuss.elastic.co/u/Emna1)
#### Post date: [May 13, 2020, 2:53pm UTC](https://discuss.elastic.co/t/how-to-check-multiple-values-using-if-else-condition-in-field-and-create-a-new-scripted-field/231967/13 "2020-05-13T14:53:15Z")

</div>

okay, first i want to thank you because when i try the script again it works, then just i want to know please when i change the field with code (type ; string)

> Blockquote  
> if( doc['code'].value == '1' ){  
> return 10 ;  
> } else if (doc['code'].value == '2') {  
> return50;  
> }

> Blockquote  
> The error is like that

> Blockquote  
> .......  
> org.elasticsearch.search.lookup.LeafDocLookup.get(LeafDocLookup.java:98)",  
> "org.elasticsearch.search.lookup.LeafDocLookup.get(LeafDocLookup.java:41)",  
> "if( doc['code'].value == '1' ){\r\n \r\n ",  
> " ^---- HERE"  
> ],  
> "script": "if( doc['code'].value == '1' ){\r\n \r\n return 10 ;\r\n \r\n } else if (doc['code'].value == '2') {\r\n \r\n return 50;\r\n \r\n }",  
> "lang": "painless",  
> "caused\_by": {  
> "type": "illegal\_argument\_exception",  
> "reason": "Fielddata is disabled on text fields by default. Set fielddata=true on [code] in order to load fielddata in memory by uninverting the inverted index. Note that this can however use significant memory. Alternatively use a keyword field instead."  
> }  
> }  
> }  
> ]  
> }

---

<div class="post-metadata">

### Author: ![flash1293](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/flash1293/32/41227_2.png) [@flash1293](https://discuss.elastic.co/u/flash1293)
#### Post date: [May 13, 2020, 4:21pm UTC](https://discuss.elastic.co/t/how-to-check-multiple-values-using-if-else-condition-in-field-and-create-a-new-scripted-field/231967/14 "2020-05-13T16:21:56Z")

</div>

The "reason" field of the error gives it away:

`Fielddata is disabled on text fields by default. Set fielddata=true on [code] in order to load fielddata in memory by uninverting the inverted index. Note that this can however use significant memory. Alternatively use a keyword field instead`

If you are using the default mapping for your index, `code.keyword` instead of `code` should fix the problem.

---

<div class="post-metadata">

### Author: ![Emna1](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/emna1/32/64354_2.png) [@Emna1](https://discuss.elastic.co/u/Emna1)
#### Post date: [May 13, 2020, 4:36pm UTC](https://discuss.elastic.co/t/how-to-check-multiple-values-using-if-else-condition-in-field-and-create-a-new-scripted-field/231967/15 "2020-05-13T16:36:19Z")

</div>

@flash1293 ,thank you so much for help

---

<div class="post-metadata">

### Author: ![Emna1](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/emna1/32/64354_2.png) [@Emna1](https://discuss.elastic.co/u/Emna1)
#### Post date: [May 14, 2020, 2:51pm UTC](https://discuss.elastic.co/t/how-to-check-multiple-values-using-if-else-condition-in-field-and-create-a-new-scripted-field/231967/16 "2020-05-14T14:51:42Z")

</div>

Hi, please can you tell me what does it mean this error ?

> Blockquote  
> failed","phase":"query","grouped":true,"failed\_shards":[{"shard":0,"index":"test","node":"LHk548iBS6u1OZMSNqZDMg","reason":{"type":"aggregation\_execution\_exception","reason":"Unsupported script value , expected a number, date, or boolean"}}]},"status":500}

---

<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: [June 11, 2020, 2:51pm UTC](https://discuss.elastic.co/t/how-to-check-multiple-values-using-if-else-condition-in-field-and-create-a-new-scripted-field/231967/17 "2020-06-11T14:51:43Z")

</div>

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