有用链接:

最有用的: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"]

【elasticsearch】python下的使用的更多相关文章

  1. python下ssh的简单实现

    python下的ssh都需要借助第三方模块paramiko来实现,在使用前需要手动安装. 一.python实现ssh (1) linux下的ssh登录 root@ubuntu:~# ssh morra ...

  2. python下编译py成pyc和pyo

     python下编译py成pyc和pyo   其实很简单, 用 python -m py_compile file.py python -m py_compile /root/src/{file1,f ...

  3. Python下划线与命名规范

    Python下划线与命名规范 先看结论,节省只想知道答案你的宝贵时间: _xxx 不能用于from module import * 以单下划线开头的表示的是protected类型的变量.即保护类型只能 ...

  4. python下的orm基本操作(1)--Mysql下的CRUD简单操作(含源码DEMO)

    最近逐渐打算将工作的环境转移到ubuntu下,突然发现对于我来说,这ubuntu对于我这种上上网,收收邮件,写写博客,写写程序的时实在是太合适了,除了刚接触的时候会不怎么完全适应命令行及各种权限管理, ...

  5. Python下科学计算包numpy和SciPy的安装

    转载自:http://blog.sina.com.cn/s/blog_62dfdc740101aoo6.html Python下大多数工具包的安装都很简单,只需要执行 “python setup.py ...

  6. python下的复杂网络编程包networkx的安装及使用

    由于py3.x与工具包的兼容问题,这里采用py2.7 1.python下的复杂网络编程包networkx的使用: http://blog.sina.com.cn/s/blog_720448d30101 ...

  7. Python学习入门基础教程(learning Python)--5.1 Python下文件处理基本过程

    Python下的文件读写操作过程和其他高级语言如C语言的操作过程基本一致,都要经历以下几个基本过程. 1. 打开文件 首先是要打开文件,打开文件的主要目的是为了建立程序和文件之间的联系.按程序访问文件 ...

  8. python下读取excel文件

    项目中要用到这个,所以记录一下. python下读取excel文件方法多种,用的是普通的xlrd插件,因为它各种版本的excel文件都可读. 首先在https://pypi.python.org/py ...

  9. python下异常处理

    1.python下异常如何处理: #encoding=utf-8 """ python遇到异常,程序直接运行 try: "判断有可能抛出异常的代码" ...

  10. python下线程以及锁

    1.python多线程 #encoding=utf-8 """ python多线程,并非真正意义上的多线程 全局锁:在指定时间里,有且只有一个线程在运行 "&q ...

随机推荐

  1. [译]简单得不得了的教程-一步一步用 NODE.JS, EXPRESS, JADE, MONGODB 搭建一个网站

    原文: http://cwbuecheler.com/web/tutorials/2013/node-express-mongo/ 原文的源代码在此 太多的教程教你些一个Hello, World!了, ...

  2. [译]通过IIS Request Filtering解决SQL Server CPU高的问题

    原文http://www.peterviola.com/solving-sql-server-high-cpu-with-iis-request-filtering/ Top Queries by T ...

  3. ROS之VPN服务器设置教程.

    关于ROS系统的安装此处将不再累述,可以自行谷歌,百度搜索“ROS 安装配置教程”. (安装方法可以使用光盘安装,USB引导安装,硬盘写入.) 好了,演示创建VPN服务器的方法: 1.使用WinBox ...

  4. GridView控件隐藏列

    GridView隐藏列visible="false" 后你就无法取得这列的值了 下面是迄今为止最简洁的解决方法了. protected void GVList_RowDataBou ...

  5. UML浅析

    UML概述 UML (Unified Modeling Language)为面向对象软件设计提供统一的.标准的.可视化的建模语言.适用于描述以用例为驱动,以体系结构为中心的软件设计的全过程. UML模 ...

  6. 如何利用cookie来保存用户登录账号

    众所周知,cookie在网页编写中不接或缺,今天就谈谈如何利用cookie技术来保存用户登录账号 1.首先是否保存用户登录账号当然是用户自行决定,所以我们需要在用户登录界面设置一个复选框,以此取得用户 ...

  7. maven之clean、install命令

    1.进入到maven根目录,执行mvn compile命令会在根目录生成target文件(参照maven之helloworld案例),如下图: 2.执行mvn clean可将根目录下生成的target ...

  8. asp.net的sql防注入和去除html标记的方法

    一. // <summary> /// 过滤标记 /// </summary> /// <param name="NoHTML">包括HTML, ...

  9. Redis学习笔记二:单机数据库的实现

    1. 数据库 服务器中的数据库 Redis服务器将所有数据库都保存在服务器状态redis.h/redisServer结构的db数组中,db数组的每个项都是一个redis.h/redisDb结构,每个r ...

  10. 关于JavaScript中对象的继承实现的学习总结

    一.原型链 JavaScript 中原型链是实现继承的主要方法.其主要的思想是利用原型让一个引用类型继承另一个引用类型的属性和方法.实现原型链有一种基本模式,其代码如下. function Super ...