首页 文章

Elasticsearch查询“必须匹配”在日志中

提问于
浏览
0

我在我的日志中有以下内容,我想使用ElasticSearch查询来查找:

2014-07-02 20:52:39 INFO home.helloworld: LOGGER/LOG:ID1234 has successfully been received, {"uuid"="abc123"}
2014-07-02 20:52:39 INFO home.helloworld: LOGGER/LOG:ID1234 has successfully been transferred, {"uuid"="abc123"}
2014-07-02 20:52:39 INFO home.byebyeworld: LOGGER/LOG:ID1234 has successfully been processed, {"uuid"="abc123"}
2014-07-02 20:52:39 INFO home.byebyeworld: LOGGER/LOG:ID1234 has exited, {"uuid"="abc123"}
2014-07-02 20:53:00 INFO home.helloworld: LOGGER/LOG:ID1234 has successfully been received, {"uuid"="def123"}
2014-07-02 20:53:00 INFO home.helloworld: LOGGER/LOG:ID1234 has successfully been transferred, {"uuid"="def123"}
2014-07-02 20:53:00 INFO home.byebyeworld: LOGGER/LOG:ID1234 has successfully been processed, {"uuid"="def123"}
2014-07-02 20:53:00 INFO home.byebyeworld: LOGGER/LOG:ID1234 has exited, {"uuid"="def123"}

由于上面的每一行在elasticsearch中都表示为单个“message”,因此我很难使用POST休息调用来查询它 . 我尝试使用如下所示的“必须匹配”来获取我的日志的第1行但它不一致,有时它会返回多次点击而不是只有1次点击:

{
   "query" : {
      "constant_score" : { 
         "filter" : {
            "bool" : {
              "must" : [
                 { "match_phrase_prefix" : {"message" : "home.helloworld:"}}, 
                 { "match_phrase_prefix" : {"message" : "LOGGER/LOG:ID1234"}},
                 { "match" : {"message" : "received, {\"uuid\"=\"abc123\"}"}} 
              ]
           }
         }
      }
   }
}

我在上面的elasticsearch查询中做错了吗?我认为“必须”等于AND,“匹配”更多是CONTAINS,“match_phrase_prefix”是STARTSWITH?有人可以告诉我如何正确查询填充上述日志的日志与不同的uuid号码,只返回单击?最初我以为我得到了上面的查询,它首先返回只有1次击中,然后它返回2然后更多... . 对我来说是不一致的 . 先感谢您!!

1 回答

  • 0

    问题出在 bool 查询的第3个子句中 . 让我给你几个问题,这些问题对你有用,我会解释他们为什么要做这个工作 .

    First Query

    curl -XGET http://localhost:9200/my_logs/_search -d '
    {
       "query" : {
          "constant_score" : {
             "filter" : {
                "bool" : {
                  "must" : [
                     { "match_phrase_prefix" : {"message" : "home.helloworld:"}},
                     { "match_phrase_prefix" : {"message" : "LOGGER/LOG:ID1234"}},
                     { "match" : {
                         "message" : {
                            "query": "received, {\"uuid\"=\"abc123\"", 
                            "operator": "and"
                         }
                       }
                     }
                  ]
               }
             }
          }
       }
    }'
    

    说明

    让我们确保我们在关于索引的同一页面上 . 默认情况下,索引器将通过标准分析链传递您的数据 . 即通过空格分割,减少特殊字符,制作较低的套管等 . 因此在索引中我们只需要有其位置的标记 .

    Match query作为全文查询将采用您的查询文本“ received, {\"uuid\"=\"abc123\" ”,并将通过分析 . 默认情况下,此分析只是按空格分割文本,减少特殊字符,使下限等等 . 此分析的结果与此类似(简化): receiveduuidabc123 .

    match query会做什么 - 它将使用默认 operator (即 or )将这些令牌与 message 字段组合在一起 . 因此,作为逻辑表达式,最后一个子句( match-query )将如下所示: message:received OR message:uuid OR message:abc123 .

    这就是前4个日志条目匹配的原因 . 我能够重现它 .

    Second Query

    curl -XGET http://localhost:9200/my_logs/_search -d '
    {
       "query" : {
          "constant_score" : {
             "filter" : {
                "bool" : {
                  "must" : [
                     { "match_phrase_prefix" : {"message" : "home.helloworld:"}},
                     { "match_phrase_prefix" : {"message" : "LOGGER/LOG:ID1234"}},
                     { "match_phrase_prefix" : {"received, {\"uuid\"=\"abc123\""}}
                  ]
               }
             }
          }
       }
    }'
    

    说明

    这一点有点简单 . 请记住:我们的索引过程会留下令牌及其在索引中的位置 .

    实际Match Phrase Prefix查询正在做什么 - 它采用输入查询(让我们以“ received, {\"uuid\"=\"abc123\" ”为例),使查询文本分析完全相同 . 并尝试在索引中的 neighboring 位置找到令牌 receiveduuidabc123 . 只是按照相同的顺序排列: received - > uuid - > abc123 (差不多) .

    除了最后一个令牌,在我们的例子中是 abc123 . 准确地说,它将为最后一个令牌制作通配符 . 即 received - > uuid - > abc123* .

    要成为完美主义者我会添加 received - > uuid - > abc123 (即最后没有通配符) - 实际上Match Phrase查询正在做什么 . 它还计算索引中的位置,即尝试匹配'phrase',而不仅仅是随机位置的单独令牌 .

相关问题