依赖
<dependency> <groupId>org.elasticsearch.client</groupId> <artifactId>elasticsearch-rest-high-level-client</artifactId> <version>6.8.5</version> </dependency>
配置
代码
package com.elasticsearch.controller; import org.elasticsearch.action.search.SearchRequest; import org.elasticsearch.action.search.SearchResponse; import org.elasticsearch.client.RequestOptions; import org.elasticsearch.client.RestHighLevelClient; import org.elasticsearch.index.query.QueryBuilders; import org.elasticsearch.search.SearchHit; import org.elasticsearch.search.SearchHits; import org.elasticsearch.search.builder.SearchSourceBuilder; import org.elasticsearch.search.sort.FieldSortBuilder; import org.elasticsearch.search.sort.SortOrder; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; import java.io.IOException; @RestController public class ElasticsearchController { @Autowired private RestHighLevelClient client; @GetMapping("/page") public String page(int page,int size) throws IOException { SearchRequest searchRequest = new SearchRequest( "app_list" ); searchRequest.types( "_doc" ); SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder(); searchSourceBuilder.query( QueryBuilders.matchAllQuery() ); searchSourceBuilder.sort(new FieldSortBuilder("_id").order( SortOrder.ASC)); int from = (page - 1) * size; int sum = page * size; if(sum > 10000){ searchSourceBuilder.from(0); searchSourceBuilder.size(10000); searchRequest.source( searchSourceBuilder ); System.out.println(searchSourceBuilder); SearchResponse searchResponse = client.search( searchRequest ,RequestOptions.DEFAULT); SearchHits hits = searchResponse.getHits(); if(hits.getTotalHits() < sum - size){ System.out.println("数据不够--"); return null; } SearchHit[] hitsHits = hits.getHits(); if(hitsHits.length > 0){ int eachSize = sum / 10000; if(eachSize * 10000 < sum){ eachSize++; } SearchHit last = hitsHits[hitsHits.length - 1]; for (int i = 1; i < eachSize; i++) { searchSourceBuilder.searchAfter(last.getSortValues()); searchRequest.source( searchSourceBuilder ); System.out.println(searchSourceBuilder); searchResponse = client.search( searchRequest ,RequestOptions.DEFAULT); hits = searchResponse.getHits(); hitsHits = hits.getHits(); if(hitsHits.length == 0){// 没有那么多数据 break; } last = hitsHits[hitsHits.length - 1]; } int skipSize = (10000) * (eachSize - 1); int resultSum = sum - skipSize; int returnSize = resultSum / size; int mod = resultSum % size; if(mod == 0){ SearchHit[] result = new SearchHit[size]; int start = hitsHits.length - size; for (int i = start; i < hitsHits.length; i++) { result[i - start] = hitsHits[i]; } System.out.println(2); }else { int resultsize = hitsHits.length - (returnSize * size); SearchHit[] result = new SearchHit[size]; int start = hitsHits.length - resultsize; for (int i = start; i < hitsHits.length; i++) { result[i - start] = hitsHits[i]; } System.out.println(1); } System.out.println("超过10000---"); } }else { searchSourceBuilder.from(from); searchSourceBuilder.size(size); searchRequest.source( searchSourceBuilder ); System.out.println(searchSourceBuilder); SearchResponse searchResponse = client.search( searchRequest ,RequestOptions.DEFAULT); SearchHits hits = searchResponse.getHits(); SearchHit[] hitsHits = hits.getHits(); System.out.println(" not over 10000"); } return "page"; } }