我正尝试使用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