Unable to update document using java API?


(Sam-4) #1

Hi

My index contain the following sources:

Source Document ID

{id=1, address=xxx1, age=28, name= AB} 1
{id=2, address=xxx2, age=27, name= CD} 2
{id=3, address=xxx3, age=26, name= EF} 3
{id=4, address=xxx4, age=25 name= GH} 4

I want to update document id 1 name field AB to XYZ but it doesn't
update name field with XYZ.

used code:

        GetRequestBuilder getrequest = client.prepareGet();
        GetResponse getresponse =

getrequest.setIndex(indexName).setType(indexType).setId(documentId).setRefresh(true).execute().actionGet();
documentExistance=getresponse.exists();
System.out.println(documentExistance); //found
true if document exist in index

      if(documentExistance)
      {

         XContentBuilder jsonObject =

XContentFactory.jsonBuilder().startObject();
jsonObject.field("name").value("XYZ");
jsonObject.endObject();

         IndexRequestBuilder indexRequestBuilder = new

IndexRequestBuilder(client, indexName);
IndexRequestBuilder indexbulider= indexRequestBuilder
.setCreate(false)
.setId(documentId)
.setType(indexType)
.setSource(jsonObject )
.setRefresh(true);

          BulkRequestBuilder bulkRequestBuilder =

client.prepareBulk();
BulkResponse response =
bulkRequestBuilder.add(indexbulider).execute().actionGet();

please help where i'm doing wrong.

Thanks


(system) #2