0

I can create the following string saved in a Java String object called updates.

{ "update":{ "_index":"myindex", "_type":"order", "_id":"1"} }
{ "doc":{"field1" : "aaa", "field2" : "value2" }}
{ "update":{ "_index":"myindex", "_type":"order", "_id":"2"} }
{ "doc":{"field1" : "bbb", "field2" : "value2" }}
{ "update":{ "_index":"myindex", "_type":"order", "_id":"3"} }
{ "doc":{"field1" : "ccc", "field2" : "value2" }}

Now I want to do bullk update within a Java program:

Client client = getClient(); //TransportClient

BulkRequestBuilder bulkRequest = client.prepareBulk();
//?? how to attach updates variable to bulkRequest?

BulkResponse bulkResponse = bulkRequest.execute().actionGet();

I am unable to find a way to attach the above updates variable to bulkRequest before execute.

I notice that I am able to add UpdateRequest object to bulkRequest, but it seems to add only one document one time. As indicated above, I have multiple to-be-updated document in one string.

Can someone enlighten me on this? I have a gut feel that I may do things wrong way.

Thanks and regards.

2
  • instead of parsing, creating the string and trying to attach this string as a bulk.. you can attach to bulkRequest while parsing itself.. ?? i dont think there is something which you are looking for.. even if there is.. it does the same thing internally.. dont you think? Commented Mar 15, 2015 at 5:56
  • karthik. Thanks for chiming in!! Commented Mar 15, 2015 at 12:36

2 Answers 2

3

The following code should work fine for you. For each document updation , you need to create a separate update request as below and keep on adding it to the bulk requests. Once the bulk requests is ready , execute a get on it.

JSONObject obj = new JSONObject();
obj.put("field1" , "value1");
obj.put("field2" , "value2");
UpdateRequest updateRequest = new UpdateRequest(index, indexType, id1).doc(obj.toString());
BulkRequestBuilder bulkRequest = client.prepareBulk();
bulkRequest.add(updateRequest);


obj = new JSONObject();
obj.put("fieldX" , "value1");
obj.put("fieldY" , "value2");
updateRequest = new UpdateRequest(index, indexType, id2).doc(obj.toString());
bulkRequest = client.prepareBulk();
bulkRequest.add(updateRequest);


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

3 Comments

Vineeth, by "new UpdateRequest(index, indexType, id)", which document are trying to update? I am hoping to add multiple document string in one statement. Not sure Elasticsearch allows me to do so.
This works, because this is what I am already doing. It appears that my initial attempt is not supported in Elasticsearch.
How can we add filterquery to update specific documents ?
0

I ran into the same problem where only 1 document get updated in my program. Then I found the following way which worked perfectly fine. This uses spring java client. I have also listed the the dependencies I used in the code.

import org.elasticsearch.action.update.UpdateRequest;

import org.elasticsearch.index.query.QueryBuilder;

import org.springframework.data.elasticsearch.core.query.UpdateQuery;

import org.springframework.data.elasticsearch.core.query.UpdateQueryBuilder;

private UpdateQuery updateExistingDocument(String Id) {
    // Add updatedDateTime, CreatedDateTime, CreateBy, UpdatedBy field in existing documents in Elastic Search Engine
    UpdateRequest updateRequest = new UpdateRequest().doc("UpdatedDateTime", new Date(), "CreatedDateTime", new Date(), "CreatedBy", "admin", "UpdatedBy", "admin");

    // Create updateQuery
    UpdateQuery updateQuery = new UpdateQueryBuilder().withId(Id).withClass(ElasticSearchDocument.class).build();
    updateQuery.setUpdateRequest(updateRequest);

    // Execute update
     elasticsearchTemplate.update(updateQuery);
}

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.