2

当我将从 MongoDB java 驱动程序函数返回的 org.bson.Document 传递给 elasticsearchMongoDatabase.getCollection().find()索引时,我得到以下异常。

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();

但是在传递从返回的 pymongo 文档时没有收到这样的错误MongoClient().mydb.collection_name.find()

这些 API 有什么区别?与 pymongo 的 find() API 等效的 java 是什么?

4

1 回答 1

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();
于 2016-01-07T07:23:27.633 回答