Python Elasticsearch api

 
描述:ElasticSearch是一个基于Lucene的搜索服务器。它提供了一个分布式多用户能力的全文搜索引擎,基于RESTful web接口。下面介绍了利用Python API接口进行数据查询,方便其他系统的调用。

安装API

  1. pip install elasticsearch

建立es连接

  1. from elasticsearch import Elasticsearch
  2. es = Elasticsearch([{'host':'10.10.13.12','port':9200}])

  

数据检索功能

  1. 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大致一样,但只输出统计值

  1. In[52]: es.count(index='logstash-2015.08.21', q='http_status_code:500')
  2. Out[52]:{u'_shards':{u'failed':0, u'successful':5, u'total':5}, u'count':17042}

  

知识扩展

滚动demo

  1. # Initialize the scroll
  2. page = es.search(
  3. index ='yourIndex',
  4. doc_type ='yourType',
  5. scroll ='2m',
  6. search_type ='scan',
  7. size =1000,
  8. body ={
  9. # Your query's body
  10. })
  11. sid = page['_scroll_id']
  12. scroll_size = page['hits']['total']
  13. # Start scrolling
  14. while(scroll_size >0):
  15. print "Scrolling..."
  16. page = es.scroll(scroll_id = sid, scroll ='2m')
  17. # Update the scroll ID
  18. sid = page['_scroll_id']
  19. # Get the number of results that we returned in the last scroll
  20. scroll_size = len(page['hits']['hits'])
  21. print "scroll size: "+ str(scroll_size)
  22. # Do something with the obtained page

以上demo实现了一次取若干数据,数据取完之后结束,不会获取到最新更新的数据。我们滚动完之后想获取最新数据怎么办?滚动的时候会有一个统计值,如total: 5。跳出循环之后,我们可以用_from参数定位到5开始滚动之后的数据。

Query DSL

range过滤器查询范围

gt: > 大于
lt: < 小于
gte: >= 大于或等于
lte: <= 小于或等于

  1. "range":{
  2. "money":{
  3. "gt":20,
  4. "lt":40
  5. }
  6. }

  

bool组合过滤器

must:所有分句都必须匹配,与 AND 相同。
must_not:所有分句都必须不匹配,与 NOT 相同。
should:至少有一个分句匹配,与 OR 相同。

  1. {
  2. "bool":{
  3.   "must":[],
  4.   "should":[],
  5.   "must_not":[],
  6. }
  7. }

term过滤器

term单过滤

  1. {
  2. "terms":{
  3.   "money":20
  4. }
  5. }

  

terms复数版本,允许多个匹配条件

  1. {
  2. "terms":{
  3.   "money": [20,30]
  4. }
  5. }

正则查询 

  1. {
  2. "regexp": {
  3. "http_status_code": "5.*"
  4. }
  5. }

match查询

match 精确匹配

  1. {
  2. "match":{
  3.   "email":"123456@qq.com"
  4. }
  5. }

multi_match 多字段搜索

  1. {
  2. "multi_match":{
  3.   "query":"11",
  4.   "fields":["Tr","Tq"]
  5. }
  6. }

demo

获取最近一小时的数据

  1. {'query':
  2. {'filtered':
  3. {'filter':
  4. {'range':
  5. {'@timestamp':{'gt':'now-1h'}}
  6. }
  7. }
  8. }
  9. }

  

条件过滤查询

  1. {
  2. "query":{
  3. "filtered":{
  4. "query":{"match":{"http_status_code":500}},
  5. "filter":{"term":{"server_name":"vip03"}}
  6. }
  7. }
  8. }

Terms Facet 单字段统计

  1. {'facets':
  2. {'stat':
  3. {'terms':
  4. {'field':'http_status_code',
  5. 'order':'count',
  6. 'size':50}
  7. }
  8. },
  9. 'size':0
  10. }

   

一次统计多个字段

  1. {'facets':
  2. {'cip':
  3. {'terms':
  4. {'fields':['client_ip']}},
  5. 'status_facets':{'terms':{'fields':['http_status_code'],
  6. 'order':'term',
  7. 'size':50}}},
  8. 'query':{'query_string':{'query':'*'}},
  9. 'size':0
  10. }

  

多个字段一起统计

  1. {'facets':
  2. {'tag':
  3. {'terms':
  4. {'fields':['http_status_code','client_ip'],
  5. 'size':10
  6. }
  7. }
  8. },
  9. 'query':
  10. {'match_all':{}},
  11. 'size':0
  12. }

  

数据组装

以下是kibana首页的demo,用来统计一段时间内的日志数量

  1. {
  2. "facets": {
  3. "0": {
  4. "date_histogram": {
  5. "field": "@timestamp",
  6. "interval": "5m"
  7. },
  8. "facet_filter": {
  9. "fquery": {
  10. "query": {
  11. "filtered": {
  12. "query": {
  13. "query_string": {
  14. "query": "*"
  15. }
  16. },
  17. "filter": {
  18. "bool": {
  19. "must": [
  20. {
  21. "range": {
  22. "@timestamp": {
  23. 'gt': 'now-1h'
  24. }
  25. }
  26. },
  27. {
  28. "exists": {
  29. "field": "http_status_code.raw"
  30. }
  31. },
  32. # --------------- -------
  33. # 此处加匹配条件
  34. ]
  35. }
  36. }
  37. }
  38. }
  39. }
  40. }
  41. }
  42. },
  43. "size": 0
  44. }

  

