3. Miscellaneous Elasticsearch Operation Support


This chapter covers additional support for Elasticsearch operations that cannot be directly accessed via the repository interface. It is recommended to add those operations as custom implementation as described in Section 1.3, “Custom implementations for Spring Data repositories” .

3.1 Filter Builder

Filter Builder improves query speed.

Example 3.1. 

                private ElasticsearchTemplate elasticsearchTemplate;
                SearchQuery searchQuery = new NativeSearchQueryBuilder()
                .withFilter(boolFilter().must(termFilter("id", documentId)))
                Page<SampleEntity> sampleEntities =

3.2 Using Scan And Scroll For Big Result Set

Elasticsearch has scan and scroll feature for getting big result set in chunks. ElasticsearchTemplate has scan and scroll methods that can be used as below.

Example 3.2.  Using Scan and Scroll

                SearchQuery searchQuery = new NativeSearchQueryBuilder()
                .withPageable(new PageRequest(0,1))
                String scrollId = elasticsearchTemplate.scan(searchQuery,1000,false);
                List<SampleEntity> sampleEntities = new ArrayList<SampleEntity>();
                boolean hasRecords = true;
                while (hasRecords){
                Page<SampleEntity> page = elasticsearchTemplate.scroll(scrollId, 5000L , new ResultsMapper<SampleEntity>()
                public Page<SampleEntity> mapResults(SearchResponse response) {
                List<SampleEntity> chunk = new ArrayList<SampleEntity>();
                for(SearchHit searchHit : response.getHits()){
                if(response.getHits().getHits().length <= 0) {
                return null;
                SampleEntity user = new SampleEntity();
                return new PageImpl<SampleEntity>(chunk);
                if(page != null) {
                hasRecords = page.hasNextPage();
                hasRecords = false;