public HttpResult<ESResult> inserSql(int pageNum,int pageSize) throws IOException { RestClient restClient = RestClient.builder( new HttpHost("127.0.0.1", 9200, "http")).build(); /*高level的客户端*/ RestHighLevelClient client = new RestHighLevelClient(restClient); String index = "index_name"; String docType= "document_type"; SearchSourceBuilder sourceBuilder = new SearchSourceBuilder(); pageSize = pageSize >= 1000 ? 1000 : pageSize; pageSize = pageSize <= 0 ? 15 : pageSize; //其实位置 sourceBuilder.from(pageNum); //每页数量 sourceBuilder.size(pageSize); sourceBuilder.sort(new FieldSortBuilder("_uid").order(SortOrder.DESC)); SearchRequest rq = new SearchRequest(); //索引 rq.indices(index); rq.types(docType); //各种组合条件 rq.source(sourceBuilder); //请求 SearchResponse rp = client.search(rq); ESResult esResult = new ESResult(); esResult.setSearchResponse(rp); esResult.setSearchRequest(rq); return HttpResult.ok(esResult); }
maven依赖
<dependency> <groupId>org.elasticsearch.client</groupId> <artifactId>elasticsearch-rest-client</artifactId> <version>5.6.0</version> <scope>compile</scope> </dependency> <dependency> <groupId>org.elasticsearch.client</groupId> <artifactId>elasticsearch-rest-high-level-client</artifactId> <version>5.6.0</version> </dependency>