# Java: Getting the average of a calculation

**URL:** https://discuss.elastic.co/t/java-getting-the-average-of-a-calculation/190619
**Category:** Elasticsearch
**Created:** [July 15, 2019, 10:31pm UTC](https://discuss.elastic.co/t/java-getting-the-average-of-a-calculation/190619 "2019-07-15T22:31:28Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![tzn01](https://avatars.discourse-cdn.com/v4/letter/t/7bcc69/32.png) [@tzn01](https://discuss.elastic.co/u/tzn01)
#### Post date: [July 15, 2019, 10:31pm UTC](https://discuss.elastic.co/t/java-getting-the-average-of-a-calculation/190619/1 "2019-07-15T22:31:28Z")

</div>

Hi,

I am using ElasticSearch 6.3 and I want to do a calculation on two fields and get the average of the results.

First I tried with org.elasticsearch.client : transport as noted in the doc, but noticed some things were deprecated in favor of the high level rest api.

So I tried with this:

```
RestHighLevelClient client = new RestHighLevelClient(
	RestClient.builder(
    	new HttpHost("hostname", 443, "https")));

```

I tried a simple subtract:

```
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder(); 
 searchSourceBuilder.query(QueryBuilders.scriptQuery(new Script("doc['someFieldName'].value - doc['someOtherFieldName'].value")));

SearchRequest searchRequest = new SearchRequest(); 
searchRequest.source(searchSourceBuilder);
SearchResponse searchResponse = client.search(searchRequest);

SearchHits hits = searchResponse.getHits();
SearchHit[] searchHits = hits.getHits();
for (SearchHit hit : searchHits) {
System.out.println(hit.toString());
	}

client.close();

```

I got a huge amount of errors like:  
"search\_phase\_execution\_exception","reason":"all shards failed"

Even when I added a simpler script I got the same:  
`searchSourceBuilder.query(QueryBuilders.scriptQuery(new Script("doc['someFieldName'].value")));`

So the questions are:  
-How to specify in the above which index I am searching in?  
-How to add other fields to filter to the above? Like I want to add a filter to search only where the xyzField=TestValue  
-How to specify a time range in the mentioned query?

Many thanks,  
TZ

---

<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: [July 16, 2019, 1:35am UTC](https://discuss.elastic.co/t/java-getting-the-average-of-a-calculation/190619/2 "2019-07-16T01:35:26Z")

</div>

Your script does not generate a boolean value AFAICS. I believe this is the problem.  
Note that if you share the full error message we might be able to tell more or confirm.

Few advices:

- before going to the java code, I'd try to solve everything with a simple Kibana script which runs in the dev console. Once you have it working, you can work on translating the code to Java. We can help for that.
- do not use scripts if you can avoid them. Like if you can compute things at index time like x = b - a, that will be a great win.
- upgrade. 6.3 is super old. Go to 6.8 or better 7.2. Also the java rest client is completely implemented in 6.8/7.2 which might not be the case with a 6.3 client.

---

<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: [August 13, 2019, 1:40am UTC](https://discuss.elastic.co/t/java-getting-the-average-of-a-calculation/190619/3 "2019-08-13T01:40:40Z")

</div>

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