6

In Spring Data Elasticsearch - I noticed something that looks like a bug with the generated findAllByFoo type methods. These seem to be limited to the default size (of 10) - which goes against the semantics you would expect from a findAll operation.

The main findAll() operation does work correctly however and does return all documents.

So - is this a bug or is there something I'm missing? Cheers, Eugen.

Eugen
  • 8,523
  • 8
  • 52
  • 74

3 Answers3

5

It doesn't look like a bug.

According to elasticsearch's documentation (from/size) it looks like 10 is the default number of results that elasticsearch will return from a search query.

You can modify the page size with an appropriate Pageable parameter, e.g.:

Page<User> users = repository.findAll(new PageRequest(1, 20))
sam
  • 1,800
  • 1
  • 25
  • 47
Mark McLaren
  • 11,470
  • 2
  • 48
  • 79
  • 1
    That makes sense - and that's how I was working around it. However, this is a findAll operation, so my thinking is that, when I look at that API, I expect ALL entities to be returned. So basically I expect Spring Data to generate a query that will do that. But - if there's nothing I'm missing, than that's that :) Thanks. – Eugen Jan 07 '15 at 18:41
  • @Eugen The way I see it, it would make sense for a "findAll" operation to return every record in the context of a database, but since Elasticsearch is operating in a search context, it simply means "search without criteria" and is subject to the same default result limit as any other search. – Shawn Bush Jan 11 '15 at 19:46
  • Well, sure, but considering that the main usecase here is storage, and the whole point is using elasticsearch as a storage system not only (or not primarily) for search - I'd much rather have it behave as such. – Eugen Jan 11 '15 at 22:26
2

You can use ElasicsearchTemplate to search through multiple nodes and provide the number of pageRequest (or results) you want. The default is 10.

@Autowired
private ElasticsearchTemplate elasticsearchTemplate;    

public List<MyElasticDocument> getData() {

    String textToSearch = "John";

    QueryBuilder query = QueryBuilders.boolQuery()
            .should(
                    QueryBuilders.queryStringQuery(textToSearch)
                            .lenient(true)
                            .field("jsonNode1")
            )
            .should(
                    QueryBuilders.queryStringQuery(textToSearch + "*")
                            .lenient(true)
                            .field("jsonNode2")
                            .field("jsonNode3")
            );

    NativeSearchQuery build = new NativeSearchQueryBuilder()
            .withQuery(query)
            .withPageable(new PageRequest(0, 20)) // provide the no. of results you want
            .build();

    List<MyElasticDocument> elasticDocumentList = elasticsearchTemplate.queryForList(build, MyElasticDocument.class)

    return elasticDocumentList;
}
sam
  • 1,800
  • 1
  • 25
  • 47
-1

According to https://jira.spring.io/browse/DATAES-58 it works as designed and is not subject to change. Read the comments in this issue to understand the performance background.

nobody
  • 19,814
  • 17
  • 56
  • 77
sepe
  • 41
  • 4