Elasticsearch
Pythonインタフェース
サーチ…
パラメーター
| パラメータ | 詳細 |
|---|---|
| ホスト | キーhostとportを含むオブジェクトの形式のホストの配列。デフォルトのhostは 'localhost'で、 portは9200です。サンプルエントリは[{"host": "ip of es server", "port": 9200}] |
| sniff_on_start | ブール値クライアントが起動時にノードをスニッフィングするようにする場合、スニッフィングはelasticsearchクラスタ内のノードのリストを取得することを意味します |
| sniff_on_connection_fail | クライアントがアクティブなときに接続が失敗した場合のスニッフィングをトリガーするブール値 |
| sniffer_timeout | 各スニフの時間差(秒) |
| sniff_timeout | スニッフィングの1回の要求のための時間 |
| retry_on_timeout | Booelanのためにクライアントが別のelasticsearchノードに連絡するか、ちょうどエラーをスローするようにタイムアウトするべきか |
| http_auth | 基本的なhttp認証はusername:password形式でここに提供できusername:password |
ドキュメントの索引付け(つまり、サンプルの追加)
必要なPythonライブラリをインストールする:
$ pip install elasticsearch
Elasticsearchに接続し、文書を作成し(例:データ入力)、Elasticsearchを使用して文書を「索引付け」します。
from datetime import datetime
from elasticsearch import Elasticsearch
# Connect to Elasticsearch using default options (localhost:9200)
es = Elasticsearch()
# Define a simple Dictionary object that we'll index to make a document in ES
doc = {
'author': 'kimchy',
'text': 'Elasticsearch: cool. bonsai cool.',
'timestamp': datetime.now(),
}
# Write a document
res = es.index(index="test-index", doc_type='tweet', id=1, body=doc)
print(res['created'])
# Fetch the document
res = es.get(index="test-index", doc_type='tweet', id=1)
print(res['_source'])
# Refresh the specified index (or indices) to guarantee that the document
# is searchable (avoid race conditions with near realtime search)
es.indices.refresh(index="test-index")
# Search for the document
res = es.search(index="test-index", body={"query": {"match_all": {}}})
print("Got %d Hits:" % res['hits']['total'])
# Show each "hit" or search response (max of 10 by default)
for hit in res['hits']['hits']:
print("%(timestamp)s %(author)s: %(text)s" % hit["_source"])
クラスタへの接続
es = Elasticsearch(hosts=hosts, sniff_on_start=True, sniff_on_connection_fail=True, sniffer_timeout=60, sniff_timeout=10, retry_on_timeout=True)
空のインデックスの作成とマッピングの設定
この例では、マッピングを定義することによって空のインデックスを作成します(インデックスなしのドキュメント)。
まず、 ElasticSearchインスタンスを作成し、選択したマッピングを定義します。次に、インデックスが存在するかどうかを確認し、存在しない場合は、それぞれインデックス名とマッピングの本文を含むindexとbodyパラメータを指定してindexを作成します。
from elasticsearch import Elasticsearch
# create an ElasticSearch instance
es = Elasticsearch()
# name the index
index_name = "my_index"
# define the mapping
mapping = {
"mappings": {
"my_type": {
"properties": {
"foo": {'type': 'text'},
"bar": {'type': 'keyword'}
}
}
}
}
# create an empty index with the defined mapping - no documents added
if not es.indices.exists(index_name):
res = es.indices.create(
index=index_name,
body=mapping
)
# check the response of the request
print(res)
# check the result of the mapping on the index
print(es.indices.get_mapping(index_name))
部分更新と更新による更新
部分的な更新:部分的な文書の更新が必要な場合に使用されます。つまり、次の例では、 doc_idの文書のフィールドnameは 'John'に更新されます。フィールドがない場合は、フィールドに追加されます。
doc = {
"doc": {
"name": "John"
}
}
es.update(index='index_name',
doc_type='doc_name',
id='doc_id',
body=doc)
照会による更新:条件を満たす文書を更新する必要がある場合に使用します。つまり、次の例では、 nameフィールドが 'John'と一致する文書の経過時間を更新します。
q = {
"script": {
"inline": "ctx._source.age=23",
"lang": "painless"
},
"query": {
"match": {
"name": "John"
}
}
}
es.update_by_query(body=q,
doc_type='doc_name',
index='index_name')
Modified text is an extract of the original Stack Overflow Documentation
ライセンスを受けた CC BY-SA 3.0
所属していない Stack Overflow