当我将从MongoDB java驱动程序函数MongoDatabase.getCollection().find()
返回的org.bson.Document传递给elasticsearch索引时,我得到以下异常。
MapperParsingException[Field [_id] is a metadata field and cannot be added inside a document. Use the index API request parameters.]
这是代码,
MongoCursor<Document> cursor = mongoCollection.find().iterator();
Document doc = cursor.next();
IndexRequest indexRequest = new IndexRequest(indexName, indexType);
indexRequest.source(doc.toJson());
BulkRequestBuilder bulkRequest = client.prepareBulk();
bulkRequest.add(indexRequest);
bulkRequest.execute().actionGet();
但是在传递从MongoClient().mydb.collection_name.find()
这些API有什么区别?什么是Java相当于pymongo的find()API?
答案 0 :(得分:0)
Python的弹性搜索批量索引API在内部提取"_id"
字段,因为它是元数据字段。
当我们使用Java客户端时,我们必须删除它。
MongoCursor<Document> cursor = mongoCollection.find().iterator();
Document doc = cursor.next();
String id = doc.getString("_id"); // get the _id
doc.remove("_id"); // remove the _id field
IndexRequest indexRequest = new IndexRequest(indexName, indexType, id);
indexRequest.source(doc.toJson());
BulkRequestBuilder bulkRequest = client.prepareBulk();
bulkRequest.add(indexRequest);
bulkRequest.execute().actionGet();