Tuesday, 29 December 2015

Update Index in Elasticsearch

Whenever there is a change to be done in an indexed document, it is not mandatory to replace the entire document, instead elasticsearch allows the partial update of required field in it.
The create index functionality will completely replace the existing details, whereas the update functionality will allow us to partially change the document.

REST Service to update the indexed document:
POST http://localhost:9200/index_name/type_name/document_Id/_update
{
    "doc":{
        "field_name":"new_value"
    }
}

Java API to update the indexed document:

UpdateRequest updateRequest = new UpdateRequest();
updateRequest.index(“index_Name”);
updateRequest.type(“type_Name”);
updateRequest.id(“document_id”);
Map<String, Object> json = new HashMap<String, Object>();
json.put(“field_name”,”field_Value”);
updateRequest.doc(json);
try {
            client.update(updateRequest).get();
} catch (InterruptedException | ExecutionException e) {
            e.printStackTrace();
}

Note :  More than one field can be updated using a single method call.


Thanks !! Meet you soon with next post !!

No comments:

Post a Comment