Spring 用于聚合的数据 elasticsearch 命名查询

Spring data elasticsearch named queries for agreggation

我有大量聚合的查询,因此,我正在尝试对 elasticsearch 使用命名查询。我设法从属性文件中读取了查询,但无法读取聚合。有没有办法从属性文件中读取整个查询和查询聚合?

读取查询的工作示例:

QueryRepository:

public interface UserRepository extends ExtendedElasticsearchRepository<User, Integer> {
    //named query lookup found getUserQuery inside users-named-queries.properties and used its query
    SearchHits<User> getUserQuery(Integer fieldId);
}

方法调用:

SearchHits<User> users = userRepository.getUserQuery(1); <- returns hits
Aggregations usersAggregations = users.getAggregations(); <- returns null beacouse there is no aggregation

应用程序:

@EnableElasticsearchRepositories(basePackages = "org...", repositoryBaseClass = CdpElasticsearchRepository.class, namedQueriesLocation = "classpath:*-named-queries.properties")

用户命名-queries.properties

User.getUserQuery={"bool": { "filter": [{"term": {"fieldId": ?0}}]}}

理想行为:

User.getUserQuery={"query":{"bool": { "filter": [{"term": {"fieldId": ?0}}]}}, "aggs": {"sum_field": {"sum": {"field": "fieldId"}}}}

错误

org.springframework.data.elasticsearch.UncategorizedElasticsearchException: Elasticsearch exception [type=parsing_exception, reason=unknown query [query]]; nested exception is ElasticsearchStatusException[Elasticsearch exception [type=parsing_exception, reason=unknown query [query]]]; nested: ElasticsearchException[Elasticsearch exception [type=named_object_not_found_exception, reason=[1:10] unknown field [query]]];

好的,我学到了一些我还不知道的关于 Spring Data Elasticsearch 的知识:您可以在查询属性文件中定义查询,作为使用 @Query 注释函数的替代方法。谢谢你让我参与其中。

这些查询与使用 @Query 注释时相同,这意味着只能将发送到 Elasticsearch 的搜索请求的 query 部分放在那里,而不是 aggs 或其他可能进入搜索请求正文的内容。

命名查询的值在 wrapper query 中发送到 Elasticsearch,它将只包含查询部分而不是 aggs

我同意能够添加聚合或其他元素会很棒,我为此创建了 an issue