Skip to content

Sorting an empty result should not fail. #1908

Closed
@KOSSOKO

Description

@KOSSOKO

I have this method

ebookReviewRepository.findByEbookIdOrderByCommentDateDesc(ebookId, PageRequest.of(page, size));

I noticed that when the index is empty, I get the error Elasticsearch exception [type=search_phase_execution_exception, reason=all shards failed]. This error is due to the fact that elastic search tries to sort and empty result.

They added "ignore_unmapped": true to ignore it:

{
    "from": 0,
    "size": "15",
    "sort": {
        "title": {
            "order": "asc",
            "ignore_unmapped": true
        }
    }
}

elastic/elasticsearch#1558

But it how could i use the ignore_unmapped option in Spring Data elastic search ? There are no traces in the documentation.
I added a check in my services workaround, but for large data, making count each time is not a solution.

// We have to check first if we have data, in order to avoid issues about sorting when empty

		if(this.ebookReviewRepository.count() != 0) {
			
			Page<EbookReview> ebookReviewPage =  ebookReviewRepository.findByEbookIdOrderByCommentDateDesc(ebookId, PageRequest.of(page, size));
			// We add app user name to the review
			for(EbookReview review : ebookReviewPage) {
				EbookReviewDto ebookReviewDto = createCompleteEbookReviewDto(review, appUserId);
				if(ebookReviewDto.getAppUserId() != null) {
					rewiewsDtoResult.add(ebookReviewDto);
				}
			}
			isHasNext = ebookReviewPage.hasNext();
		}

How could I handle it correctly ? Thanks ?

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions