Python Elasticsearch api,组合过滤器,term过滤器,正则查询 ,match查询,获取最近一小时的数据
Python Elasticsearch api
安装API
- pip install elasticsearch
建立es连接
- from elasticsearch import Elasticsearch
- es = Elasticsearch([{'host':'10.10.13.12','port':9200}])
数据检索功能
- es.search(index='logstash-2015.08.20', q='http_status_code:5* AND server_name:"web1"', from_='124119')
常用参数
index - 索引名
q - 查询指定匹配 使用Lucene查询语法
from_ - 查询起始点 默认0
doc_type - 文档类型
size - 指定查询条数 默认10
field - 指定字段 逗号分隔
sort - 排序 字段:asc/desc
body - 使用Query DSL
scroll - 滚动查询
统计查询功能
# 语法同search大致一样,但只输出统计值
- In[52]: es.count(index='logstash-2015.08.21', q='http_status_code:500')
- Out[52]:{u'_shards':{u'failed':0, u'successful':5, u'total':5}, u'count':17042}
知识扩展
滚动demo
- # Initialize the scroll
- page = es.search(
- index ='yourIndex',
- doc_type ='yourType',
- scroll ='2m',
- search_type ='scan',
- size =1000,
- body ={
- # Your query's body
- })
- sid = page['_scroll_id']
- scroll_size = page['hits']['total']
- # Start scrolling
- while(scroll_size >0):
- print "Scrolling..."
- page = es.scroll(scroll_id = sid, scroll ='2m')
- # Update the scroll ID
- sid = page['_scroll_id']
- # Get the number of results that we returned in the last scroll
- scroll_size = len(page['hits']['hits'])
- print "scroll size: "+ str(scroll_size)
- # Do something with the obtained page
以上demo实现了一次取若干数据,数据取完之后结束,不会获取到最新更新的数据。我们滚动完之后想获取最新数据怎么办?滚动的时候会有一个统计值,如total: 5。跳出循环之后,我们可以用_from参数定位到5开始滚动之后的数据。
Query DSL
range过滤器查询范围
gt: > 大于
lt: < 小于
gte: >= 大于或等于
lte: <= 小于或等于
- "range":{
- "money":{
- "gt":20,
- "lt":40
- }
- }
bool组合过滤器
must:所有分句都必须匹配,与 AND 相同。
must_not:所有分句都必须不匹配,与 NOT 相同。
should:至少有一个分句匹配,与 OR 相同。
- {
- "bool":{
- "must":[],
- "should":[],
- "must_not":[],
- }
- }
term过滤器
term单过滤
- {
- "terms":{
- "money":20
- }
- }
terms复数版本,允许多个匹配条件
- {
- "terms":{
- "money": [20,30]
- }
- }
正则查询
- {
- "regexp": {
- "http_status_code": "5.*"
- }
- }
match查询
match 精确匹配
- {
- "match":{
- "email":"123456@qq.com"
- }
- }
multi_match 多字段搜索
- {
- "multi_match":{
- "query":"11",
- "fields":["Tr","Tq"]
- }
- }
demo
获取最近一小时的数据
- {'query':
- {'filtered':
- {'filter':
- {'range':
- {'@timestamp':{'gt':'now-1h'}}
- }
- }
- }
- }
条件过滤查询
- {
- "query":{
- "filtered":{
- "query":{"match":{"http_status_code":500}},
- "filter":{"term":{"server_name":"vip03"}}
- }
- }
- }
Terms Facet 单字段统计
- {'facets':
- {'stat':
- {'terms':
- {'field':'http_status_code',
- 'order':'count',
- 'size':50}
- }
- },
- 'size':0
- }
一次统计多个字段
- {'facets':
- {'cip':
- {'terms':
- {'fields':['client_ip']}},
- 'status_facets':{'terms':{'fields':['http_status_code'],
- 'order':'term',
- 'size':50}}},
- 'query':{'query_string':{'query':'*'}},
- 'size':0
- }
多个字段一起统计
- {'facets':
- {'tag':
- {'terms':
- {'fields':['http_status_code','client_ip'],
- 'size':10
- }
- }
- },
- 'query':
- {'match_all':{}},
- 'size':0
- }
数据组装
以下是kibana首页的demo,用来统计一段时间内的日志数量
- {
- "facets": {
- "0": {
- "date_histogram": {
- "field": "@timestamp",
- "interval": "5m"
- },
- "facet_filter": {
- "fquery": {
- "query": {
- "filtered": {
- "query": {
- "query_string": {
- "query": "*"
- }
- },
- "filter": {
- "bool": {
- "must": [
- {
- "range": {
- "@timestamp": {
- 'gt': 'now-1h'
- }
- }
- },
- {
- "exists": {
- "field": "http_status_code.raw"
- }
- },
- # --------------- -------
- # 此处加匹配条件
- ]
- }
- }
- }
- }
- }
- }
- }
- },
- "size": 0
- }
如果想添加匹配条件,在以上代码标识部分加上过滤条件,按照以下代码格式即可
- {
- "query": {
- "query_string": {"query": "backend_name:baidu.com"}
- }
- },
Python Elasticsearch api,组合过滤器,term过滤器,正则查询 ,match查询,获取最近一小时的数据的更多相关文章
- Python Elasticsearch api
描述:ElasticSearch是一个基于Lucene的搜索服务器.它提供了一个分布式多用户能力的全文搜索引擎,基于RESTful web接口.下面介绍了利用Python API接口进行数据查询,方便 ...
- Elasticsearch学习系列之term和match查询
lasticsearch查询模式 一种是像传递URL参数一样去传递查询语句,被称为简单查询 GET /library/books/_search //查询index为library,type为book ...
- Elasticsearch学习系列之term和match查询实例
Elasticsearch查询模式 一种是像传递URL参数一样去传递查询语句,被称为简单查询 GET /library/books/_search //查询index为library,type为boo ...
- asp.net mvc ,asp.net mvc api 中使用全局过滤器进行异常捕获记录
MVC下的全局异常过滤器注册方式如下:标红为asp.net mvc ,asp.net mvc api 注册全局异常过滤器的不同之处 using SuperManCore; using System. ...
- springCloud学习05之api网关服务zuul过滤器filter
前面学习了zuul的反向代理.负载均衡.fallback回退.这张学习写过滤器filter,做java web开发的对filter都不陌生,那就是客户端(如浏览器)发起请求的时候,都先经过过滤器fil ...
- ES 20 - 查询Elasticsearch中的数据 (基于DSL查询, 包括查询校验match + bool + term)
目录 1 什么是DSL 2 DSL校验 - 定位不合法的查询语句 3 match query的使用 3.1 简单功能示例 3.1.1 查询所有文档 3.1.2 查询满足一定条件的文档 3.1.3 分页 ...
- ELK 学习笔记之 elasticsearch bool组合查询
elasticsearch bool组合查询: 相当于sql:where _type = 'books' and (price = 500 or title = 'bigdata') Note: mu ...
- Elasticsearch API响应的一些常用选项
我们可以点击Elasticsearch API以获取所需的响应,但是如果要修改API响应,以便我们更改显示格式或过滤掉某些字段,然后我们可以将这些选项与查询一起应用. 有一些常见的选项可以适用于API ...
- 笨鸟先飞之ASP.NET MVC系列之过滤器(01过滤器简介)
过滤器 什么是过滤器? 过滤器(Filter) 主要的作用大致可以理解为把我们的附加逻辑注入到MVC框架的请求处理. 在ASP.NET MVC的请求处理中一种有19个管道事件分别是 BeginRequ ...
随机推荐
- 探索Redis设计与实现9:数据库redisDb与键过期删除策略
本文转自互联网 本系列文章将整理到我在GitHub上的<Java面试指南>仓库,更多精彩内容请到我的仓库里查看 https://github.com/h2pl/Java-Tutorial ...
- 74、Salesforce的String的format方法
String placehodler = 'Hello {0} , {1} is cool!'; List<String> fillers = new String[]{'Jason',' ...
- hibernate.Criteria分页排序模糊查询
org.hibernate.Criteria criteria = simpleDAO.getSession().createCriteria(Event.class); Criterion c = ...
- 【转】理解JMX之介绍和简单使用
原文链接:https://blog.csdn.net/lmy86263/article/details/71037316 近期在项目上需要添加一些功能,想把一个开源工程整合进来,虽说是整合,但是觉得跟 ...
- 04 循环结构概述和for语句的格式及其使用
04.01_Java语言基础(循环结构概述和for语句的格式及其使用) A:循环结构的分类 for,while,do…while B:循环结构for语句的格式: for(初始化表达式;条件表达式;循环 ...
- 浅析Draw Call
Draw Call是CPU对GPU的一种命令,仅仅指向一个需要被渲染的图元列表,在OpenGL和DirectX中分别体现为glDrawElements和DrawIndexedPrimitive图像编程 ...
- 用Emacs进行Python开发
用Emacs进行Python开发 */--> code {color: #FF0000} pre.src {background-color: #002b36; color: #839496;} ...
- 【Spring】每个程序员都使用Spring(四)——Aop+自定义注解做日志拦截
一.前言 上一篇博客向大家介绍了Aop的概念,对切面=切点+通知 .连接点.织入.目标对象.代理(jdk动态代理和CGLIB代理)有所了解了.理论很强,实用就在这篇博客介绍. 这篇博客中,小编向大家介 ...
- centos6系列更换阿里yum源
1.首先备份原来的cent os官方yum源 cp /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.bak 2. ...
- 三、hibernate中持久化类的使用
hibernate的持久化类 持久化:将内存中的一个对象持久化到数据库中的过程,hibernate就是一个用来进行持久化的框架 持久化类:一个Java对象与数据库中表建立了关系映射,那么这个类在hib ...