【发布时间】:2017-08-16 17:05:02
【问题描述】:
这是我使用的代码。 DocumentModel 类用作 elasticsearch 文档。目前我使用 url 作为 elasticsearch '_id' ,并使用 java 为其他字段 documentId 生成 UUID。当我尝试使用此代码索引文档时,它会更新文档(如果存在)或索引(如果不存在)。问题是当它更新文档时,它也会更新documentId。但我不需要更新 documentId 并使用现有的 documentId。 我应该对此代码进行哪些更改?
String uuid = UUID.randomUUID().toString();
documentModel.setDocumentID(uuid);
String jsonForIndex = gson.toJson(documentModel);
IndexRequest indexRequest = new IndexRequest(indexName, typeName, documentModel.getId());
indexRequest.source(jsonForIndex);
UpdateRequest updateRequest = new UpdateRequest(indexName, typeName, documentModel.getId());
updateRequest.doc(jsonForUpdate);
updateRequest.upsert(indexRequest);
UpdateQuery updateQuery = new UpdateQueryBuilder().withIndexName(indexName).withType(typeName)
.withId(documentModel.getId()).withDoUpsert(true).withUpdateRequest(updateRequest).withIndexRequest(indexRequest).build();
elasticsearchTemplate.update(updateQuery).getId();
【问题讨论】:
标签: java elasticsearch