如果想添加匹配条件,在以上代码标识部分加上过滤条件,按照以下代码格式即可

  1. {
  2. "query": {
  3. "query_string": {"query": "backend_name:baidu.com"}
  4. }
  5. },

Python Elasticsearch api,组合过滤器,term过滤器,正则查询 ,match查询,获取最近一小时的数据的更多相关文章

  1. Python Elasticsearch api

    描述:ElasticSearch是一个基于Lucene的搜索服务器.它提供了一个分布式多用户能力的全文搜索引擎,基于RESTful web接口.下面介绍了利用Python API接口进行数据查询,方便 ...

  2. Elasticsearch学习系列之term和match查询

    lasticsearch查询模式 一种是像传递URL参数一样去传递查询语句,被称为简单查询 GET /library/books/_search //查询index为library,type为book ...

  3. Elasticsearch学习系列之term和match查询实例

    Elasticsearch查询模式 一种是像传递URL参数一样去传递查询语句,被称为简单查询 GET /library/books/_search //查询index为library,type为boo ...

  4. asp.net mvc ,asp.net mvc api 中使用全局过滤器进行异常捕获记录

    MVC下的全局异常过滤器注册方式如下:标红为asp.net mvc ,asp.net mvc api  注册全局异常过滤器的不同之处 using SuperManCore; using System. ...

  5. springCloud学习05之api网关服务zuul过滤器filter

    前面学习了zuul的反向代理.负载均衡.fallback回退.这张学习写过滤器filter,做java web开发的对filter都不陌生,那就是客户端(如浏览器)发起请求的时候,都先经过过滤器fil ...

  6. 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 分页 ...

  7. ELK 学习笔记之 elasticsearch bool组合查询

    elasticsearch bool组合查询: 相当于sql:where _type = 'books' and (price = 500 or title = 'bigdata') Note: mu ...

  8. Elasticsearch API响应的一些常用选项

    我们可以点击Elasticsearch API以获取所需的响应,但是如果要修改API响应,以便我们更改显示格式或过滤掉某些字段,然后我们可以将这些选项与查询一起应用. 有一些常见的选项可以适用于API ...

  9. 笨鸟先飞之ASP.NET MVC系列之过滤器(01过滤器简介)

    过滤器 什么是过滤器? 过滤器(Filter) 主要的作用大致可以理解为把我们的附加逻辑注入到MVC框架的请求处理. 在ASP.NET MVC的请求处理中一种有19个管道事件分别是 BeginRequ ...

随机推荐

  1. [CSP-S模拟测试63]题解

    A.Median 这题的数据生成方式并没有什么规律,所以可以认为是随机数据. 维护一个桶,表示当前K长区间里的值域情况. 并且用变量记录中位数值域上的左侧有多少个数,当区间调整时一并调整桶和这个变量即 ...

  2. [jzoj5840]Miner 题解(欧拉路)

    首先考虑第一问.每个联通块的情况是相对独立的,所以可以分别求每个联通块的答案.无向图中存在欧拉路的条件是奇点数为0或2,那么合法方案肯定是tp到一个奇点,通过一条欧拉路到另一个奇点,再tp到另一个奇点 ...

  3. IDEA的骚操作

    1.var声明 "jack".var ==>final String name = "jack"; 2.null判空 user.null==>if( ...

  4. Linux终端提示符颜色设置

    将以下语句添加到用户目录下的 .bashrc 文件中,然后执行 source .bashrc 使其立即生效. PS1='${debian_chroot:+($debian_chroot)}\[\033 ...

  5. 向上转型---父类引用指向子类对象 A a = New B()的使用

    一.向上转型 向上转型是JAVA中的一种调用方式,是多态的一种表现.向上转型并非是将B自动向上转型为A的对象,相反它是从另一种角度去理解向上两字的:它是对A的对象的方法的扩充,即A的对象可访问B从A中 ...

  6. 反向代理Reverse proxy

    https://www.zhihu.com/question/24723688/answer/160252724 反向代理在计算机世界里,由于单个服务器的处理客户端(用户)请求能力有一个极限,当用户的 ...

  7. 百度上有个最难数独, 用python跑它

    直接上代码 #!/usr/bin/python3 #coding=GB2312 import tkinter as tk import threading import time import ran ...

  8. javascript实现继承的六种方式

    ​ /*实现继承的六种方式*/ /*1.扩展原型对象的方法实现继承*/ function Foo1(){} //在Foo1函数上扩展一个fn1方法,由构造函数创建的对象都具有fn1这个方法 Foo1. ...

  9. upc组队赛6 Bumped!【最短路】

    Bumped! 题目描述 Peter returned from the recently held ACM ICPC World finals only to find that his retur ...

  10. 前端学习笔记——CSS选择器

    学习css选择器之前我们先了解下css规则: CSS 规则由两个主要的部分构成:选择器,以及一条或多条声明: 选择器通常是需要改变样式的 HTML 元素. 每条声明由一个属性和一个值组成. 1.id选 ...