1

I am using BulkProcessor of Elasticsearch Java api for insert/update/delete documents in index. Following methods works well for inserts and deletes

bulkProcessor.add(indexRequest(index).type(type).id(id).source(document))
bulkProcessor.add(deleteRequest(index).type(type).id(id));

Could indexRequest be used to update partial document in index. Say in elasticsearch index, I have a document

{
  "_id": "abcdefghijk",
  "id": "1",
  "title": "Harry Potter",
  "description": "Description for Harry Potter",
  "price": 10,
  "category": "Book"
}  

Is it possible to only update price attribute in the document, say I want to change price to {"price":15} using indexRequest or with any other methods in api

2 Answers 2

1

The following code shall solve the problem.

   final BulkRequestBuilder bulkRequest = esClient.prepareBulk();

   //start loop for multiple products

   final XContentBuilder contentBuilder = XContentFactory.jsonBuilder();
    try {

      contentBuilder.startObject();
      contentBuilder.field("price", 20);
      contentBuilder.endObject();

    } catch (final IOException ex) {
      ex.printStackTrace();
    }
  bulkRequest.add(esClient.prepareUpdate(index, type,_id).setDetectNoop(false)
          .setDoc(contentBuilder));
    //end loop

    BulkResponse bulkResponse = bulkRequest.execute().actionGet();
Sign up to request clarification or add additional context in comments.

1 Comment

This is bulkRequest but I think its not possible with bulkProcessor using indexRequest
1
bulkProcessor.add(new UpdateRequest(indexName, type, docId).doc(document).upsert(document));

If the document id does not exist, the content of the upsert element will be used to index a new document, else if already exist, the content of doc element used to update document.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.