http://blog.csdn.net/cnweike/article/details/33736429html
es教程:http://es.xiaoleilu.com/010_Intro/35_Tutorial_Aggregations.html
es python api:http://elasticsearch-py.readthedocs.io/en/latest/python
安装APIweb
pip install elasticsearch
创建es链接api
from elasticsearch import Elasticsearch es = Elasticsearch([{'host':'10.10.13.12','port':9200}])
数据检索功能缓存
es.search(index='logstash-2015.08.20', q='http_status_code:5* AND server_name:"web1"', from_='124119')
- took —— Elasticsearch执行这个搜索的耗时,以毫秒为单位
- timed_out —— 指明这个搜索是否超时elasticsearch
- _shards —— 指出多少个分片被搜索了,同时也指出了成功/失败的被搜索的shards的数量
- hits —— 搜索结果
- hits.total —— 可以匹配咱们查询标准的文档的总数目
- hits.hits —— 真正的搜索结果数据(默认只显示前10个文档).net
Elasticsearch中的全部的查询都会触发相关度得分的计算。对于那些咱们不须要相关度得分的场景下,Elasticsearch以过滤器的形式提供了另外一种查询功能。过滤器在概念上相似于查询,可是它们有很是快的执行速度,这种快的执行速度主要有如下两个缘由
- 过滤器不会计算相关度的得分,因此它们在计算上更快一些
- 过滤器能够被缓存到内存中,这使得在重复的搜索查询上,其要比相应的查询快出许多。
日志
经常使用参数code
统计查询功能server
# 语法同search大体同样,但只输出统计值
In[52]: es.count(index='logstash-2015.08.21', q='http_status_code:500') Out[52]:{u'_shards':{u'failed':0, u'successful':5, u'total':5}, u'count':17042}
知识扩展
# Initialize the scroll page = es.search( index ='yourIndex', doc_type ='yourType', scroll ='2m', search_type ='scan', size =1000, body ={ # Your query's body }) sid = page['_scroll_id'] scroll_size = page['hits']['total'] # Start scrolling while(scroll_size >0): print "Scrolling..." page = es.scroll(scroll_id = sid, scroll ='2m') # Update the scroll ID sid = page['_scroll_id'] # Get the number of results that we returned in the last scroll scroll_size = len(page['hits']['hits']) print "scroll size: "+ str(scroll_size) # Do something with the obtained page
以上demo实现了一次取若干数据,数据取完以后结束,不会获取到最新更新的数据。咱们滚动完以后想获取最新数据怎么办?滚动的时候会有一个统计值,如total: 5。跳出循环以后,咱们能够用_from参数定位到5开始滚动以后的数据。
range过滤器查询范围
gt: > 大于
lt: < 小于
gte: >= 大于或等于
lte: <= 小于或等于
"range":{ "money":{ "gt":20, "lt":40 } }
bool组合过滤器
must:全部分句都必须匹配,与 AND 相同。
must_not:全部分句都必须不匹配,与 NOT 相同。
should:至少有一个分句匹配,与 OR 相同。
{ "bool":{ "must":[], "should":[], "must_not":[], } }
term过滤器
{ "terms":{ "money":20 } }
{ "terms":{ "money": [20,30] } }
match查询
{ "match":{ "email":"123456@qq.com" } }
{ "multi_match":{ "query":"11", "fields":["Tr","Tq"] } }
demo
{'query': {'filtered': {'filter': {'range': {'@timestamp':{'gt':'now-1h'}} } } } }
{ "query":{ "filtered":{ "query":{"match":{"http_status_code":500}}, "filter":{"term":{"server_name":"vip03"}} } } }
{'facets': {'stat': {'terms': {'field':'http_status_code', 'order':'count', 'size':50} } }, 'size':0 }
{'facets': {'cip': {'terms': {'fields':['client_ip']}}, 'status_facets':{'terms':{'fields':['http_status_code'], 'order':'term', 'size':50}}}, 'query':{'query_string':{'query':'*'}}, 'size':0 }
{'facets': {'tag': {'terms': {'fields':['http_status_code','client_ip'], 'size':10 } } }, 'query': {'match_all':{}}, 'size':0 }
数据组装
如下是kibana首页的demo,用来统计一段时间内的日志数量
{ "facets": { "0": { "date_histogram": { "field": "@timestamp", "interval": "5m" }, "facet_filter": { "fquery": { "query": { "filtered": { "query": { "query_string": { "query": "*" } }, "filter": { "bool": { "must": [ { "range": { "@timestamp": { 'gt': 'now-1h' } } }, { "exists": { "field": "http_status_code.raw" } }, # --------------- ------- # 此处加匹配条件 ] } } } } } } } }, "size": 0 }
若是想添加匹配条件,在以上代码标识部分加上过滤条件,按照如下代码格式便可
{ "query": { "query_string": {"query": "backend_name:baidu.com"} } },