I'm migrating my application from Elasticsearch 7.15 version to 8.x but I'm facing some issues while writing aggregation with script query using SearchTemplateRequest with Java API Client because after 7.15 version they removed some methods from the SearchTemplateResponse that include aggregation method as well now I'm trying to get the aggregation response from the script query by using 8.x version way but not able to replace. If anyone can help me with us then it will be a great help for me.
Below is the old code of the method (7.15 v):
public SearchTemplateResponse getScriptResponse(String indexName, String scriptName,
Map<String, Object> scriptParams) throws IOException {
SearchTemplateRequest request = null;
SearchTemplateResponse response = null;
try {
SearchTemplateRequest request = new SearchTemplateRequest();
request.setRequest(new SearchRequest(new String[] { indexName }, trackTotalHitsSourceBuilder()));
request.setScriptType(ScriptType.STORED);
request.setScript(scriptName);
request.setScriptParams(scriptParams);
request.getRequest().source().trackTotalHits();
return request;
response = getClient().searchTemplate(request, RequestOptions.DEFAULT);
} catch (Exception e) {
throw e;
}
return response;
}
Somewhere in the code i'm using below aggregation method of the older SearchTemplateResponse class but now in the 8.x its not available.
templateResponse.getResponse().getAggregations().get(DATE_HIST_AGG_NAME);
Please someone help me to migrate the above method into Elasticsearch 8.x version.
I tired to migrate the above method like this but it is still not returning the aggregation response which I need as per my existing implementation
public SearchTemplateResponse getScriptResponse(String indexName, String scriptName,
Map<String, Object> scriptParams) throws IOException {
Map<String, JsonData> params = new HashMap<>(scriptParams.size());
for (String key : scriptParams.keySet()){
params.put(key, JsonData.of(scriptParams.get(key)));
}
SearchTemplateRequest searchTemplateRequest = new SearchTemplateRequest.Builder()
.index(indexName).params(params).source(scriptName).build();
return getClient().searchTemplate(searchTemplateRequest,SearchTemplateResponse.class);
}