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