Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set result size zero in spring data elasticsearch

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 ?

like image 382
blackSmith Avatar asked May 13 '16 10:05

blackSmith


3 Answers

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
}
like image 193
Alex Avatar answered Oct 02 '22 16:10

Alex


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.

like image 28
Val Avatar answered Oct 02 '22 14:10

Val


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

like image 33
muttonUp Avatar answered Oct 02 '22 15:10

muttonUp