我正嘗試使用Python API將大量文檔批量插入到彈性搜索中。如何在使用Python API的彈性搜索中進行批量索引
import elasticsearch
from pymongo import MongoClient
es = elasticsearch.Elasticsearch()
def index_collection(db, collection, fields, host='localhost', port=27017):
conn = MongoClient(host, port)
coll = conn[db][collection]
cursor = coll.find({}, fields=fields, timeout=False)
print "Starting Bulk index of {} documents".format(cursor.count())
def action_gen():
"""
Generator to use for bulk inserts
"""
for n, doc in enumerate(cursor):
op_dict = {
'_index': db.lower(),
'_type': collection,
'_id': int('0x' + str(doc['_id']), 16),
}
doc.pop('_id')
op_dict['_source'] = doc
yield op_dict
res = bulk(es, action_gen(), stats_only=True)
print res
的文件來自MongoDB的收集和我會哄上面的函數根據該文檔的方式解釋來做大量的索引。
批量索引繼續填充數千個空文檔的彈性搜索。誰能告訴我我做錯了什麼?
你的索引已經存在於ES嗎?如果是這樣,是否有爲其定義的映射(是否所有可能的來自Mongo的文檔都不適合映射)? – rchang 2015-02-05 23:37:14
你的代碼適合我。也許你的bug是數據特定的。謹慎舉一個最小的例子? – thorwhalen 2015-03-23 16:03:53