代码之家  ›  专栏  ›  技术社区  ›  Amir Soleimani Borujerdi

Kibana中的查询不会返回带有Regexp的日志

  •  0
  • Amir Soleimani Borujerdi  · 技术社区  · 5 年前

    我有一个字段名为 log.file.path 在Elasticsearch中 /var/log/dev-collateral/uaa.2020-09-26.log 值,我试图检索所有 日志文件路径 这个领域始于 /var/log/dev-collateral/uaa 我使用了下面的regexp,但它不起作用。

    {
        "regexp":{
            "log.file.path": "/var/log/dev-collateral/uaa.*"
        }
    }
    
    0 回复  |  直到 5 年前
        1
  •  2
  •   A l w a y s S u n n y    5 年前

    让我们看看它为什么不起作用?我已经使用Kibana UI为两个文档编制了索引,如下所示-

    PUT myindex/_doc/1
    {
      "log.file.path" : "/var/log/dev-collateral/uaa.2020-09-26.log"
    }
    
    PUT myindex/_doc/2
    {
      "log.file.path" : "/var/log/dev-collateral/uaa.2020-09-26.txt"
    }
    

    当我试图看到 代币 有关文本的详细信息 log.file.path 现场使用 _analyze 美国石油学会

    POST _analyze
    {
      "text": "/var/log/dev-collateral/uaa.2020-09-26.log"
    }
    

    它给了我,

    {
      "tokens" : [
        {
          "token" : "var",
          "start_offset" : 1,
          "end_offset" : 4,
          "type" : "<ALPHANUM>",
          "position" : 0
        },
        {
          "token" : "log",
          "start_offset" : 5,
          "end_offset" : 8,
          "type" : "<ALPHANUM>",
          "position" : 1
        },
        {
          "token" : "dev",
          "start_offset" : 9,
          "end_offset" : 12,
          "type" : "<ALPHANUM>",
          "position" : 2
        },
        {
          "token" : "collateral",
          "start_offset" : 13,
          "end_offset" : 23,
          "type" : "<ALPHANUM>",
          "position" : 3
        },
        {
          "token" : "uaa",
          "start_offset" : 24,
          "end_offset" : 27,
          "type" : "<ALPHANUM>",
          "position" : 4
        },
        {
          "token" : "2020",
          "start_offset" : 28,
          "end_offset" : 32,
          "type" : "<NUM>",
          "position" : 5
        },
        {
          "token" : "09",
          "start_offset" : 33,
          "end_offset" : 35,
          "type" : "<NUM>",
          "position" : 6
        },
        {
          "token" : "26",
          "start_offset" : 36,
          "end_offset" : 38,
          "type" : "<NUM>",
          "position" : 7
        },
        {
          "token" : "log",
          "start_offset" : 39,
          "end_offset" : 42,
          "type" : "<ALPHANUM>",
          "position" : 8
        }
      ]
    }
    

    你可以看到,Elasticsearch已经将你的输入文本分割成标记,当你在索引中插入它们时。这是因为elasticsearch使用 标准分析仪 当我们索引文档和 它将我们的文档拆分为小部分作为标记,删除标点符号、小写文本等 .这就是你当前的regexp查询不起作用的原因。

    GET myindex/_search
    {
      "query": {
        "match": {
          "log.file.path": "var"
        }
      }
    }
    

    如果你尝试这种方式,它将工作,但对于你的情况,你需要匹配每一个 日志文件路径 最后是 日志 那你现在怎么办?只是在索引文档时不要应用分析器。关键字类型按原样存储您提供的字符串。

    使用创建映射 keyword 类型

    PUT myindex2/
    {
      "mappings": {
        "properties": {
          "log.file.path": {
            "type": "keyword"
          }
        }
      }
    }
    

    索引文件,

    PUT myindex2/_doc/1
    {
      "log.file.path" : "/var/log/dev-collateral/uaa.2020-09-26.log"
    }
    
    PUT myindex2/_doc/2
    {
      "log.file.path" : "/var/log/dev-collateral/uaa.2020-09-26.txt"
    }
    

    搜索 regexp ,

    GET myindex2/_search
    {
      "query": {
        "regexp": {
          "log.file.path": "/var/log/dev-collateral/uaa.2020-09-26.*"
        }
      }
    }
    
        2
  •  0
  •   Amir Soleimani Borujerdi    5 年前

    我使用了这个查询,它是有效的!

    {
      "query": {
        "regexp": {
          "log.file.path.keyword": {
            "value": "/var/log/dev-collateral/uaa.*",
            "flags": "ALL",
            "max_determinized_states": 10000,
            "rewrite": "constant_score"
          }
        }
      }
    }