新浪博客

ElasticSearch-DSL 按照@timestamp查询的方法

2016-04-21 09:01阅读:
ELK架构中ES-DSL API接口,可以通过python程序进行调用查询。但是某人的query只能查询到10条记录,而且如何通过给定的开始时间戳及结束的时间戳来进行快速过滤呢?详见以下代码示例:
client = Elasticsearch(['http://127.0.0.1:9200'])
my_index = 'logstash-' + str(time.strftime('%Y.%m.%d', time.localtime(end_time)))
search_terms = Search(using = client, index = my_index).filter('range', ** {'@timestamp': {'gte':'2016-04-20T10:18:48.946Z','lt':'2016-04-20T10:23:48.946Z'}}) \
.query('match', channel_name = channel)[0:1000]
response = search_terms.execute()
关键点解释:
1、连接ES的本机端口;
2、设置index索引,这里的关键字是'logstash+日期'
3、调用Search方法,关键在于先filter,后query.
4、filter的时间戳过滤方法为**{'@timestamp':{'gte': end_time, 'lt':beg_time}} 为
官网的提供方法;
5、query查询,单项匹配,channel_name为logstash的模板中的关键字。
6、query 默认只返回10个结果,[0:1000] ,设置取值范围,视时间段的访问个数而定,这里设置0~1000
总结:ELK的功能很强大,调用Elasticsearch的API很灵活,尤其是python封装的接口API,可以用于nginx\haporxy等的访问日志分析,流量、带宽、实时地图等实时展示。

我的更多文章

下载客户端阅读体验更佳

APP专享