在 kibana 仪表板中过滤特定日期

Filter specfic dates in kibana dashboard

我正在使用 ELK – 7.12.1,在 Kibana 仪表板中,我需要使用 painless 过滤低于假期的日期。

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
    }
}

索引模式 > my-index > 脚本字段 > 假期 > 预览结果

[
 {
  "_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
  ]

它returns null。那么我如何解决这个问题以过滤真(或)假?作为 这将检查时间戳是否在那些日子中的任何一天,如果是 returns 则为真。然后只需要在仪表板上过滤 holiday = False 就是这个主意。

有人可以帮我解决这个问题吗?会有帮助的。

您看到的是空值,因为您的脚本在不是一月时没有 return 任何内容。外部 if 没有对应的 else。当没有条件匹配时会发生什么?

注意:目前,尽管有您的介绍,您的脚本是 returning:

  • 以下日期为真: 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;

显然,这里我没有涵盖所有情况(前提是这个答案应该很容易实现),也没有涵盖年份,但也许你应该考虑一下。

亲切的问候, 米尔科