# Elasticsearch API for Updating multiple fields for multiple documents that belongs to an index in Elasticsearch

**URL:** <https://discuss.elastic.co/t/elasticsearch-api-for-updating-multiple-fields-for-multiple-documents-that-belongs-to-an-index-in-elasticsearch/268515>\
**Category:** Elasticsearch\
**Created:** [March 26, 2021, 4:02pm UTC](https://discuss.elastic.co/t/elasticsearch-api-for-updating-multiple-fields-for-multiple-documents-that-belongs-to-an-index-in-elasticsearch/268515 "2021-03-26T16:02:01Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Srikanth\_Choppara](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/srikanth_choppara/32/86154_2.png) [@Srikanth\_Choppara](https://discuss.elastic.co/u/Srikanth_Choppara)\
**Post date:** [March 26, 2021, 4:02pm UTC](https://discuss.elastic.co/t/elasticsearch-api-for-updating-multiple-fields-for-multiple-documents-that-belongs-to-an-index-in-elasticsearch/268515/1 "2021-03-26T16:02:01Z")

</div>

I have a requirement to update fields of all the documents that were fetched based on come criteria or filter. There is one particular field which should be updated as appending to the existing content.

Is there any way where we can acheive this functionality? I am looking at UpdaeByQueryRequest and could'nt understand few things in the example

Following is my code snippet followed by the error i m getting

```
     UpdateByQueryRequest updateByQueryRequest = new UpdateByQueryRequest(INDEX_NAME);
     updateByQueryRequest.setConflicts("proceed");
     BoolQueryBuilder consolidatedQueryBuilder = new BoolQueryBuilder();
     
     // This will build consolidatedQueryBuilder based on the filter passed.
     buildCriteria(filterData, consolidatedQueryBuilder);
     updateByQueryRequest.setBatchSize(1000);
     updateByQueryRequest.setQuery(consolidatedQueryBuilder);
     Map<String, Object> updatesToSet = new HashMap<String, Object>();
     String inlineAction = "";
     switch (action) {
	case "ACKNOWLEDGE_ACTION":
		updatesToSet.put("ack", true);
		updatesToSet.put("ackedBy", remoteUser);
		updatesToSet.put("ackTimestamp", updateTimestamp);
		updateByQueryRequest.setScript(new Script(ScriptType.INLINE, "painless", inlineAction, updatesToSet));
		break;
	case "UNACKNOWLEDGE_ACTION":
		updatesToSet.put("ack", false);
		updatesToSet.put("ackedBy", "");
		updatesToSet.put("ackTimestamp", 0L);
		updateByQueryRequest.setScript(new Script(ScriptType.INLINE, "painless", inlineAction, updatesToSet));
		break;
	case "CLEAR_ACTION":
		updatesToSet.put("consideration", 0);
		updatesToSet.put("cancelledTimestamp", updateTimestamp);
		updatesToSet.put("cancelledBy", remoteUser);
		updateByQueryRequest.setScript(new Script(ScriptType.INLINE, "painless", inlineAction, updatesToSet));
		break;
	case "COMMENT_ACTION":
		inlineAction = "ctx._source.Comments += "+ parameterValue +"";
		updateByQueryRequest.setScript(new Script(ScriptType.INLINE, "painless", inlineAction, updatesToSet));
		break;
	case "ASSIGN_OWNERSHIP_ACTION":
		updatesToSet.put("owner", parameterValue);
		break;
	default:
		throw new IllegalArgumentException("Action not supported");
     }
     BulkByScrollResponse bulkResponse = null;
     makeConnection();
     try {
		bulkResponse = restHighLevelClient.updateByQuery(updateByQueryRequest, RequestOptions.DEFAULT);
		closeConnection();
	} catch (IOException e) {
		log.error("unable to update records for UI Bar Events");
	}

```

The error I am seeing if i execute the above code is

Servlet.service() for servlet [dispatcherServlet] in context with path threw exception [Request processing failed; nested exception is ElasticsearchStatusException[Elasticsearch exception [type=script\_exception, reason=compile error]]; nested: ElasticsearchException[Elasticsearch exception [type=illegal\_argument\_exception, reason=cannot resolve symbol [Testing]]];] with root cause

```
org.elasticsearch.ElasticsearchException: Elasticsearch exception [type=illegal_argument_exception, reason=cannot resolve symbol [Testing]]
at org.elasticsearch.ElasticsearchException.innerFromXContent(ElasticsearchException.java:485) ~[elasticsearch-7.11.1.jar:7.11.1]
at org.elasticsearch.ElasticsearchException.fromXContent(ElasticsearchException.java:396) ~[elasticsearch-7.11.1.jar:7.11.1]
at org.elasticsearch.ElasticsearchException.innerFromXContent(ElasticsearchException.java:426) ~[elasticsearch-7.11.1.jar:7.11.1]
at org.elasticsearch.ElasticsearchException.failureFromXContent(ElasticsearchException.java:592) ~[elasticsearch-7.11.1.jar:7.11.1]
at org.elasticsearch.rest.BytesRestResponse.errorFromXContent(BytesRestResponse.java:168) ~[elasticsearch-7.11.1.jar:7.11.1]
at org.elasticsearch.client.RestHighLevelClient.parseEntity(RestHighLevelClient.java:1900) ~[elasticsearch-rest-high-level-client-7.11.1.jar:7.11.1]
at org.elasticsearch.client.RestHighLevelClient.parseResponseException(RestHighLevelClient.java:1877) ~[elasticsearch-rest-high-level-client-7.11.1.jar:7.11.1]
at org.elasticsearch.client.RestHighLevelClient.internalPerformRequest(RestHighLevelClient.java:1634) ~[elasticsearch-rest-high-level-client-7.11.1.jar:7.11.1]
at org.elasticsearch.client.RestHighLevelClient.performRequest(RestHighLevelClient.java:1591) ~[elasticsearch-rest-high-level-client-7.11.1.jar:7.11.1]
at org.elasticsearch.client.RestHighLevelClient.performRequestAndParseEntity(RestHighLevelClient.java:1561) ~[elasticsearch-rest-high-level-client-7.11.1.jar:7.11.1]
at org.elasticsearch.client.RestHighLevelClient.updateByQuery(RestHighLevelClient.java:592) ~[elasticsearch-rest-high-level-client-7.11.1.jar:7.11.1]
at a.b.c.b.efghu.jklmn(efghu.java:952) ~[classes/:na]
at a.b.c.b.efghu$$FastClassBySpringCGLIB$$930e65a6.invoke(<generated>) ~[classes/:na]
at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:218) ~[spring-core-5.2.7.RELEASE.jar:5.2.7.RELEASE]
at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:771) ~[spring-aop-5.2.7.RELEASE.jar:5.2.7.RELEASE]
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:163) ~[spring-aop-5.2.7.RELEASE.jar:5.2.7.RELEASE]
at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.proceed(CglibAopProxy.java:749) ~[spring-aop-5.2.7.RELEASE.jar:5.2.7.RELEASE]
at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:139) ~[spring-tx-5.2.7.RELEASE.jar:5.2.7.RELEASE]
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186) ~[spring-aop-5.2.7.RELEASE.jar:5.2.7.RELEASE]
at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.proceed(CglibAopProxy.java:749) ~[spring-aop-5.2.7.RELEASE.jar:5.2.7.RELEASE]
at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:691) ~[spring-aop-5.2.7.RELEASE.jar:5.2.7.RELEASE]
at a.b.c.b.efghu$$EnhancerBySpringCGLIB$$c45b5357.UIBarEventsUpdate(<generated>) ~[classes/:na]
at com.rbbn.fm.service.EventsDBServiceImpl.UIBarEventsUpdate(EventsDBServiceImpl.java:120) ~[classes/:na]
at com.rbbn.fm.controller.FmEventDBApiController.UIBarEventsUpdate(FmEventDBApiController.java:80) ~[classes/:na]
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:na]
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:na]
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:na]
at java.base/java.lang.reflect.Method.invoke(Method.java:566) ~[na:na]
at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:190) ~[spring-web-5.2.7.RELEASE.jar:5.2.7.RELEASE]
at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:138) ~[spring-web-5.2.7.RELEASE.jar:5.2.7.RELEASE]
at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:105) ~[spring-webmvc-5.2.7.RELEASE.jar:5.2.7.RELEASE]
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:879) ~[spring-webmvc-5.2.7.RELEASE.jar:5.2.7.RELEASE]
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:793) ~[spring-webmvc-5.2.7.RELEASE.jar:5.2.7.RELEASE]
at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:87) ~[spring-webmvc-5.2.7.RELEASE.jar:5.2.7.RELEASE]
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1040) ~[spring-webmvc-5.2.7.RELEASE.jar:5.2.7.RELEASE]
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:943) ~[spring-webmvc-5.2.7.RELEASE.jar:5.2.7.RELEASE]
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1006) ~[spring-webmvc-5.2.7.RELEASE.jar:5.2.7.RELEASE]
at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:909) ~[spring-webmvc-5.2.7.RELEASE.jar:5.2.7.RELEASE]
at javax.servlet.http.HttpServlet.service(HttpServlet.java:660) ~[tomcat-embed-core-9.0.36.jar:9.0.36]
at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:883) ~[spring-webmvc-5.2.7.RELEASE.jar:5.2.7.RELEASE]
at javax.servlet.http.HttpServlet.service(HttpServlet.java:741) ~[tomcat-embed-core-9.0.36.jar:9.0.36]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231) ~[tomcat-embed-core-9.0.36.jar:9.0.36]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-9.0.36.jar:9.0.36]
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53) ~[tomcat-embed-websocket-9.0.36.jar:9.0.36]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) ~[tomcat-embed-core-9.0.36.jar:9.0.36]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-9.0.36.jar:9.0.36]
at org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:100) ~[spring-web-5.2.7.RELEASE.jar:5.2.7.RELEASE]
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119) ~[spring-web-5.2.7.RELEASE.jar:5.2.7.RELEASE]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) ~[tomcat-embed-core-9.0.36.jar:9.0.36]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-9.0.36.jar:9.0.36]
at org.springframework.web.filter.FormContentFilter.doFilterInternal(FormContentFilter.java:93) ~[spring-web-5.2.7.RELEASE.jar:5.2.7.RELEASE]
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119) ~[spring-web-5.2.7.RELEASE.jar:5.2.7.RELEASE]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) ~[tomcat-embed-core-9.0.36.jar:9.0.36]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-9.0.36.jar:9.0.36]
at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:201) ~[spring-web-5.2.7.RELEASE.jar:5.2.7.RELEASE]
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119) ~[spring-web-5.2.7.RELEASE.jar:5.2.7.RELEASE]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) ~[tomcat-embed-core-9.0.36.jar:9.0.36]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-9.0.36.jar:9.0.36]
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:202) ~[tomcat-embed-core-9.0.36.jar:9.0.36]
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96) ~[tomcat-embed-core-9.0.36.jar:9.0.36]
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:541) ~[tomcat-embed-core-9.0.36.jar:9.0.36]
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:139) ~[tomcat-embed-core-9.0.36.jar:9.0.36]
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92) ~[tomcat-embed-core-9.0.36.jar:9.0.36]
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:74) ~[tomcat-embed-core-9.0.36.jar:9.0.36]
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:343) ~[tomcat-embed-core-9.0.36.jar:9.0.36]
at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:373) ~[tomcat-embed-core-9.0.36.jar:9.0.36]
at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:65) ~[tomcat-embed-core-9.0.36.jar:9.0.36]
at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:868) ~[tomcat-embed-core-9.0.36.jar:9.0.36]
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1590) ~[tomcat-embed-core-9.0.36.jar:9.0.36]
at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49) ~[tomcat-embed-core-9.0.36.jar:9.0.36]
at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128) ~[na:na]
at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628) ~[na:na]
at
```

---

<div class="post-metadata">

**Author:** ![Srikanth\_Choppara](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/srikanth_choppara/32/86154_2.png) [@Srikanth\_Choppara](https://discuss.elastic.co/u/Srikanth_Choppara)\
**Post date:** [March 27, 2021, 2:02am UTC](https://discuss.elastic.co/t/elasticsearch-api-for-updating-multiple-fields-for-multiple-documents-that-belongs-to-an-index-in-elasticsearch/268515/2 "2021-03-27T02:02:25Z")

</div>

I have figured out how to use and completed the code. Now it is working as expected.

---

<div class="post-metadata">

**Author:** ![stephenb](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/stephenb/32/40856_2.png) [@stephenb](https://discuss.elastic.co/u/stephenb)\
**Post date:** [March 27, 2021, 2:05am UTC](https://discuss.elastic.co/t/elasticsearch-api-for-updating-multiple-fields-for-multiple-documents-that-belongs-to-an-index-in-elasticsearch/268515/3 "2021-03-27T02:05:19Z")

</div>

@Srikanth_Choppara  
Cool!  
Can you share your solution for others that may have the same issue?

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [April 24, 2021, 2:05am UTC](https://discuss.elastic.co/t/elasticsearch-api-for-updating-multiple-fields-for-multiple-documents-that-belongs-to-an-index-in-elasticsearch/268515/4 "2021-04-24T02:05:28Z")

</div>

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