Github: https://github.com/elastic/elasticsearch-py
Python Elasticsearch Client
Elasticsearchの公式低レベルクライアント。 その目的は、PythonのすべてのElasticsearch関連コードに共通の基盤を提供することです。 このため、それは意見のない、非常に拡張可能なものにしようとします。
より限定されたスコープのより高いレベルのクライアントライブラリについては、 elasticsearch-dslを見てください。これは、 elasticsearch elasticsearch-py
上に座っているよりpythonのライブラリです。
クエリを記述し操作するために、より便利で慣用的な方法を提供します。 それはElasticsearch JSON DSLの近くにあり、用語や構造を反映しながら、定義されたクラスやクエリーセットのような表現を直接使ってPythonからDSLの全範囲を公開しています。
また、マッピングを定義し、ドキュメントを取得して保存し、ドキュメントデータをユーザー定義のクラスにラップするという、ORMのような方法でPythonオブジェクトとしてドキュメントを操作するオプションの永続レイヤも提供します。
互換性
ライブラリは0.90.x
以降のすべてのElasticsearchバージョンと互換性がありますが、マッチするメジャーバージョンを使用する必要があります:
Elasticsearch 6.0以降では、ライブラリのメジャーバージョン6( 6.xy
)を使用します。
Elasticsearch 5.0以降では、ライブラリのメジャーバージョン5( 5.xy
)を使用します。
Elasticsearch 2.0以降では、ライブラリのメジャーバージョン2( 2.xy
)などを使用します。
setup.pyまたはrequirements.txtに要件を設定するための推奨される方法は次のとおりです。
# 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
同時に複数のバージョンをインストールする必要がある場合は、古いバージョンもelasticsearch2
とelasticsearch5
としてリリースされます。
インストール
pipで elasticsearch
パッケージをインストールします。
pip install elasticsearch
使用例
簡単な使用例:
>>> from datetime import datetime >>> from elasticsearch import Elasticsearch # by default we connect to localhost:9200 >>> es = Elasticsearch() # create an index in elasticsearch, ignore status code 400 (index already exists) >>> es.indices.create(index='my-index', ignore=400) {u'acknowledged': True} # datetimes will be serialized >>> es.index(index="my-index", doc_type="test-type", id=42, body={"any": "data", "timestamp": datetime.now()}) {u'_id': u'42', u'_index': u'my-index', u'_type': u'test-type', u'_version': 1, u'ok': True} # but not deserialized >>> es.get(index="my-index", doc_type="test-type", id=42)['_source'] {u'any': u'data', u'timestamp': u'2013-05-12T19:45:31.804229'}
Elastic Cloud(およびSSL)ユースケース:
>>> from elasticsearch import Elasticsearch >>> es = Elasticsearch("https://elasticsearch.url:port", http_auth=('elastic','yourpassword')) >>> es.info()
自己署名付き証明書を使用したSSLコンテキストの使用例:
>>> from elasticsearch import Elasticsearch >>> from ssl import create_default_context >>> context = create_default_context(cafile="path/to/cafile.pem") >>> es = Elasticsearch("https://elasticsearch.url:port", ssl_context=context, http_auth=('elastic','yourpassword')) >>> es.info()
特徴
クライアントの機能は次のとおりです。
- 基本的なPythonのデータ型をjsonとの間で変換する(パフォーマンス上の理由からdatetimesはデコードされない)
- クラスタノードの構成可能な自動検出
- 永続的な接続
- 使用可能なすべてのノード間での負荷分散(プラグイン可能な選択戦略による)
- 失敗した接続の不利益(タイムベース – 失敗した接続はタイムアウトに達するまで再試行されません)
- sslとhttp認証のサポート
- スレッドの安全性
- プラガブルアーキテクチャ
ライセンス
著作権2017弾性検索
Apache License、Version 2.0(以下「ライセンス」)の下でライセンスされています。 ライセンスに従わない限り、このファイルを使用することはできません。 あなたはライセンスのコピーを
適用法または書面による合意が必要な場合を除き、本ライセンスに基づいて配布されるソフトウェアは、明示的または黙示的にいかなる種類の保証または条件もなく「現状有姿」で配布されます。 ライセンスに基づいて許可および制限を規定する特定の言語については、ライセンスを参照してください。