利用Scan和Scroll处理大结果集

Elasticsearch在处理大结果集时可以使用scan和scroll。在Spring Data Elasticsearch中,可以向下面那样使用ElasticsearchTemplate来使用scan和scroll处理大结果集。

Example 39. Using Scan and Scroll(使用scan和scroll)

  1. SearchQuery searchQuery = new NativeSearchQueryBuilder()
  2. .withQuery(matchAllQuery())
  3. .withIndices("test-index")
  4. .withTypes("test-type")
  5. .withPageable(new PageRequest(0,1))
  6. .build();
  7. String scrollId = elasticsearchTemplate.scan(searchQuery,1000,false);
  8. List<SampleEntity> sampleEntities = new ArrayList<SampleEntity>();
  9. boolean hasRecords = true;
  10. while (hasRecords){
  11. Page<SampleEntity> page = elasticsearchTemplate.scroll(scrollId, 5000L , new ResultsMapper<SampleEntity>()
  12. {
  13. @Override
  14. public Page<SampleEntity> mapResults(SearchResponse response) {
  15. List<SampleEntity> chunk = new ArrayList<SampleEntity>();
  16. for(SearchHit searchHit : response.getHits()){
  17. if(response.getHits().getHits().length <= 0) {
  18. return null;
  19. }
  20. SampleEntity user = new SampleEntity();
  21. user.setId(searchHit.getId());
  22. user.setMessage((String)searchHit.getSource().get("message"));
  23. chunk.add(user);
  24. }
  25. return new PageImpl<SampleEntity>(chunk);
  26. }
  27. });
  28. if(page != null) {
  29. sampleEntities.addAll(page.getContent());
  30. hasRecords = page.hasNextPage();
  31. }
  32. else{
  33. hasRecords = false;
  34. }
  35. }
  36. }