【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
上面链接有点老了。新链接
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"]
【elasticsearch】python下的使用的更多相关文章
- python下ssh的简单实现
python下的ssh都需要借助第三方模块paramiko来实现,在使用前需要手动安装. 一.python实现ssh (1) linux下的ssh登录 root@ubuntu:~# ssh morra ...
- python下编译py成pyc和pyo
python下编译py成pyc和pyo 其实很简单, 用 python -m py_compile file.py python -m py_compile /root/src/{file1,f ...
- Python下划线与命名规范
Python下划线与命名规范 先看结论,节省只想知道答案你的宝贵时间: _xxx 不能用于from module import * 以单下划线开头的表示的是protected类型的变量.即保护类型只能 ...
- python下的orm基本操作(1)--Mysql下的CRUD简单操作(含源码DEMO)
最近逐渐打算将工作的环境转移到ubuntu下,突然发现对于我来说,这ubuntu对于我这种上上网,收收邮件,写写博客,写写程序的时实在是太合适了,除了刚接触的时候会不怎么完全适应命令行及各种权限管理, ...
- Python下科学计算包numpy和SciPy的安装
转载自:http://blog.sina.com.cn/s/blog_62dfdc740101aoo6.html Python下大多数工具包的安装都很简单,只需要执行 “python setup.py ...
- python下的复杂网络编程包networkx的安装及使用
由于py3.x与工具包的兼容问题,这里采用py2.7 1.python下的复杂网络编程包networkx的使用: http://blog.sina.com.cn/s/blog_720448d30101 ...
- Python学习入门基础教程(learning Python)--5.1 Python下文件处理基本过程
Python下的文件读写操作过程和其他高级语言如C语言的操作过程基本一致,都要经历以下几个基本过程. 1. 打开文件 首先是要打开文件,打开文件的主要目的是为了建立程序和文件之间的联系.按程序访问文件 ...
- python下读取excel文件
项目中要用到这个,所以记录一下. python下读取excel文件方法多种,用的是普通的xlrd插件,因为它各种版本的excel文件都可读. 首先在https://pypi.python.org/py ...
- python下异常处理
1.python下异常如何处理: #encoding=utf-8 """ python遇到异常,程序直接运行 try: "判断有可能抛出异常的代码" ...
- python下线程以及锁
1.python多线程 #encoding=utf-8 """ python多线程,并非真正意义上的多线程 全局锁:在指定时间里,有且只有一个线程在运行 "&q ...
随机推荐
- 全栈工程师是不是必须得会HTML5?
最近看了很多招聘,都要求前端工程师必须得会HTML5.个人觉得只要会了html,HTML5不会太难,不过全栈工程师要求会的太多了 HTML CSS JavaScript HTML 5 CSS 3 都说 ...
- Why Reflection is slowly?(Trail: The Reflection API)
反射的使用 反射通常用于在JVM中应用程序运行中需要检查或者修改运行时行为的项目.这是一个相对高级的特性,并且仅仅可以被对深刻理解java原理的开发者使用.这里给出一个警告的意见,反射是一个强大的技术 ...
- 基于jQuery的对象切换插件:soChange 1.5 (点击下载)
http://www.jsfoot.com/jquery/demo/2011-09-20/192.html 所有参数: $(obj).soChange({ thumbObj:null, //导 ...
- SpringDataJPA的几个使用记录
public Page<XMGLFileTemplateDTO> findXMGLFileTemplateByConditions(XMGLFileTemplateDTO xmglFile ...
- 内网安全工具之hscan扫描
工具下载地址:hscan1.2.zip 界面简单,看配置: 这里我们主要需要配置的是模块和参数 模块,按照默认配置就行,取消 check HTTP vulnerability(漏洞检测) 会更快一点. ...
- 剑指Offer 二叉搜索树的后序遍历序列
题目描述 输入一个整数数组,判断该数组是不是某二叉搜索树的后序遍历的结果.如果是则输出Yes,否则输出No.假设输入的数组的任意两个数字都互不相同. 思路: 后续遍历数组的尾部为根节点,前面的部分 ...
- mysql 主从数据库设置方法
1.主从数据库都需开启bin-log日志 2.在my.ini(windows)或my.cnf(linux)配置文件中添加 server-id = 1(主从配置 id 必须不同) 例子: [mysqld ...
- BZOJ 1057: [ZJOI2007]棋盘制作
Decsription 给你一个矩阵,求最大了 01相间 的矩阵. Sol DP+悬线法. 这是一个论文啊 <浅谈用极大化思想解决最大子矩形问题>--王知昆. 枚举每一根悬线,记录最左/右 ...
- IOS系统ipa软件包在线安装
如果iis不支持ipa和plist下载,可以添加memi类型. ipa application/x-plist plist application/xml 下载用普通链接即可: <a h ...
- linux操作系统flash player问题--ubuntu
adobe公司停止了对linux系统的flash player的更新,这导致很多网页视频不能够通过浏览器观看,很是不爽! 还好,给用户留下了一点点希望,那便是chrome浏览器. 谷歌浏览器,有一款插 ...