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

sg2wtvxw  于 11个月前  发布在  Kibana
关注(0)|答案(2)|浏览(231)

我有一个领域在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 - *'

xmjla07d

xmjla07d1#

正则表达式:“D_\d+ - \d+”将只捕获您想要捕获的两行。
“D_”与字符“D_”匹配(区分大小写)
“\d”匹配数字(相当于[0-9])
“+”在一次和无限次之间匹配上一个令牌,尽可能多的次数,根据需要回馈(贪婪)
“-“与字符“-”匹配(区分大小写)
参见:https://regex101.com/r/SJQ6ou/1
并使用此网站学习和测试regex

bpzcxfmw

bpzcxfmw2#

我建议你使用an ingest pipeline来检查这个条件,并为此创建一个新的字段。下面是你如何做到这一点:

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的结果:


相关问题