我正在使用以下 java 代码查询 elasticsearch 7.3 并获取 1 个文档。我已将其设置为只有 1 个得分最高的文档。它工作正常,并完美地返回了我的文档。
@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();
}
响应如下,我想使用 java 代码访问值Calories , Fat , Protein , Carbohydrate
并忽略 other 的值Nutrient
。我需要 4 个变量中的值,就像String caloriesVar=153 kcal
其他 3 个变量一样。
[