I am able to extract hour of day using the below script format. But facing the issue while extracting the day of week because of timezone.
I used this script.
["","Mon","Tue","Wed","Thu","Fri","Sat","Sun"]doc['event_time'].value.dayOfWeek
You can see the July 23 extract Fri and Thu as a day of week.
So please guide how to resolve this issue? Thank you
Continuing the discussion from Hour Scripted Field:
This is done in Painless Lab so will need to change for a scripted field.
# Get the date and store in ZonedDateTime variable.
ZonedDateTime zdt = ZonedDateTime.parse(params.date_field);
# Set the pattern which is day of week. See documentation below.
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("E");
# Extract the day of the week and save as a string.
String day_of_week = zdt.format(formatter);
# Output the day of week.
return day_of_week;
Input
"date_field": "2021-06-12T09:28:48+00:00"
Output
Sat
Documentation
how to do in scripted field ?
The below should work. Just replace with your field name. The only thing that changes is where you are getting the data from.
# Get the date and store in ZonedDateTime variable.
ZonedDateTime zdt = ZonedDateTime.parse(doc['YOUR-FIELD-NAME'].value);
# Set the pattern which is day of week. See documentation below.
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("E");
# Extract the day of the week and save as a string.
String day_of_week = zdt.format(formatter);
# Output the day of week.
return day_of_week;
I am getting this error.
{
"root_cause": [
{
"type": "script_exception",
"reason": "runtime error",
"script_stack": [
"java.base/java.lang.Class.cast(Class.java:3818)",
"zdt = ZonedDateTime.parse(doc['fm_data.event_time'].value);\nDateTimeFormatter ",
" ^---- HERE"
],
"script": "ZonedDateTime zdt = ZonedDateTime.parse(doc['fm_data.event_time'].value); ...",
"lang": "painless",
"position": {
"offset": 65,
"start": 14,
"end": 92
}
}
],
"type": "search_phase_execution_exception",
"reason": "all shards failed",
"phase": "query",
"grouped": true,
"failed_shards": [
{
"shard": 0,
"index": "4g-radio-fm",
"node": "n-2CcZomQIWiklu2KWP19g",
"reason": {
"type": "script_exception",
"reason": "runtime error",
"script_stack": [
"java.base/java.lang.Class.cast(Class.java:3818)",
"zdt = ZonedDateTime.parse(doc['fm_data.event_time'].value);\nDateTimeFormatter ",
" ^---- HERE"
],
"script": "ZonedDateTime zdt = ZonedDateTime.parse(doc['fm_data.event_time'].value); ...",
"lang": "painless",
"position": {
"offset": 65,
"start": 14,
"end": 92
},
"caused_by": {
"type": "class_cast_exception",
"reason": "Cannot cast org.elasticsearch.script.JodaCompatibleZonedDateTime to java.lang.CharSequence"
}
}
}
]
}
I tried to use this script. This script is given in the documentation.
LocalDateTime.ofInstant(Instant.ofEpochMilli(doc['fm_data.event_time'].value), ZoneId.of('Asia/Kolkata')).getDayOfWeek().getDisplayName(TextStyle.FULL, Locale.getDefault())
Here also I am getting the error. Please see the error below.
{
"root_cause": [
{
"type": "script_exception",
"reason": "runtime error",
"script_stack": [
"LocalDateTime.ofInstant(Instant.ofEpochMilli(doc['fm_data.event_time'].value), ZoneId.of('Asia/Kolkata')).getDayOfWeek().getDisplayName(TextStyle.FULL, Locale.getDefault())\n\n",
" ^---- HERE"
],
"script": " ...",
"lang": "painless",
"position": {
"offset": 71,
"start": 1,
"end": 175
}
}
],
"type": "search_phase_execution_exception",
"reason": "all shards failed",
"phase": "query",
"grouped": true,
"failed_shards": [
{
"shard": 0,
"index": "4g-radio-fm",
"node": "n-2CcZomQIWiklu2KWP19g",
"reason": {
"type": "script_exception",
"reason": "runtime error",
"script_stack": [
"LocalDateTime.ofInstant(Instant.ofEpochMilli(doc['fm_data.event_time'].value), ZoneId.of('Asia/Kolkata')).getDayOfWeek().getDisplayName(TextStyle.FULL, Locale.getDefault())\n\n",
" ^---- HERE"
],
"script": " ...",
"lang": "painless",
"position": {
"offset": 71,
"start": 1,
"end": 175
},
"caused_by": {
"type": "wrong_method_type_exception",
"reason": "cannot convert MethodHandle(Dates)JodaCompatibleZonedDateTime to (Object)long"
}
}
}
]
}
I am able to figure out the solution. This script is perfectly working for me
LocalDateTime.ofInstant(Instant.ofEpochMilli(doc['event_time'].value.millis), ZoneId.of('Asia/Kolkata')).getDayOfWeek().getDisplayName(TextStyle.SHORT, Locale.getDefault())
Thanks