How to update hashmap data in ElasticSearch using API Update in Java

I am trying to update data in Elastic Search Server using the following Code. The code is written on Java 1.8. and Elastic Search version 6.2.1

  1. I have used the following to insert the data in elastic search.

HashMap<String, String> hmExtra = new HashMap<String, String>();
hmExtra.put("One", "1");
hmExtra.put("Two", "2");

 HashMap<String, Object> data = new HashMap<String, Object>();
 data.put("case_name","Test");
 data.put("case_extra",hmExtra);


 IndexRequest indexRequest = new IndexRequest("org-8").type("Group").source(data);

 IndexResponse response = client.index(indexRequest, header);

When I queried the Elastic search using the key, the got back the following result:

 {
   "_index": "org-8",
   "_type": "Group",
   "_id": "AW4gTZwCAdqCPewR2ukW",
   "_version": 2,
   "found": true,
   "_source": {
     "case_name","Test",
     "case_extra": {
       "One": "1",
       "Two": "2"
     }
   }
 }

After the insertion of data(HashMap) it is inserted successfully into search.

  1. Trying to update the same.

After that when I tried to update the case_extra with below code. The search query was displaying the existing values.

 HashMap<String, String> hmExtra = new HashMap<String, String>(); 
 hmExtra.put("One", "9");

HashMap<String, object> data = new HashMap<String, Object>();
 data.put("case_name","Test2");
data.put("case_extra",hmExtra);


UpdateRequest request = new UpdateRequest("org-8", "Group", "AW4gTZwCAdqCPewR2ukW").doc(data);
UpdateResponse updateResponse = client.update(request);

Result:

 {
   "_index": "org-8",
   "_type": "Group",
   "_id": "AW4gTZwCAdqCPewR2ukW",
   "_version": 2,
   "found": true,
   "_source": {
     "case_name","Test2",
     "case_extra": {
       "One": "9",
       "Two": "2"
     }
   }
 }

Based on the above result, Key 'Two' was not removed from the case_extra.

I need help to use Api Update in elasticsearch, I tried the above code and the problem is the second value of the HashMap removed from case_extra.

Then I tried below code:

Before update request, the document looks like below:

{
  "_index": "org-8",
  "_type": "Group",
  "_id": "AW4cWb2g90EOmg5E-TFl",
  "_version": 22,
  "found": true,
  "_source": {
	"case_name": "Test1",
	"case_extra": {
	  "One": "1",
      "Two": "2"
	}
  }
}

Update Request code:

    RestHighLevelClient client = new RestHighLevelClient(
            RestClient.builder(new HttpHost("localhost", 9200,"http")));
    
    HashMap<String, String> hmExtra1 = new HashMap<String, String>();
    hmExtra1.put("One", "9");

    Map<String, Object> data1 = new HashMap<String, Object>();
    data1.put("case_extra",hmExtra1);
    data1.put("case_name", "Test2");

    Map<String, Object> parameters = Collections.singletonMap("val", data1);

    UpdateRequest request = new UpdateRequest("org-8", "Group", "AW4cWb2g90EOmg5E-TFl");

    Script script = new Script(ScriptType.INLINE, "painless",
                               "ctx._source = params.val", parameters);
    request.script(script);

    try {
		UpdateResponse updateResponse = client.update(request);
		System.out.println(updateResponse.getGetResult().sourceAsString());
	} catch (Throwable e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}

After I ran the above code, the error that I received is below:

ElasticsearchStatusException[Elasticsearch exception [type=illegal_argument_exception, reason=[script] unknown field [source], parser not found]]
	at org.elasticsearch.rest.BytesRestResponse.errorFromXContent(BytesRestResponse.java:177)
	at org.elasticsearch.client.RestHighLevelClient.parseEntity(RestHighLevelClient.java:618)
	at org.elasticsearch.client.RestHighLevelClient.parseResponseException(RestHighLevelClient.java:594)
	at org.elasticsearch.client.RestHighLevelClient.performRequest(RestHighLevelClient.java:501)
	at org.elasticsearch.client.RestHighLevelClient.performRequestAndParseEntity(RestHighLevelClient.java:474)
	at org.elasticsearch.client.RestHighLevelClient.update(RestHighLevelClient.java:353)
	at com.goclockwork.ontracksearchengine.test.UpdateSearchTest.main(UpdateSearchTest.java:54)
	Suppressed: org.elasticsearch.client.ResponseException: method [POST], host [http://localhost:9200], URI [/org-8/Group/AW4cWb2g90EOmg5E-TFl/_update?timeout=1m], status line [HTTP/1.1 400 Bad Request]
{"error":{"root_cause":[{"type":"illegal_argument_exception","reason":"[script] unknown field [source], parser not found"}],"type":"illegal_argument_exception","reason":"[script] unknown field [source], parser not found"},"status":400}
		at org.elasticsearch.client.RestClient$1.completed(RestClient.java:357)
		at org.elasticsearch.client.RestClient$1.completed(RestClient.java:346)
		at org.apache.http.concurrent.BasicFuture.completed(BasicFuture.java:119)
		at org.apache.http.impl.nio.client.DefaultClientExchangeHandlerImpl.responseCompleted(DefaultClientExchangeHandlerImpl.java:177)
		at org.apache.http.nio.protocol.HttpAsyncRequestExecutor.processResponse(HttpAsyncRequestExecutor.java:436)
		at org.apache.http.nio.protocol.HttpAsyncRequestExecutor.inputReady(HttpAsyncRequestExecutor.java:326)
		at org.apache.http.impl.nio.DefaultNHttpClientConnection.consumeInput(DefaultNHttpClientConnection.java:265)
		at org.apache.http.impl.nio.client.InternalIODispatch.onInputReady(InternalIODispatch.java:81)
		at org.apache.http.impl.nio.client.InternalIODispatch.onInputReady(InternalIODispatch.java:39)
		at org.apache.http.impl.nio.reactor.AbstractIODispatch.inputReady(AbstractIODispatch.java:114)
		at org.apache.http.impl.nio.reactor.BaseIOReactor.readable(BaseIOReactor.java:162)
		at org.apache.http.impl.nio.reactor.AbstractIOReactor.processEvent(AbstractIOReactor.java:337)
		at org.apache.http.impl.nio.reactor.AbstractIOReactor.processEvents(AbstractIOReactor.java:315)
		at org.apache.http.impl.nio.reactor.AbstractIOReactor.execute(AbstractIOReactor.java:276)
		at org.apache.http.impl.nio.reactor.BaseIOReactor.execute(BaseIOReactor.java:104)
		at org.apache.http.impl.nio.reactor.AbstractMultiworkerIOReactor$Worker.run(AbstractMultiworkerIOReactor.java:588)
		at java.lang.Thread.run(Unknown Source)

Dependency which I am using as below:

  <dependencies>
	<dependency>
		<groupId>org.elasticsearch.client</groupId>
		<artifactId>elasticsearch-rest-high-level-client</artifactId>
		<version>6.2.1</version>
	</dependency>
	<dependency>
		<groupId>org.apache.httpcomponents</groupId>
		<artifactId>httpclient</artifactId>
		<version>4.5</version>
	</dependency>
	<dependency>
		<groupId>com.fasterxml.jackson.core</groupId>
		<artifactId>jackson-databind</artifactId>
		<version>2.9.3</version>
	</dependency>
  </dependencies>

Any help is really appreciated.

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.