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

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

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

1 Like

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

1 Like

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