Upserting an elasticsearch document

Viewed 978

Here is the code I used. DocumentModel class is used as elasticsearch docment. Currently I am using url as elasticsearch '_id' ,and generating UUID for other field documentId using java. When I try to index a document using this code ,it updates document if exist,or index if not exist. Problem is when it updates document,it also updates documentId. But I require not to update documentId and use existing documentId. what change should I made in this code to do so?

    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();
1 Answers
Related