【发布时间】:2015-04-05 23:21:46
【问题描述】:
我正在尝试使用 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 的文档不适合映射)?
-
您的代码对我有用。也许您的错误是特定于数据的。愿意举一个简单的例子吗?
标签: python mongodb elasticsearch