I am trying to get all the different values for a certain field (e.g. "name") in ElasticSearch with SpringData.
As a first approach, I have this JSON which does what I want:
{
"aggs" : {
"nameAgg" : {
"terms" : { "field" : "name.key", "size":10000 }
}
},
"size":0
}
It works fine if I perform a GET over the index, retrieving data like this:
"aggregations": {
"nameAgg": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [
{
"key": "Paul",
"doc_count": 12
},
{
"key": "John",
"doc_count": 7
}
]
}
}
Since I only need all the distinct values for the field, this suits my need. Now, I am trying to achieve this in Java with spring-data-elasticsearch.
The closest try I did was this:
AbstractAggregationBuilder<TermsAggregationBuilder> agBuilder = AggregationBuilders.terms("name.key").field("name.key").size(10000);
Query query = new NativeSearchQueryBuilder().withQuery(QueryBuilders.matchAllQuery()).addAggregation(agBuilder).build();
But the output contains all the data for each indexed document and it does not match my expected output. How could I execute this type of request with spring-data-elasticsearch?
UPDATE: I have tried to specify a Pageable argument to the query, just like this:
AbstractAggregationBuilder<TermsAggregationBuilder> agBuilder = AggregationBuilders.terms("name.key").field("name.key");
Query query = new NativeSearchQueryBuilder()
.withPageable(PageRequest.of(0, 0))
.withQuery(QueryBuilders.matchAllQuery())
.addAggregation(agBuilder).build();
But then I receive this exception:
org.springframework.web.util.NestedServletException: Request processing failed; nested exception is java.lang.IllegalArgumentException: Page size must not be less than one!
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1014)
at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:898)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:497)
Caused by: java.lang.IllegalArgumentException: Page size must not be less than one!
at org.springframework.data.domain.AbstractPageRequest.<init>(AbstractPageRequest.java:50)
at org.springframework.data.domain.PageRequest.<init>(PageRequest.java:43)
at org.springframework.data.domain.PageRequest.of(PageRequest.java:70)
at org.springframework.data.domain.PageRequest.of(PageRequest.java:58)
Query
? - P.J.Meisch