Elasticsearch Java Client Aggregation Exception - all shards failed

Hi,

I'm trying to create an aggregation on Elasticsearch through Java client using this below link

But I'm getting the exception:
co.elastic.clients.elasticsearch._types.ElasticsearchException: [es/search] failed: [search_phase_execution_exception] all shards failed

The index is having only 1233 documents and all the shards in the cluster are active.
When I execute the query using Dev Tools in Kibana its working. But from Java its failing. Please help.

Below is my code

	RestClient restClient = RestClient.builder(
			new HttpHost("XXXX", 9200)).build();
	ElasticsearchTransport transport = new RestClientTransport(
			restClient, new JacksonJsonpMapper());
	ElasticsearchClient client = new ElasticsearchClient(transport);
	
	try {
		String searchText = "iPhone 12";

		Query query = MatchQuery.of(m -> m
		    .field("deviceDetails.deviceBrandModel.keyword")
		    .query(searchText)
		)._toQuery();
		
		SearchResponse<Void> response = client.search(b -> b
				.index(indexName)
				.size(1233)
				.query(query)
				.aggregations("device-name-histogram", a -> a 
						.histogram(h -> h 
								.field("deviceDetails.deviceBrandModel.keyword")
								)),Void.class);
		List<HistogramBucket> buckets = response.aggregations().get("device-name-histogram").histogram().buckets().array();
		for (HistogramBucket bucket: buckets) {
		    logger.info( bucket.docCount() + " : " + bucket.key());
		    
		}
	} catch (ElasticsearchException | IOException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}

What does the query look like in Kibana Dev Tools? What is the full elasticsearch error please?

Hello Bharath,

It looks like the aggregation code is missing the 'interval' parameter, which defines the width of each bucket. You can define it like this:

...
 .aggregations("device-name-histogram", a -> a
        .histogram(h -> h
                .field("deviceDetails.deviceBrandModel.keyword")
                .interval(20.0) // <- here! 
                )),Void.class);
...

Feel free to change the value to match your dataset better.

Regarding the 'all shards failed' exception, you can usually find a more in depth explanation of the error in the response, for example:

catch (ElasticsearchException e){
    e.response().error();
}

Which in this case would return this:
... [{"type":"illegal_argument_exception","reason":"interval must be positive, got: 0.0"}]}

Which explains it much better :smiley:

Hope this helps, have a nice day!

1 Like

Hi @dadoonet

The below is the query executed from Kibana Dev Tools

POST /mtn-catalog/_search
{
  "aggs": {
    "deviceBrandModel": {
      "terms": {"field":"deviceDetails.deviceBrandModel.keyword"}
    }
  }
}

Response:

  "aggregations" : {
    "deviceBrandModel" : {
      "doc_count_error_upper_bound" : 0,
      "sum_other_doc_count" : 615,
      "buckets" : [
        {
          "key" : "iPhone 13",
          "doc_count" : 76
        },
        {
          "key" : "iPhone 14",
          "doc_count" : 72
        },
        {
          "key" : "iPhone 14 Plus",
          "doc_count" : 72
        },
        {
          "key" : "iPhone 14 Pro",
          "doc_count" : 72
        },
        {
          "key" : "iPhone 13 mini",
          "doc_count" : 66
        },
        {
          "key" : "iPhone 14 Pro Max",
          "doc_count" : 60
        },
        {
          "key" : "iPhone 12",
          "doc_count" : 56
        },
        {
          "key" : "Galaxy A13",
          "doc_count" : 34
        },
        {
          "key" : "Galaxy A13 5G",
          "doc_count" : 34
        },
        {
          "key" : "Galaxy A22 LTE",
          "doc_count" : 34
        }
      ]
    }
  }

Hi @dadoonet ,

This is the whole exception stacktrace I got. Also there is no excetpions/errors printed in Elasticsearch logs.
When I inspected the cluster health, all the shards are active. Also when I execute the same aggregation on the same index using Kibana Dev Tools, there is no issue. I have posted the response in my above comment.

