【elasticsearch】python下的使用

最有用的:http://es.xiaoleilu.com/054_Query_DSL/70_Important_clauses.html

不错的博客:http://www.cnblogs.com/letong/p/4749234.html

其他1:http://www.jianshu.com/p/14aa8b09c789

其他2:http://xiaorui.cc/

上面链接有点老了。新链接

http://elasticsearch-dsl.readthedocs.io/en/latest/

https://elasticsearch.cn/book/elasticsearch_definitive_guide_2.x/_search_lite.html

 

 

1.查询索引中的所有内容

复制代码

#coding=utf8
from elasticsearch import Elasticsearch

es = Elasticsearch([{'host':'x.x.x.x','port':9200}])
index = "test"
query = {"query":{"match_all":{}}}
resp = es.search(index, body=query)
resp_docs = resp["hits"]["hits"]
total = resp['hits']['total']

print total  #总共查找到的数量
print resp_docs[0]['_source']['@timestamp'] #输出一个字段

复制代码

 

2.用scroll分次查询所有内容+复杂条件

过滤条件:字段A不为空且字段B不为空,且时间在过去10天~2天之间

复制代码

#coding=utf8
from elasticsearch import Elasticsearch
import json
import datetime

es = Elasticsearch([{'host':'x.x.x.x','port':9200}])
index = "test"
query = { \
        "query":{ \
            "filtered":{ \
                "query":{ \
                    "bool":{ \
                        "must_not":{"term":{"A":""}}, \
                        "must_not":{"term":{"B":""}}, \
                        } \
                    }, \
                "filter":{
                    "range":{'@timestamp':{'gte':'now-10d','lt':'now-2d'}}
                    }
                }\
            } \
        }
resp = es.search(index, body=query, scroll="1m",size=100)
scroll_id = resp['_scroll_id']
resp_docs = resp["hits"]["hits"]
total = resp['hits']['total']
count = len(resp_docs)
datas = resp_docs
while len(resp_docs) > 0:
    scroll_id = resp['_scroll_id']
    resp = es.scroll(scroll_id=scroll_id, scroll="1m")
    resp_docs = resp["hits"]["hits"]
    datas.extend(resp_docs)
    count += len(resp_docs)
    if count >= total:
        break

print len(datas)

复制代码

 

3.聚合

查看一共有多少种@timestamp字段

复制代码

#coding=utf8
from elasticsearch import Elasticsearch

es = Elasticsearch([{'host':'x.x.x.x','port':9200}])
index = "test"
query = {"aggs":{"all_times":{"terms":{"field":"@timestamp"}}}}
resp = es.search(index, body=query)
total = resp['hits']['total']
print total
print resp["aggregations"]

复制代码

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值