# Elasticsearch Java Client Aggregation Exception - all shards failed

**URL:** <https://discuss.elastic.co/t/elasticsearch-java-client-aggregation-exception-all-shards-failed/349860>\
**Category:** Elasticsearch\
**Tags:** language-clients\
**Created:** [December 22, 2023, 2:32pm UTC](https://discuss.elastic.co/t/elasticsearch-java-client-aggregation-exception-all-shards-failed/349860 "2023-12-22T14:32:01Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![bharath.krishn2](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/bharath.krishn2/32/130240_2.png) [@bharath.krishn2](https://discuss.elastic.co/u/bharath.krishn2)\
**Post date:** [December 22, 2023, 2:32pm UTC](https://discuss.elastic.co/t/elasticsearch-java-client-aggregation-exception-all-shards-failed/349860/1 "2023-12-22T14:32:01Z")

</div>

Hi,

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

> **[Aggregations | Elasticsearch Java API Client \[8.11\] | Elastic](https://www.elastic.co/guide/en/elasticsearch/client/java-api-client/current/aggregations.html)**

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();
	}

```

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [December 22, 2023, 3:13pm UTC](https://discuss.elastic.co/t/elasticsearch-java-client-aggregation-exception-all-shards-failed/349860/2 "2023-12-22T15:13:41Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![ltrotta](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ltrotta/32/124003_2.png) [@ltrotta](https://discuss.elastic.co/u/ltrotta)\
**Post date:** [December 22, 2023, 3:15pm UTC](https://discuss.elastic.co/t/elasticsearch-java-client-aggregation-exception-all-shards-failed/349860/3 "2023-12-22T15:15:06Z")

</div>

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:

```auto
...
 .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:

```auto
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 😃

Hope this helps, have a nice day!

---

<div class="post-metadata">

**Author:** ![bharath.krishn2](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/bharath.krishn2/32/130240_2.png) [@bharath.krishn2](https://discuss.elastic.co/u/bharath.krishn2)\
**Post date:** [December 26, 2023, 5:49am UTC](https://discuss.elastic.co/t/elasticsearch-java-client-aggregation-exception-all-shards-failed/349860/4 "2023-12-26T05:49:17Z")

</div>

Hi @dadoonet

The below is the query executed from Kibana Dev Tools

```auto
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
        }
      ]
    }
  }

```

---

<div class="post-metadata">

**Author:** ![bharath.krishn2](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/bharath.krishn2/32/130240_2.png) [@bharath.krishn2](https://discuss.elastic.co/u/bharath.krishn2)\
**Post date:** [December 26, 2023, 5:55am UTC](https://discuss.elastic.co/t/elasticsearch-java-client-aggregation-exception-all-shards-failed/349860/5 "2023-12-26T05:55:03Z")

</div>

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.

```auto
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)

```

---

<div class="post-metadata">

**Author:** ![bharath.krishn2](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/bharath.krishn2/32/130240_2.png) [@bharath.krishn2](https://discuss.elastic.co/u/bharath.krishn2)\
**Post date:** [December 26, 2023, 5:59am UTC](https://discuss.elastic.co/t/elasticsearch-java-client-aggregation-exception-all-shards-failed/349860/6 "2023-12-26T05:59:04Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![bharath.krishn2](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/bharath.krishn2/32/130240_2.png) [@bharath.krishn2](https://discuss.elastic.co/u/bharath.krishn2)\
**Post date:** [December 26, 2023, 6:28am UTC](https://discuss.elastic.co/t/elasticsearch-java-client-aggregation-exception-all-shards-failed/349860/7 "2023-12-26T06:28:14Z")

</div>

@ltrotta ,

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

```auto
{
"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.

 ![image](https://us1.discourse-cdn.com/elastic/original/3X/e/2/e2c210c423a5ba13f094c623e68687bc73d95482.png)

---

<div class="post-metadata">

**Author:** ![bharath.krishn2](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/bharath.krishn2/32/130240_2.png) [@bharath.krishn2](https://discuss.elastic.co/u/bharath.krishn2)\
**Post date:** [December 26, 2023, 7:10am UTC](https://discuss.elastic.co/t/elasticsearch-java-client-aggregation-exception-all-shards-failed/349860/8 "2023-12-26T07:10:28Z")

</div>

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.

---

<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:** [January 23, 2024, 7:11am UTC](https://discuss.elastic.co/t/elasticsearch-java-client-aggregation-exception-all-shards-failed/349860/9 "2024-01-23T07:11:09Z")

</div>

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