国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

PythonElasticsearchapi

2019-11-14 17:03:23
字體:
來源:轉載
供稿:網友
描述:ElasticSearch是一個基于Lucene的搜索服務器。它提供了一個分布式多用戶能力的全文搜索引擎,基于RESTful web接口。下面介紹了利用Python API接口進行數據查詢,方便其他系統的調用。
 
 
安裝API
pip install elasticsearch
 
建立es連接
from elasticsearch import Elasticsearches = 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 scrollpage = 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 scrollingwhile(scroll_size >0):    
 
  • multi_match 多字段搜索
{    "multi_match":{      "query":"11",      "fields":["Tr","Tq"]    }}
 
 
demo
  • 獲取最近一小時的數據
{'query':    {'filtered':        {'filter':            {'range':                {'@timestamp':{'gt':'now-1h'}}            }        }    }}
  1.   

  • 條件過濾查詢
{    "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"}    }},

  

先介紹到這里,后續會有Query DSL API介紹。

 


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 湖州市| 松潘县| 桐梓县| 湖南省| 江口县| 麻江县| 安化县| 阳朔县| 桦川县| 凤山市| 岳阳县| 阜城县| 新昌县| 崇礼县| 三河市| 莒南县| 仁化县| 宁远县| 上杭县| 台南县| 若尔盖县| 许昌市| 康保县| 赤壁市| 瓦房店市| 庄浪县| 张家口市| 绥芬河市| 雅安市| 澎湖县| 砚山县| 洪江市| 改则县| 雷山县| 奉化市| 长子县| 星子县| 工布江达县| 梓潼县| 南开区| 中牟县|