博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
【elasticsearch】python下的使用
阅读量:7030 次
发布时间:2019-06-28

本文共 1822 字,大约阅读时间需要 6 分钟。

有用链接:

最有用的:

不错的博客:

其他1:

其他2:

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

 

 

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

#coding=utf8from elasticsearch import Elasticsearches = 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=utf8from elasticsearch import Elasticsearchimport jsonimport datetimees = 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_docswhile 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: breakprint len(datas)

 

3.聚合

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

#coding=utf8from elasticsearch import Elasticsearches = 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 totalprint resp["aggregations"]

 

转载地址:http://rrrxl.baihongyu.com/

你可能感兴趣的文章
游戏小学生01-egret环境搭建
查看>>
从零开始写爬虫
查看>>
微信小程序,个人开发者创业新平台
查看>>
Chrome vim插件vimium快捷键学习
查看>>
【Redis】Redis常用命令
查看>>
node跨域方法
查看>>
JavaScript笔记——常见DOM知识
查看>>
Angular2、AngularJS、React、vue.js过去一年的Google趋势分析
查看>>
3D轮播图
查看>>
同源策略和跨域方法
查看>>
JavaScript中的delete操作符
查看>>
es7与es8其他知识
查看>>
使用 Hexo 创建项目文档网站
查看>>
typeof和instanceof的区别
查看>>
XAMPP Windows 安装中报错解决方法备忘
查看>>
sublime之利器使用篇
查看>>
每个类都应将所有能力以最小粒度提供给外部可配置,每个业务所需要的功能是这些能力的组合...
查看>>
使用cached的wrapper类读取请求响应内容
查看>>
[python][os]分离文件目录,文件名以及文件后缀
查看>>
解决Android Studio SDK无法下载问题
查看>>