当我将从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()返回的pymongo文档时没有得到这样的错误

这些API有什么区别?相当于pymongo的find()API的Java是什么?

最佳答案

Python的elasticsearch批量索引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();

10-06 03:47