23
23
<example >
24
24
<programlisting language =" java" >
25
25
private ElasticsearchTemplate elasticsearchTemplate;
26
- SearchQuery searchQuery = new SearchQuery();
27
- searchQuery.setElasticsearchQuery(matchAllQuery());
28
- searchQuery.setElasticsearchFilter(boolFilter().must(termFilter("id", documentId)));
26
+ SearchQuery searchQuery = new NativeSearchQueryBuilder()
27
+ .withQuery(matchAllQuery())
28
+ .withFilter(boolFilter().must(termFilter("id", documentId)))
29
+ .build();
29
30
Page< SampleEntity> sampleEntities = elasticsearchTemplate.queryForPage(searchQuery,SampleEntity.class);
30
31
</programlisting >
31
32
</example >
@@ -42,41 +43,41 @@ Page<SampleEntity> sampleEntities = elasticsearchTemplate.queryForPage(sea
42
43
Using Scan and Scroll
43
44
</title >
44
45
<programlisting language =" java" >
45
- SearchQuery searchQuery = new SearchQuery();
46
- searchQuery.addIndices("test-index");
47
- searchQuery.addTypes ("test-type");
48
- searchQuery.setElasticsearchQuery(matchAllQuery());
49
- searchQuery.setPageable (new PageRequest(0,1));
50
- String scrollId = elasticsearchTemplate.scan(searchQuery,1000,false );
51
- List < SampleEntity > sampleEntities = new ArrayList < SampleEntity > ( );
52
- boolean hasRecords = true ;
53
- while (hasRecords){
54
- Page < SampleEntity > page = elasticsearchTemplate.scroll(scrollId, 5000L , new ResultsMapper < SampleEntity > () {
55
- @Override
56
- public Page < SampleEntity > mapResults(SearchResponse response) {
57
- List < SampleEntity> chunk = new ArrayList < SampleEntity > ();
58
- for(SearchHit searchHit : response.getHits()){
59
- if( response.getHits().getHits().length < = 0) {
60
- return null;
61
- }
62
- SampleEntity user = new SampleEntity();
63
- user.setId(searchHit.getId() );
64
- user.setMessage((String) searchHit.getSource().get("message" ));
65
- chunk.add(user );
66
- }
67
- return new PageImpl < SampleEntity > (chunk);
68
- }
69
-
70
- });
71
- if(page != null) {
72
- sampleEntities.addAll(page.getContent());
73
- hasRecords = page.hasNextPage();
74
- }
75
- else{
76
- hasRecords = false;
77
- }
78
- }
79
- }</programlisting >
46
+ SearchQuery searchQuery = new NativeSearchQueryBuilder()
47
+ .withQuery(matchAllQuery())
48
+ .withIndices ("test-index")
49
+ .withTypes("test-type")
50
+ .withPageable (new PageRequest(0,1))
51
+ .build( );
52
+ String scrollId = elasticsearchTemplate.scan(searchQuery,1000,false );
53
+ List < SampleEntity > sampleEntities = new ArrayList < SampleEntity > () ;
54
+ boolean hasRecords = true;
55
+ while (hasRecords) {
56
+ Page < SampleEntity > page = elasticsearchTemplate.scroll(scrollId, 5000L , new ResultsMapper < SampleEntity > () {
57
+ @Override
58
+ public Page < SampleEntity> mapResults(SearchResponse response) {
59
+ List < SampleEntity > chunk = new ArrayList < SampleEntity > ();
60
+ for(SearchHit searchHit : response.getHits()) {
61
+ if(response.getHits().getHits().length < = 0) {
62
+ return null;
63
+ }
64
+ SampleEntity user = new SampleEntity( );
65
+ user.setId( searchHit.getId( ));
66
+ user.setMessage((String)searchHit.getSource().get("message") );
67
+ chunk.add(user);
68
+ }
69
+ return new PageImpl < SampleEntity > (chunk);
70
+ }
71
+ });
72
+ if(page != null) {
73
+ sampleEntities.addAll(page.getContent());
74
+ hasRecords = page.hasNextPage();
75
+ }
76
+ else{
77
+ hasRecords = false;
78
+ }
79
+ }
80
+ }</programlisting >
80
81
</example >
81
82
</section >
82
83
</chapter >
0 commit comments