Search code examples
elasticsearchspring-data-elasticsearch

Spring data elasticsearch named queries for agreggation


I have query with large aggregation and because of that, I'm trying to use named queries for elasticsearch. I managed to read the query from the properties file but I'm unable to read aggregation. Is there a way to read the entire query and queries aggregation from the properties file?

Working example of reading query:

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);
}

Method call:

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

App:

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

users-named-queries.properties

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

Desirable behavior:

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

Error

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]]];


Solution

  • Ok, I learned something about Spring Data Elasticsearch which I did not yet know: You can define queries in a queries properties file as an alternative to use @Query annotated functions. Thanks for pushing me into that.

    These queries are the same as when using the @Query annotation and this means that only the query part of a search request sent to Elasticsearch can be put in there, not the aggs or what else might go into the body of a search request.

    The value of the named query is sent to Elasticsearch in a wrapper query which will only take the query part and not aggs.

    I agree that it would be great to be able to add aggs or other elements, I created an issue for that,.