Consider the following elasticsearch query :
{
"query": {"match_all": {}},
"size": 0,
"aggs": {
"Terms": { "terms": { "field":"fileName" }
}
}
}
Here I'm just interested in the aggregation and not in the documents. That's why I set size:0
and it works as expected. However I'm unable to achieve the same with spring-data
. Code sample :
PageRequest page = new PageRequest(0, 0);
SearchQuery searchQuery = new NativeSearchQueryBuilder()
.withIndices(indexName).withTypes(typeName)
.withQuery(queryBuilder).withAggregation(aggsBuilder)
.withPageable(pageable).build();
This PageRequest
constructor throws an exception then, which is from its parent:
public AbstractPageRequest(int page, int size) {
if (page < 0) {
throw new IllegalArgumentException("Page index must not be less than zero!");
}
if (size < 1) {
throw new IllegalArgumentException("Page size must not be less than one!");
}
this.page = page;
this.size = size;
}
Question : Is there any way in spring data to limit document size to zero ?
We can define empty pageable interface implementation class, like this:
public static class EmptyPage implements Pageable {
public static final EmptyPage INSTANCE = new EmptyPage();
@Override
public int getPageNumber() {
return 0;
}
@Override
public int getPageSize() {
return 0;
}
@Override
public int getOffset() {
return 0;
}
@Override
public Sort getSort() {
return null;
}
@Override
public Pageable next() {
return null;
}
@Override
public Pageable previousOrFirst() {
return null;
}
@Override
public Pageable first() {
return null;
}
@Override
public boolean hasPrevious() {
return false;
}
}
Use:
SearchQuery searchQuery = new NativeSearchQueryBuilder()
.withQuery(queryBuilder)
.withIndices(indexName).withTypes(typeName)
.addAggregation(aggsBuilder)
.withPageable(EmptyPage.INSTANCE)
.build();
Result:
{
"aggregations": { ... },
"from": 0,
"query": { ... },
"size": 0
}
If you're using ES 1.x, what you can do is leave out the Pageable
and specify the search type COUNT
instead.
SearchQuery searchQuery = new NativeSearchQueryBuilder()
.withIndices(indexName).withTypes(typeName)
.withQuery(queryBuilder).withAggregation(aggsBuilder)
.withSearchType(SearchType.COUNT).build();
As of ES 2.x, SearchType.COUNT
will be deprecated and not available anymore, but for ES 1.x needs this should do the job.
Note that there's a similar need for other users, too, but the issue is still open.
From Spring Data Elasticsearch 4.0 on, NativeSearchQuery
, StringQuery
and CriteriaQuery
have a method setMaxResults(Integer maxResults)
so the query would be
NativeSearchQuery searchQuery = new NativeSearchQueryBuilder()
.withIndices(indexName).withTypes(typeName)
.withQuery(queryBuilder).withAggregation(aggsBuilder)
.build();
searchQuery.setMaxResults(0);
The setMaxResults()
method returns void
so can't be used with the builder syntax
This will add "size":0
at the top of the request object
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With