Search Result

The find method returns a SearchResult object. Like the CrudsResult, it is a way to return either a failed state or a success state. If a success, it can return the results and a total number of items that satisfy the query.

if (searchResult.isError()) {

List<CarDetails> results = searchResult.getResults();
int totalNumberOfItems = searchResult.getTotalNumberOfItems();
Option<CarDetails> first = searchResult.getFirst();

Note that if the paging is defined to only return a limited amount of results (say 7), then the getTotalNumberOfItems-method still returns the real number of items even if it exceeds this limit (e.g. 100).

Also note that if no items in the database satisfy the query, this is not considered an error state. The SearchResult will be a success with an empty result list and a total number of items of 0. The correct way of checking if the resultset is non-empty is:

boolean hasResult = searchResult.getTotalNumberOfItems() > 0;
/* OR */
boolean hasResult = searchResult.getFirst().isDefined();