I am using the following java code to query the elasticsearch 7.3 and get 1 document. I have set it to only 1 doc with the highest score. It is working fine and returning me the doc perfectly.
@Autowired
RestHighLevelClient client;
@RequestMapping(value = "/search", method = RequestMethod.GET)
public @ResponseBody
String getItem(@RequestParam("string") String string) throws IOException {
QueryBuilder matchQueryBuilder = QueryBuilders.simpleQueryStringQuery(string);
SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
sourceBuilder.query(matchQueryBuilder);
sourceBuilder.from(0);
sourceBuilder.size(1);
sourceBuilder.timeout(new TimeValue(60, TimeUnit.SECONDS));
SearchRequest searchRequest = new SearchRequest("nutrients");
searchRequest.source(sourceBuilder);
SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);
return searchResponse.toString();
}
The response is as follow, I want to access the values using java code of Calories , Fat , Protein , Carbohydrate
and ignore the values of other Nutrient
.
I need the values in 4 variables like String caloriesVar=153 kcal
and similarly for the other 3.
[