如何在关键字字段Elasticsearch查询6.4.2上执行反匹配模式

时间:2019-06-14 23:41:34

标签: elasticsearch kibana

问题:

我们的日志数据包含27-34百万个/ event-heartbeat条目。 我需要过滤掉那些条目,以便仅在Kibana中查看可行的日志消息。

将Kibana过滤器与通配符一起使用不起作用。因此,我认为我必须在6.4.2版Elasticsearch中编写QueryDSL才能做到,以使其能够过滤掉事件心跳。

我一直在寻找,并且找不到关于如何进行反模式匹配的好的解释,以便搜索消息中没有/ event-heartbeat的所有条目。

以下是日志消息:

@timestamp:
    June 14th 2019, 12:39:09.225
host.name:
    iislogs-production
source:
    C:\inetpub\logs\LogFiles\W3SVC5\u_ex19061412.log
offset:
    83,944,181
message:
    2019-06-14 19:39:06 0.0.0.0 GET /event-heartbeat id=Budrug2UDw 443 - 0.0.0.0 - - 200 0 0 31
prospector.type:
    log
input.type:
    log
beat.name:
    iislogs-production
beat.hostname:
    MYHOSTNAME
beat.version:
    6.4.2
_id:
    yg6AV2sB0_n
_type:
    doc
_index:
    iislogs-production-6.4.2-2019.06.14
_score:
    - 

消息是一个关键字字段,因此我可以轻松编写脚本。

我使用了Lucene语法

NOT message: "*/event-heartbeat*"

This is the anti pattern the kibana filter generates.
{
  "query": {
    "bool": {
      "should": [
        {
          "match_phrase": {
            "message": "*event-heartbeat*"
          }
        }
      ],
      "minimum_should_match": 1
    }
  }
}

我已经在下面通过拥抱尝试了建议的解决方案。我还根据他的评论调整了查询​​,并尝试了两种方法。我用单词word而不是match进行调整,并且尝试了两种方法,因为从技术上讲该字段是关键字,所以我可以轻松编写脚本。该查询仍然返回事件心跳日志条目。

enter image description here

enter image description here

这是我从以下建议的解决方案中尝试的两个查询:

GET /iislogs-production-*/_search
{
   "query":{
      "bool":{
         "must":{
            "match_all":{

            }
         },
         "filter":{
            "bool":{
               "must_not":[
                  {
                     "term":{
                        "message.whitespace":"event-heartbeat"
                     }
                  }
               ]
            }
         }
      }
   }
}

GET /iislogs-production-*/_search
{
   "query":{
      "bool":{
         "must":{
            "match_all":{

            }
         },
         "filter":{
            "bool":{
               "must_not":[
                  {
                     "match":{
                        "message.whitespace":"event-heartbeat"
                     }
                  }
               ]
            }
         }
      }
   }
}

索引映射: https://gist.github.com/zukeru/907a9b2fa2f0d6f91a532b0865131988

1 个答案:

答案 0 :(得分:0)

您是否考虑过“ must_not”布尔查询? 由于您会使用整个集合,而不是真正在意相关函数的形状,因此,我建议使用过滤器而不是查询。您将获得更好的性能。

{
   "query":{
      "bool":{
         "must":{
            "match_all":{

            }
         },
         "filter":{
            "bool":{
               "must_not":[
                  {
                     "match":{
                        "message.whitespace":"event-heartbeat"
                     }
                  }
               ]
            }
         }
      }
   }
}

此示例假设您要查询文本字段,因此使用“匹配”查询而不是“术语”查询。 您还需要确保根据您的目标对字段进行了分析(真正地标记化)。如果您使用的是简单的甚至是标准的分析器,那么查询词中会出现破折号的事实会产生问题。 Elasticsearch会用两个词来打破这个词。您可以尝试在那一台上使用空白分析器,或从查询中删除破折号。

相关问题