【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 ...
随机推荐
- Ruby类的创建与使用
Ruby是一种面向对象编程语言,这意味着它操纵的编程结构称为"对象" 先上代码, 了解类的定义与使用方式 class Computer $manufacturer = " ...
- tc 146 2 RectangularGrid(数学推导)
SRM 146 2 500RectangularGrid Problem Statement Given the width and height of a rectangular grid, ret ...
- 【Go入门教程8】总结(25个关键字)
这一章我们主要介绍了Go语言的一些语法,通过语法我们可以发现Go是多么的简单,只有二十五个关键字.让我们再来回顾一下这些关键字都是用来干什么的. break default func ...
- Swift2.1 语法指南——可空链式调用
原档:https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programmi ...
- 将Centos的yum源更换为阿里云源
阿里云Linux安装软件镜像源 阿里云是最近新出的一个镜像源.得益与阿里云的高速发展,这么大的需求,肯定会推出自己的镜像源.阿里云Linux安装镜像源地址:http://mirrors.aliyun. ...
- C#利用Web Service实现短信发送(转)
通过编程方式实现短信息的发送对很多人来说是一件比较烦杂的事情,目前一般的解决方法是通过计算机和手机的连线,通过可对手机编程的语言编写相关的手机短信息程序来实现,而这种方法对于一般人来说是很难达到的,因 ...
- wp手机 htc x310e
入手htc x310e 手机不错,用着流畅 不习惯,已升到wp7.8,系统限制还是有些需要的功能没有,比如说短信拦截什么的 我需要的常用软件少 转手了 1 注销windows live? 设置--应用 ...
- 百度定位API报错:leaked ServiceConnection com.baidu.location.LocationClient$1@426122f0
使用百度MapApi定位时候,当退出当时使用的activity后,则会报如题的异常,解决办法: 1:当退出当前定位的activity时,一定要在onDestroy方法中要mLocClient.stop ...
- css3常用标签
30个最常用css选择器解析 你也许已经掌握了id.class.后台选择器这些基本的css选择器.但这远远不是css的全部.下面向大家系统的解析css中30个最常用的选择器,包括我们最头痛的浏览器 ...
- 第一款支持容器和云部署的开源数据库Neo4j 3.0
导读 Neo4j 3.0.0 正式发布,这是 Neo4j 3.0 系列的第一个版本.此版本对内部架构进行了全新的设计:提供给开发者更强大的生产力:提供更广阔的部署选择.Neo4j 3.0 被认为是世界 ...