在kibana仪表板中筛选特定日期



我使用的是ELK–7.12.1,在Kibana仪表板中,我需要使用无痛过滤器过滤以下假期日期。

01-Jan-2021
14-Jan-2021
26-Jan-2021
11-Mar-2021
02-Apr-2021
13-Apr-2021
14-May-2021
21-Jul-2021
10-Sep-2021
15-Oct-2021
01-Nov-2021
05-Nov-2021

无痛苦的脚本,我有如下。

Language: painless
Type: string
Format: String
Script:
def month = doc['@timestamp'].value.getMonthValue();
def day = doc['@timestamp'].value.getDayOfMonth();
if (month == 1){
if  ((day == 01) || (day == 14) || (day == 26) || (day == 11) || (day == 02) || (day == 13) || (day == 21) || (day == 10) || (day == 15) || (day == 05))  {
return true
}
else {
return false
}
}

索引模式>我的索引>脚本字段>节假日>预览结果

[
{
"_id": "38009464",
"@timestamp": "2021-02-26T11:11:39.707Z",
"holidays": [
null
]
},
{
"_id": "38026158",
"@timestamp": "2021-02-26T11:11:39.727Z",
"holidays": [
null
]
},
{
"_id": "38030065",
"@timestamp": "2021-02-26T11:11:39.735Z",
"holidays": [
null
]

返回null。那么,我如何修复这个过滤器真(或(假?像这将检查时间戳是否在这些天中的任何一天,如果是,则返回true。然后只需要在仪表板上过滤holiday=False即可。

有人能帮我修一下吗?这会很有帮助的。

您看到的是一个null值,因为您的脚本在不是一月时不会返回任何内容。外部if没有其他对应项。当条件不匹配时会发生什么?

注意:目前,尽管有您的介绍,您的脚本仍在返回:

  • 对于以下日期为true:01.01、14.01、26.01、11.01、02.01、13.01、21.01、10.01、05.01
  • 1月剩余时间为false
  • 无(即,在一年中的其他日子无效

您需要修复您的脚本以覆盖所有情况,而不仅仅是一月份。您可以简单地添加一个else条件,如下所示。

def month = doc['@timestamp'].value.getMonthValue();
def day = doc['@timestamp'].value.getDayOfMonth();
if (month == 1){
if  ((day == 01) || (day == 14) || (day == 26) || (day == 11) || (day == 02) || (day == 13) || (day == 21) || (day == 10) || (day == 15) || (day == 05))  {
return true
}
else {
return false
}
} else {
return false;
}

甚至更好:

def month = doc['@timestamp'].value.getMonthValue();
def day = doc['@timestamp'].value.getDayOfMonth();
if (month == 1){
if  ((day == 01) || (day == 14) || (day == 26) || (day == 11) || (day == 02) || (day == 13) || (day == 21) || (day == 10) || (day == 15) || (day == 05))  {
return true
}
}
// No previous condition matches, return false
return false;

所以你离你正在寻找的解决方案还很远。

要使脚本正常工作,您应该(正确地(涵盖所有情况。给出一个较短的列表:

01-Jan-2021 --> 01.01.2021
14-Jan-2021 --> 14.01.2021
26-Jan-2021 --> 26.01.2021
11-Mar-2021 --> 11.03.2021

if(month == 1) {
// Given is January
if([01, 14, 26].contains(day)) {
return true;
}
} else if (month == 3) {
// Given is March
if (day == 11) {
return true;
}
}
// If the date is not in the blacklist
return false;

显然,在这里我没有涵盖所有的案例(提供了这个答案应该很容易实现(,也没有涵盖今年,但也许你应该考虑一下

谨致问候,Mirko

最新更新