一、介紹
elasticsearch-py是一個官方提供的low-level的elasticsearch python客戶端庫。為什么說它是一個low-level的客戶端庫呢?因為它只是對elasticsearch的rest API接口做了一層簡單的封裝,因此提供了最大的靈活性,但是於此同時使用起來就不是太方便。相對於這個low-level的客戶端庫,官方還提供了一個high-level的python客戶端庫:elasticsearch-dsl,這個會在另一篇文章中介紹。
更多介紹參見官方文檔:https://elasticsearch-py.readthedocs.io/en/master/
二、安裝
不同的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
在兼容的大的版本號下盡量選擇最新的版本。
pip install elasticsearch
三、API
3.1 API文檔
所有API都盡可能緊密的映射原始的rest API。
3.1.1 全局選項
某些被客戶端添加的參數可以使用在所有的API上。
1.ignore
被用戶忽略某些http錯誤狀態碼。
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用於將可能滿足給定查詢的一個索引中的所有文檔重新索引到另一個索引