讀取多份文檔
顯式聲明索引名稱和文檔類型
from elasticsearch import Elasticsearch
from pprint import pprint
es = Elasticsearch(hosts=["192.168.1.132"])
# 根據索引名稱、類型名稱來獲取多個文檔
s = es.mget(
index="megacorp",
doc_type="employee",
body={
"docs": [
{"_id": 1},
{"_id": 2},
{"_id": 3},
{"_id": 4},
{"_id": 5},
]
}
)
pprint(s)
在body中聲明索引名稱和文檔類型
from elasticsearch import Elasticsearch
from pprint import pprint
es = Elasticsearch(hosts=["192.168.1.132"])
s = es.mget(
body={
"docs": [
{"_index": "megacorp", "_type": "employee", "_id": 1},
{"_index": "megacorp", "_type": "employee", "_id": 2},
{"_index": "megacorp", "_type": "employee", "_id": 3},
{"_index": "website", "_type": "blog", "_id": 1},
{"_index": "website", "_type": "blog", "_id": 2},
{"_index": "website", "_type": "blog", "_id": 3},
{"_index": "website", "_type": "blog", "_id": 4},
{"_index": "website", "_type": "blog", "_id": 5},
{"_index": "website", "_type": "blog", "_id": 6},
{"_index": "website", "_type": "blog", "_id": 7},
]
}
)
pprint(s)
?
?
寫入多分文檔
es.bulk
from elasticsearch import Elasticsearch
from elasticsearch import helpers
from datetime import datetime
from pprint import pprint
es = Elasticsearch(hosts=["192.168.1.132"])
s = es.bulk(
index="website",
doc_type="blog",
body=[
# {action: metadata}
{"create": {"_id": 8}},
# {request body}
{
"title": "created using es.bulk",
"date": datetime.now().strftime("%Y/%m/%d %H:%M:%S"),
"text": "try to write a doc to es"
},
# {action: metadata}
{"create": {"_id": 19}},
# {request body}
{
"title": "created using es.bulk",
"date": datetime.now().strftime("%Y/%m/%d %H:%M:%S"),
"text": "try to write a doc to es"
}
]
)
helps.bulk
from elasticsearch import Elasticsearch
from elasticsearch import helpers
from datetime import datetime
from pprint import pprint
es = Elasticsearch(hosts=["192.168.1.132"])
s = helpers.bulk(
client=es,
actions=[
{
# 備注: _op_type 默認采用的是index.
# index和create的區別在于, index表示重建索引, version會自動+1, 而
# create則表示創建, 當數據已存在時, 會報錯.
"_op_type": "index", # create, delete, index, update
"_index": "website",
"_type": "blog",
"_id": 11,
"_source": {
"title": "created using helpers.bulk",
"date": datetime.now().strftime("%Y/%m/%d %H:%M:%S"),
"text": "try to write a doc to es"
}
},
{
"_op_type": "index",
"_index": "website",
"_type": "blog",
"_id": 12,
"_source": {
"title": "created using helpers.bulk",
"date": datetime.now().strftime("%Y/%m/%d %H:%M:%S"),
"text": "try to write a doc to es"
}
}
]
)