如何使用正则表达式在 Kibana 查询语言中过滤数据

问题描述 投票:0回答:2

我在 Kibana 中发现一个字段具有以下值 -

D_00122 - A - 14
D_00133A - 15
D_00145 - 18
D_00167 - B - 18
D_00182A - 19
D_00121 - A - 13
D_0011 - 18

我想纠正 KQL 以仅选择具有以下格式的值 -

  • 以“D_”开头,然后是数字(数字后无字符)。
  • 一个空格
  • 连字符
  • 一个空格
  • 然后是数字

最终输出-

D_00145 - 18
D_0011 - 18

我尝试了下面的查询,但它不起作用 -

data_no.keyword : D_* AND NOT data_no.keyword : 'D_*A - *'
regex kibana
2个回答
0
投票

正则表达式:“D_\d+ - \d+”将仅捕获您想要捕获的两行。

“D_”字面匹配字符“D_”(区分大小写)

“\d”匹配一个数字(相当于[0-9])

“+”匹配前一个令牌一次到无限次,尽可能多的次数,根据需要回馈(贪婪)

“ - ”字面匹配字符“ - ”(区分大小写)

参见:https://regex101.com/r/SJQ6ou/1

并使用此网站来学习和测试正则表达式


0
投票

我建议您使用摄取管道来检查此条件并为此创建一个新字段。以下是您可以做到的方法:

POST _bulk
{ "index" : { "_index" : "my_sample_index", "_id" : "1" } }
{ "data_no": "D_00122 - A - 14" }
{ "index" : { "_index" : "my_sample_index", "_id" : "2" } }
{ "data_no": "D_00133A - 15" }
{ "index" : { "_index" : "my_sample_index", "_id" : "3" } }
{ "data_no": "D_00145 - 18" }
{ "index" : { "_index" : "my_sample_index", "_id" : "4" } }
{ "data_no": "D_00167 - B - 18" }
{ "index" : { "_index" : "my_sample_index", "_id" : "5" } }
{ "data_no": "D_00182A - 19" }
{ "index" : { "_index" : "my_sample_index", "_id" : "6" } }
{ "data_no": "D_00121 - A - 13" }
{ "index" : { "_index" : "my_sample_index", "_id" : "7" } }
{ "data_no": "D_0011 - 18" }

PUT _ingest/pipeline/check_dash
{
  "processors": [
      {
        "grok": {
          "field": "data_no",
          "patterns": [
            "%{NOTSPACE:data1} - %{NOTSPACE:data2} - %{NOTSPACE:data3}",
            "%{NOTSPACE:data1} - %{NOTSPACE:data2}"
          ]
        }
      },
      {
        "set": {
          "if": "ctx.data3 == null",
          "field": "number_after_dash",
          "value": true
        }
      },
      {
        "remove": {
          "field": ["data1", "data2", "data3"],
          "ignore_failure": true
        }
      }
    ]
}

POST my_sample_index/_update_by_query?pipeline=check_dash
GET my_sample_index/_search

Kibana Discover 的结果:

© www.soinside.com 2019 - 2024. All rights reserved.