我在Spring Boot示例中为在Docker上运行的弹性搜索编写范围查询时遇到问题。
我尝试编写RangeQuery但无法实现它elasticsearchClient.search.
我怎样才能做到这一点?
这是下面显示的代码片段
public List<Item> searchItemsByPriceRange(double minPrice, double maxPrice) throws IOException {
RangeQuery rangeQuery = new RangeQuery.Builder()
.field("price") // Specify the field name here
.gte(JsonData.of(minPrice)) // Greater than or equal to minPrice
.lte(JsonData.of(maxPrice)) // Less than or equal to maxPrice
.build();
log.info("Elasticsearch query: {}", rangeQuery.toString());
SearchResponse<Item> response = elasticsearchClient.search(q ->
q.index("itemindex").query(rangeQuery), Item.class);
log.info("Elasticsearch response: {}", response.toString());
return extractItemsFromResponse(response);
}
private List<Item> extractItemsFromResponse(SearchResponse<Item> response) {
return response
.hits()
.hits()
.stream()
.map(Hit::source)
.collect(Collectors.toList());
}
这是问题部分
SearchResponse<Item> response = elasticsearchClient.search(q ->
q.index("itemindex").query(rangeQuery), Item.class);
Cannot resolve method 'query(RangeQuery)'
Query.Builder
您需要在方法中使用 aquery()
,在此您可以设置rangeQuery