2017-03-09 120 views
0

「必須匹配」我在日誌下面,我想用ElasticSearch查詢發現:在elasticsearchElasticsearch查詢日誌

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"} 

由於上述各行表示爲單一的「消息」 ,我很難用POST休息調用來查詢它。我嘗試使用包含「必須匹配」只得到我的日誌的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,然後更多..這對我來說是不一致的。先謝謝你!!

回答

0

問題出在您的bool查詢的第3個子句。讓我給你幾個問題,這些問題將會爲你工作,我會解釋他們爲什麼要完成這項工作。

首先查詢

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個日誌條目會匹配。我能夠重現它。

第二個查詢

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\"」爲例),使查詢文本分析完全相同。並試圖找到標記received,uuid,abc123相鄰職位的索引。只是在相同的確切順序:received - >uuid - >abc123(差不多)。

除了最後一個標記,在我們的例子中是abc123。準確地說,它會爲最後一個標記創建通配符。即received - >uuid - >abc123*

要被完美主義者我想補充一點,received - >uuid - >abc123(即沒有結束通配符) - 是實際Match Phrase查詢正在進行。它還計算索引中的位置,即嘗試匹配「短語」,而不僅僅是隨機位置中的單獨標記。