0

信息:獲取日誌文件,因爲它是用彈性搜索

安裝機器上從那裏日誌將被讀取並送給彈性搜索服務器Filebeat。從測試機器上,使用elasticsearch-dsl,我正在讀取日誌並將其寫入文件。

問題:

原稿登錄從機:

[Timestamp][INFO] AAAAAA 
[Timestamp][INFO] BBBBBB 
[Timestamp][INFO] CCCCCC 

搜索和日誌寫入輸出文件後:

[Timestamp][INFO] CCCCCC 
[Timestamp][INFO] AAAAAA 
[Timestamp][INFO] BBBBBB 

如何保持日誌序列完整或它是?

代碼:

from elasticsearch import Elasticsearch 
from elasticsearch_dsl import Search, Q, Index 
import time 
#Make Connection 
es = Elasticsearch(["100.16.13.222:9200"]) 

#Create Index Object 
ind = Index("filebeat-*",using=es) 
#Clear Cache 
ind.clear_cache() 
#Create Search object for this index 
sear = ind.search() 

#Create query 
sear = sear.query("match",host="WIN-LK9FS7568K4").query("match",tags="old_log") 
res = sear.execute(ignore_cache=True) 
print int(res.hits.total) 

with open("a.txt","w") as fh: 
    for i in sear.scan(): 
     fh.write(i.message+"\n") 
+0

在您的搜索中,您需要按時間戳排序日誌 – Val

+0

Val - 將會有兩個時間戳。由於彈性搜索和日誌時間戳的時間戳。如何使用日誌的時間戳進行排序? –

+0

@Val - 你能幫我嗎。如何使用python elasticsearch-dsl在查詢中使用regexp? –

回答

1

您需要通過時間戳你的日誌進行排序。更改您的搜索代碼這樣:

sear = sear.sort('timestamp') 
      .query("match",host="WIN-LK9FS7568K4") 
      .query("match",tags="old_log") 

當然,您需要更改timestamp到您的時間戳字段匹配。

+0

感謝您的幫助!還有一個問題。如何使用「偏移量」進行搜索?我已嘗試sear.sort(「偏移」),但它不排序 –

+1

這是描述[這裏](https://elasticsearch-dsl.readthedocs.io/en/latest/search_dsl.html#pagination),只需使用Python切片API。 – Val