2023-12-26T11:22:03.467+05:30  INFO 26808 --- [nio-8085-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring DispatcherServlet 'dispatcherServlet'
2023-12-26T11:22:03.468+05:30  INFO 26808 --- [nio-8085-exec-1] o.s.web.servlet.DispatcherServlet        : Initializing Servlet 'dispatcherServlet'
2023-12-26T11:22:03.475+05:30  INFO 26808 --- [nio-8085-exec-1] o.s.web.servlet.DispatcherServlet        : Completed initialization in 7 ms
2023-12-26T11:22:20.806+05:30  WARN 26808 --- [nio-8085-exec-1] org.elasticsearch.client.RestClient      : request [POST http://nlaatgrvq09.mtn.co.za:9200/mtn-catalog/_search?typed_keys=true] returned 1 warnings: [299 Elasticsearch-7.16.1-5b38441b16b1ebb16a27c107a4c3865776e20c53 "Elasticsearch built-in security features are not enabled. Without authentication, your cluster could be accessible to anyone. See https://www.elastic.co/guide/en/elasticsearch/reference/7.16/security-minimal-setup.html to enable security."]
co.elastic.clients.elasticsearch._types.ElasticsearchException: [es/search] failed: [search_phase_execution_exception] all shards failed
	at co.elastic.clients.transport.rest_client.RestClientTransport.getHighLevelResponse(RestClientTransport.java:334)
	at co.elastic.clients.transport.rest_client.RestClientTransport.performRequest(RestClientTransport.java:154)
	at co.elastic.clients.elasticsearch.ElasticsearchClient.search(ElasticsearchClient.java:1882)
	at co.elastic.clients.elasticsearch.ElasticsearchClient.search(ElasticsearchClient.java:1899)
	at mtn.catalog.manager.MTNOffersManager.aggregator(MTNOffersManager.java:212)
	at mtn.catalog.controller.OffersController.deviceBrands(OffersController.java:103)
	at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77)
	at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.base/java.lang.reflect.Method.invoke(Method.java:568)
	at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:205)
	at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:150)
	at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:118)
	at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:884)
	at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:797)
	at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:87)
	at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1081)
	at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:974)
	at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1014)
	at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:903)
	at jakarta.servlet.http.HttpServlet.service(HttpServlet.java:564)
	at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:885)
	at jakarta.servlet.http.HttpServlet.service(HttpServlet.java:658)
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:205)
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:149)
	at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:51)
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:174)
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:149)
	at org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:100)
	at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:116)
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:174)
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:149)
	at org.springframework.web.filter.FormContentFilter.doFilterInternal(FormContentFilter.java:93)
	at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:116)
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:174)
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:149)
	at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:201)
	at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:116)
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:174)
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:149)
	at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:167)
	at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:90)
	at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:482)
	at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:115)
	at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:93)
	at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:74)
	at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:340)
	at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:391)
	at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:63)
	at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:896)
	at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1744)
	at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:52)
	at org.apache.tomcat.util.threads.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1191)
	at org.apache.tomcat.util.threads.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:659)
	at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
	at java.base/java.lang.Thread.run(Thread.java:833)

Hi @ltrotta ,

The index doesn't have any timestamp filed. It's just product data indexed on the elasticsearch and I wanted to get the number of devices based on the device model.
I believe the interval is not applicable in this aggregation.

@ltrotta ,

I tried to check the exception in detail. I got the below

{
"error":{
"phase":"query",
"failed_shards":[
{
"shard":0,
"index":"mtn-catalog",
"node":"vWwV2tcSTsq_oXPbMcV-dg",
"reason":{
"type":"illegal_argument_exception",
"reason":"Field [deviceDetails.deviceBrandModel.keyword] of type [keyword] is not supported for aggregation [histogram]"
}
}
],
"grouped":true,
"type":"search_phase_execution_exception",
"reason":"all shards failed",
"caused_by":{
"type":"illegal_argument_exception",
"reason":"Field [deviceDetails.deviceBrandModel.keyword] of type [keyword] is not supported for aggregation [histogram]",
"caused_by":{
"type":"illegal_argument_exception",
"reason":"Field [deviceDetails.deviceBrandModel.keyword] of type [keyword] is not supported for aggregation [histogram]"
}
},
"root_cause":[
{
"type":"illegal_argument_exception",
"reason":"Field [deviceDetails.deviceBrandModel.keyword] of type [keyword] is not supported for aggregation [histogram]"
}
]
},
"status":400
}

But when I checked the index mapping, the field - deviceDetails.deviceBrandModel.keyword is aggregatable.

Hi All,

Thanks for the reply. I realized the error now.
I should have used the Terms Aggregation instead of a Histogram.
I have corrected my code and its working now.

1 Like

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