# Kibana scripted fields

**URL:** <https://discuss.elastic.co/t/kibana-scripted-fields/262739>\
**Category:** Kibana\
**Tags:** painless\
**Created:** [January 31, 2021, 6:20am UTC](https://discuss.elastic.co/t/kibana-scripted-fields/262739 "2021-01-31T06:20:18Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![Gokul6](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/gokul6/32/84049_2.png) [@Gokul6](https://discuss.elastic.co/u/Gokul6)\
**Post date:** [January 31, 2021, 6:20am UTC](https://discuss.elastic.co/t/kibana-scripted-fields/262739/1 "2021-01-31T06:20:18Z")

</div>

**def dayOfWeek = ["", "Mon", "Tue", "Wed", "Thu", "fri", "Sat", "Sunday"]**  
**doc['@timestamp'].value.dayOfWeek;**  
the above code will return **sunday** and the value will be stored in a variable called **dayOfWeek**  
in my data i have several fields like **weekDays.monday.is24HourService** and i want to apply an **if-else statement** like below  
**if(weekDays.[dayOfWeek].is24HourService == "true"){**  
**return "true"**  
**}else{**  
**return "false"**  
**}**  
is it possible to do this using scripted fields ?? please help 🙂

---

<div class="post-metadata">

**Author:** ![aaron-nimocks](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/aaron-nimocks/32/73965_2.png) [@aaron-nimocks](https://discuss.elastic.co/u/aaron-nimocks)\
**Post date:** [February 1, 2021, 1:34am UTC](https://discuss.elastic.co/t/kibana-scripted-fields/262739/2 "2021-02-01T01:34:42Z")

</div>

Can you try to go through this question again.

1. What is the scripted field name?
2. What is the scripted field type? (bool?)
3. What is the source data field you need to to make your new field value?
4. What functionality is required to produce the new field value?

Just trying to get all the related information and then can provide you with a solution. Thanks.

---

<div class="post-metadata">

**Author:** ![Gokul6](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/gokul6/32/84049_2.png) [@Gokul6](https://discuss.elastic.co/u/Gokul6)\
**Post date:** [February 1, 2021, 2:34am UTC](https://discuss.elastic.co/t/kibana-scripted-fields/262739/3 "2021-02-01T02:34:04Z")

</div>

1. scripted field name - test
2. scripted field type - string
3. source field - weekDays.monday.is24HourService (we have a field like this for everyday) all these source field contain Boolean values.
4. I want to apply an if-else statement and check whether the field is true or false.  
**if(weekDays.[dayOfWeek].is24HourService == "true"){**  
**return "true"**  
**}else{**  
**return "false"**  
**}**

---

<div class="post-metadata">

**Author:** ![Gokul6](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/gokul6/32/84049_2.png) [@Gokul6](https://discuss.elastic.co/u/Gokul6)\
**Post date:** [February 1, 2021, 2:41am UTC](https://discuss.elastic.co/t/kibana-scripted-fields/262739/4 "2021-02-01T02:41:38Z")

</div>

The idea is instead of applying the if-else statement for everyday like this **if(weekDays.monday.is24HourService == "true"**)  
**if(weekDays.tuesday.is24HourService == "true"**)  
I just wanted to replace the output of the variable dayOfWeek with the day(monday, tuesday, etc..) present in my data and then iterate. @aaron-nimocks please help 🙂

---

<div class="post-metadata">

**Author:** ![aaron-nimocks](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/aaron-nimocks/32/73965_2.png) [@aaron-nimocks](https://discuss.elastic.co/u/aaron-nimocks)\
**Post date:** [February 1, 2021, 1:27pm UTC](https://discuss.elastic.co/t/kibana-scripted-fields/262739/5 "2021-02-01T13:27:26Z")

</div>

Sorry I am still not sure what you are looking to do but here is something that might be close.

I'm still confused on the return. You mention `true` and `false` but also the `day of the week`.

```auto
if(doc['weekDays.monday.is24HourService'].value == "true" && doc['dayOfWeek'].value == 1) { return "Monday" }
if(doc['weekDays.tuesday.is24HourService'].value == "true" && doc['dayOfWeek'].value == 2) { return "Tuesday" }
if(doc['weekDays.wednesday.is24HourService'].value == "true" && doc['dayOfWeek'].value == 3) { return "Wednesday" }
if(doc['weekDays.thursday.is24HourService'].value == "true" && doc['dayOfWeek'].value == 4) { return "Thursday" }
if(doc['weekDays.friday.is24HourService'].value == "true" && doc['dayOfWeek'].value == 5) { return "Friday" }
if(doc['weekDays.saturday.is24HourService'].value == "true" && doc['dayOfWeek'].value == 6) { return "Saturday" }
if(doc['weekDays.sunday.is24HourService'].value == "true" && doc['dayOfWeek'].value == 7) { return "Sunday" }

```

---

<div class="post-metadata">

**Author:** ![Gokul6](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/gokul6/32/84049_2.png) [@Gokul6](https://discuss.elastic.co/u/Gokul6)\
**Post date:** [February 1, 2021, 1:50pm UTC](https://discuss.elastic.co/t/kibana-scripted-fields/262739/6 "2021-02-01T13:50:16Z")

</div>

sir instead of writing these much if statements for everyday, i want to fetch current **dayOfWeek** using the below code and replace it with the word "monday" present in my data and find the solution for that day alone.  
**def dayOfWeek = ["", "Mon", "Tue", "Wed", "Thu", "fri", "Sat", "Sunday"]**  
**doc['@timestamp'].value.dayOfWeek;**  
i am sorry if i have confused you again. thanks for your time and help 😊

---

<div class="post-metadata">

**Author:** ![Gokul6](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/gokul6/32/84049_2.png) [@Gokul6](https://discuss.elastic.co/u/Gokul6)\
**Post date:** [February 1, 2021, 1:56pm UTC](https://discuss.elastic.co/t/kibana-scripted-fields/262739/7 "2021-02-01T13:56:19Z")

</div>

below is my data  
 ![elastic](https://us1.discourse-cdn.com/elastic/original/3X/7/2/72d74241ec3bd580f94747e764e55a14127c9911.png)

---

<div class="post-metadata">

**Author:** ![aaron-nimocks](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/aaron-nimocks/32/73965_2.png) [@aaron-nimocks](https://discuss.elastic.co/u/aaron-nimocks)\
**Post date:** [February 1, 2021, 9:29pm UTC](https://discuss.elastic.co/t/kibana-scripted-fields/262739/8 "2021-02-01T21:29:14Z")

</div>

I am not sure how to do this or if it's possible, sorry. Think you are trying to dynamically set field names and every test I've tried so far it isn't working.

---

<div class="post-metadata">

**Author:** ![Gokul6](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/gokul6/32/84049_2.png) [@Gokul6](https://discuss.elastic.co/u/Gokul6)\
**Post date:** [February 2, 2021, 6:04am UTC](https://discuss.elastic.co/t/kibana-scripted-fields/262739/9 "2021-02-02T06:04:51Z")

</div>

thank you for your valuable time and help @aaron-nimocks 😊 i did it and it's working fine for me, below is my code.

Map weekDaysMap = new HashMap();  
weekDaysMap.put(1, "monday");  
weekDaysMap.put(2, "tuesday");  
weekDaysMap.put(3, "wednesday");  
weekDaysMap.put(4, "thursday");  
weekDaysMap.put(5, "friday");  
weekDaysMap.put(6, "saturday");  
weekDaysMap.put(7, "sunday");

def weekDayIndex = doc['@timestamp'].value.dayOfWeek;

def weekDay = weekDaysMap.get(weekDayIndex);

if(doc["weekDays."+weekDay+".is24HourService"].value== true){  
return "true";  
}  
else{  
return "false";  
}

---

<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:** [March 2, 2021, 6:05am UTC](https://discuss.elastic.co/t/kibana-scripted-fields/262739/10 "2021-03-02T06:05:12Z")

</div>

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