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