有用链接:

最有用的: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.查询索引中的所有内容

  1. #coding=utf8
  2. from elasticsearch import Elasticsearch
  3.  
  4. es = Elasticsearch([{'host':'x.x.x.x','port':9200}])
  5. index = "test"
  6. query = {"query":{"match_all":{}}}
  7. resp = es.search(index, body=query)
  8. resp_docs = resp["hits"]["hits"]
  9. total = resp['hits']['total']
  10.  
  11. print total #总共查找到的数量
  12. print resp_docs[0]['_source']['@timestamp'] #输出一个字段

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

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

  1. #coding=utf8
  2. from elasticsearch import Elasticsearch
  3. import json
  4. import datetime
  5.  
  6. es = Elasticsearch([{'host':'x.x.x.x','port':9200}])
  7. index = "test"
  8. query = { \
  9. "query":{ \
  10. "filtered":{ \
  11. "query":{ \
  12. "bool":{ \
  13. "must_not":{"term":{"A":""}}, \
  14. "must_not":{"term":{"B":""}}, \
  15. } \
  16. }, \
  17. "filter":{
  18. "range":{'@timestamp':{'gte':'now-10d','lt':'now-2d'}}
  19. }
  20. }\
  21. } \
  22. }
  23. resp = es.search(index, body=query, scroll="1m",size=100)
  24. scroll_id = resp['_scroll_id']
  25. resp_docs = resp["hits"]["hits"]
  26. total = resp['hits']['total']
  27. count = len(resp_docs)
  28. datas = resp_docs
  29. while len(resp_docs) > 0:
  30. scroll_id = resp['_scroll_id']
  31. resp = es.scroll(scroll_id=scroll_id, scroll="1m")
  32. resp_docs = resp["hits"]["hits"]
  33. datas.extend(resp_docs)
  34. count += len(resp_docs)
  35. if count >= total:
  36. break
  37.  
  38. print len(datas)

3.聚合

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

  1. #coding=utf8
  2. from elasticsearch import Elasticsearch
  3.  
  4. es = Elasticsearch([{'host':'x.x.x.x','port':9200}])
  5. index = "test"
  6. query = {"aggs":{"all_times":{"terms":{"field":"@timestamp"}}}}
  7. resp = es.search(index, body=query)
  8. total = resp['hits']['total']
  9. print total
  10. 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. Ruby类的创建与使用

    Ruby是一种面向对象编程语言,这意味着它操纵的编程结构称为"对象" 先上代码, 了解类的定义与使用方式 class Computer $manufacturer = " ...

  2. tc 146 2 RectangularGrid(数学推导)

    SRM 146 2 500RectangularGrid Problem Statement Given the width and height of a rectangular grid, ret ...

  3. 【Go入门教程8】总结(25个关键字)

    这一章我们主要介绍了Go语言的一些语法,通过语法我们可以发现Go是多么的简单,只有二十五个关键字.让我们再来回顾一下这些关键字都是用来干什么的. break    default      func  ...

  4. Swift2.1 语法指南——可空链式调用

    原档:https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programmi ...

  5. 将Centos的yum源更换为阿里云源

    阿里云Linux安装软件镜像源 阿里云是最近新出的一个镜像源.得益与阿里云的高速发展,这么大的需求,肯定会推出自己的镜像源.阿里云Linux安装镜像源地址:http://mirrors.aliyun. ...

  6. C#利用Web Service实现短信发送(转)

    通过编程方式实现短信息的发送对很多人来说是一件比较烦杂的事情,目前一般的解决方法是通过计算机和手机的连线,通过可对手机编程的语言编写相关的手机短信息程序来实现,而这种方法对于一般人来说是很难达到的,因 ...

  7. wp手机 htc x310e

    入手htc x310e 手机不错,用着流畅 不习惯,已升到wp7.8,系统限制还是有些需要的功能没有,比如说短信拦截什么的 我需要的常用软件少 转手了 1 注销windows live? 设置--应用 ...

  8. 百度定位API报错:leaked ServiceConnection com.baidu.location.LocationClient$1@426122f0

    使用百度MapApi定位时候,当退出当时使用的activity后,则会报如题的异常,解决办法: 1:当退出当前定位的activity时,一定要在onDestroy方法中要mLocClient.stop ...

  9. css3常用标签

    30个最常用css选择器解析   你也许已经掌握了id.class.后台选择器这些基本的css选择器.但这远远不是css的全部.下面向大家系统的解析css中30个最常用的选择器,包括我们最头痛的浏览器 ...

  10. 第一款支持容器和云部署的开源数据库Neo4j 3.0

    导读 Neo4j 3.0.0 正式发布,这是 Neo4j 3.0 系列的第一个版本.此版本对内部架构进行了全新的设计:提供给开发者更强大的生产力:提供更广阔的部署选择.Neo4j 3.0 被认为是世界 ...