Hi,
I'm having some trouble in creating a specific elasticsearch query DSL for a Kibana Dashboard. I need to filter data by created date and time. I apllied the solution bellow so we can bring data to our scope, we call it as "solve today", bringing some data from yesterday and a period from today to this context.
{
"query": {
"range": {
"data.created": {
"gte": "now-1d/d+10h",
"lt": "now/d+10h"
}
}
}
}
It works fine from tuesday to friday but we need a diferent rule for the monday, something like the code bellow.
{
"query": {
"range": {
"data.created": {
"gte": "now-3d/d+10h",
"lt": "now/d+10h"
}
}
}
}
There is anyway I could do this automatically? I tried some scripts but it doesn't work fine with date ranges.
O created a scripted field bellow and it solved the problem. Now I'm just using it as a filter on Kibana dash where it is true.
int created_hour = doc['data.created'].value.getHour();
int created_day_of_week = doc['data.created'].value.getDayOfWeek();
ZonedDateTime currentdate = ZonedDateTime.ofInstant(Instant.ofEpochMilli(new Date().getTime()), ZoneId.of('GMT'));
ZonedDateTime solve_limit = doc['data.created'].value.withHour(19).withMinute(0).withSecond(0).withNano(0);
if(created_day_of_week == 6){
solve_limit = solve_limit.plusDays(2)
}
else if(created_day_of_week == 7){
solve_limit = solve_limit.plusDays(1)
}
else {
if(created_hour >= 18){
if(created_day_of_week == 5) {
solve_limit = solve_limit.plusDays(3)
}
else {
solve_limit = solve_limit.plusDays(1)
}
}
}
long solve_limit_millis = solve_limit.truncatedTo(ChronoUnit.DAYS).toInstant().toEpochMilli();
long currentdate_millis = currentdate.truncatedTo(ChronoUnit.DAYS).toInstant().toEpochMilli();
long diff = solve_limit_millis - currentdate_millis;
diff == 0