學習python庫:elasticsearch-py

1、介紹

elasticsearch-py是一個官方提供的low-level的elasticsearch python客戶端庫。爲何說它是一個low-level的客戶端庫呢?由於它只是對elasticsearch的rest API接口作了一層簡單的封裝,所以提供了最大的靈活性,可是於此同時使用起來就不是太方便。相對於這個low-level的客戶端庫,官方還提供了一個high-level的python客戶端庫:elasticsearch-dsl,這個會在另外一篇文章中介紹。node

更多介紹參見官方文檔:https://elasticsearch-py.readthedocs.io/en/master/python

2、安裝

不一樣的elasticsearch版本要求不一樣的客戶端版本,因此安裝的時候須要根據你的elasticsearch來決定,下面是一個簡單的參考:安全

# Elasticsearch 6.x
elasticsearch>=6.0.0,<7.0.0
# Elasticsearch 5.x
elasticsearch>=5.0.0,<6.0.0
# Elasticsearch 2.x
elasticsearch>=2.0.0,<3.0.0

在兼容的大的版本號下儘可能選擇最新的版本。less

 pip install elasticsearch elasticsearch

3、API

3.1 API文檔

全部API都儘量緊密的映射原始的rest API。ide

3.1.1 全局選項

某些被客戶端添加的參數可使用在全部的API上。函數

1.ignoreurl

被用戶忽略某些http錯誤狀態碼。spa

from elasticsearch import Elasticsearch
es = Elasticsearch()

# ignore 400 cause by IndexAlreadyExistsException when creating an index
es.indices.create(index='test-index', ignore=400)

# ignore 404 and 400
es.indices.delete(index='test-index', ignore=[400, 404])

2.timeout插件

被用於設置超時時間。

# only wait for 1 second, regardless of the client's default
es.cluster.health(wait_for_status='yellow', request_timeout=1)

3.filter_path

被用於過濾返回值。

es.search(index='test-index', filter_path=['hits.hits._id', 'hits.hits._type'])

3.1.2 Elasticsearch

Elasticsearch是一個low-level客戶端,提供了一個從python到es rest端點的直接映射。這個實例擁有屬性cat、cluster、indices、ingest、nodes、snapshot和tasks,經過他們能夠訪問CatClient、ClusterClient、IndicesClient、IngestClient、NodesClient、SnapshotClient和TasksClient的實例。

elasticsearch類包含了操做elasticsearch許多經常使用方法,例如:get、mget、search、index、bulk、create、delete等,這些方法的具體用法,能夠參考elasticsearch-py的官方文檔。

在執行以上方法以前,首先須要得到一個elasticsearch的實例,而獲取這個實例有兩個方法,一個是給elasticsearch的初始化函數傳遞一個connection class實例,另外一個是給elasticsearch的初始化函數傳遞要鏈接的node的host和port,其實最終這些host、port仍是被傳遞給了connection class。

# create connection to localhost using the ThriftConnection
es = Elasticsearch(connection_class=ThriftConnection)

# connect to localhost directly and another node using SSL on port 443
# and an url_prefix. Note that ``port`` needs to be an int.
es = Elasticsearch([
    {'host': 'localhost'},
    {'host': 'othernode', 'port': 443, 'url_prefix': 'es', 'use_ssl': True},
])

3.1.3 Indices

indices用於操做、查詢關於索引的信息,或者能夠說是操做、查詢索引相關的元數據。

3.1.4 Ingest

ingest是一個插件,用於豐富插入數據的插入。

3.1.5 Cluster

cluster用於獲取和集羣相關的信息,例如:集羣的健康狀態、settings等。

3.1.6 Nodes

nodes用於獲取和節點相關的信息。

3.1.7 Cat

 cat能夠用來獲取別名、分片信息、文檔數量等信息。

3.1.8 Snapshot

snapshot用於管理快照。

3.1.9 Tasks

tasks是用於任務管理的,官方文檔上提示該task是新特性,將來可能會改變,因此要注意。

3.2 X-Pack APIs

X-Pack是Elastic Stack擴展,它將安全性,警報,監視,報告和圖形功能捆綁到一個易於安裝的程序包中。

3.2.1 Info

3.2.2 Graph Explore

3.3.3 Licensing API

3.3.4 Machine Learning

3.3.5 Security APIS

3.3.6 Watcher APIS

3.3.7 Migration APIS

3.3 異常

這一節展現了使用elasticsearch-py時可能拋出的異常。

3.4 鏈接層API

connection是負責與集羣鏈接的類。

3.4.1 Transport

transport封裝與邏輯相關的傳輸。處理各個鏈接的實例化,並建立一個鏈接池來保存它們。

3.4.2 Connection Pool

connection pool是一個鏈接池,用於管理鏈接。

3.4.3 Connection Selector 

connection selector是一個鏈接選擇器,它最好的一個例子是zone-aware選擇,能夠自動選擇本地鏈接,只有當本地node都沒法鏈接是纔會去選擇鏈接其餘node。

3.4.4 Urllib3HttpConnection

默認connection class。

3.5 傳輸類

傳輸模塊列出了能夠被當作elasticsearch初始化參數connection_class的connection class。

3.5.1 Connection

connection負責管理與elasticsearch節點的鏈接。

3.5.2 Urllib3HttpConnection

基於urllib的connection class,是默認connection class。

3.5.3 RequestsHttpConnection

基於requests的connection class,除非要使用requests相關的高級特性,不然建議不要使用該類。

3.6 helpers

helpers是一個簡單的輔助函數的集合,這些函數抽象了一些細節或原始API。

3.6.1 bulk helpers

bulk API的特定格式要求致使直接使用它們會很是複雜,所以這裏提供了幾個bulk API的helper函數,具體使用方法能夠參考elasticsearch-py的官方文檔。

3.6.2 scan

scan是對scroll API的簡單抽象。

3.6.3 reindex

reindex用於將可能知足給定查詢的一個索引中的全部文檔從新索引到另外一個索引

相關文章
相關標籤/搜索