Source:
public void shouldSortResultsGivenSortCriteriaWithScanAndScroll() { List<IndexQuery> indexQueries; String documentId = randomNumeric(5); SampleEntity sampleEntity1 = SampleEntity.builder().id(documentId).message("abc").rate(10) .version(System.currentTimeMillis()).build(); String documentId2 = randomNumeric(5); SampleEntity sampleEntity2 = SampleEntity.builder().id(documentId2).message("xyz").rate(5) .version(System.currentTimeMillis()).build(); String documentId3 = randomNumeric(5); SampleEntity sampleEntity3 = SampleEntity.builder().id(documentId3).message("xyz").rate(10) .version(System.currentTimeMillis()).build(); indexQueries = getIndexQueries(Arrays.asList(sampleEntity1, sampleEntity2, sampleEntity3)); operations.bulkIndex(indexQueries, index); indexOperations.refresh(); NativeSearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(matchAllQuery()) .withSort(new FieldSortBuilder("rate").order(SortOrder.ASC)) .withSort(new FieldSortBuilder("message").order(SortOrder.DESC)).withPageable(PageRequest.of(0, 10)).build(); SearchScrollHits<SampleEntity> scroll = ((AbstractElasticsearchTemplate) operations).searchScrollStart(1000, searchQuery, SampleEntity.class, index); List<SearchHit<SampleEntity>> sampleEntities = new ArrayList<>(); while (scroll.hasSearchHits()) { sampleEntities.addAll(scroll.getSearchHits()); scroll = ((AbstractElasticsearchTemplate) operations).searchScrollContinue(scroll.getScrollId(), 1000, SampleEntity.class, index); } assertThat(sampleEntities).hasSize(3); assertThat(sampleEntities.get(0).getContent().getRate()).isEqualTo(sampleEntity2.getRate()); assertThat(sampleEntities.get(1).getContent().getRate()).isEqualTo(sampleEntity3.getRate()); assertThat(sampleEntities.get(1).getContent().getMessage()).isEqualTo(sampleEntity3.getMessage()); assertThat(sampleEntities.get(2).getContent().getRate()).isEqualTo(sampleEntity1.getRate()); assertThat(sampleEntities.get(2).getContent().getMessage()).isEqualTo(sampleEntity1.getMessage()); }

Target:
public void shouldSortResultsGivenSortCriteriaWithScanAndScroll() { String documentId = randomNumeric(5); SampleEntity sampleEntity1 = SampleEntity.builder().id(documentId).message("abc").rate(10) .version(System.currentTimeMillis()).build(); String documentId2 = randomNumeric(5); SampleEntity sampleEntity2 = SampleEntity.builder().id(documentId2).message("xyz").rate(5) .version(System.currentTimeMillis()).build(); String documentId3 = randomNumeric(5); SampleEntity sampleEntity3 = SampleEntity.builder().id(documentId3).message("xyz").rate(10) .version(System.currentTimeMillis()).build(); List<IndexQuery> indexQueries = getIndexQueries(Arrays.asList(sampleEntity1, sampleEntity2, sampleEntity3)); operations.bulkIndex(indexQueries, index); indexOperations.refresh(); NativeSearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(matchAllQuery()) .withSort(new FieldSortBuilder("rate").order(SortOrder.ASC)) .withSort(new FieldSortBuilder("message").order(SortOrder.DESC)).withPageable(PageRequest.of(0, 10)).build(); SearchScrollHits<SampleEntity> scroll = ((AbstractElasticsearchTemplate) operations).searchScrollStart(1000, searchQuery, SampleEntity.class, index); List<SearchHit<SampleEntity>> sampleEntities = new ArrayList<>(); while (scroll.hasSearchHits()) { sampleEntities.addAll(scroll.getSearchHits()); scroll = ((AbstractElasticsearchTemplate) operations).searchScrollContinue(scroll.getScrollId(), 1000, SampleEntity.class, index); } assertThat(sampleEntities).hasSize(3); assertThat(sampleEntities.get(0).getContent().getRate()).isEqualTo(sampleEntity2.getRate()); assertThat(sampleEntities.get(1).getContent().getRate()).isEqualTo(sampleEntity3.getRate()); assertThat(sampleEntities.get(1).getContent().getMessage()).isEqualTo(sampleEntity3.getMessage()); assertThat(sampleEntities.get(2).getContent().getRate()).isEqualTo(sampleEntity1.getRate()); assertThat(sampleEntities.get(2).getContent().getMessage()).isEqualTo(sampleEntity1.getMessage()); }