- 这个包可能会与httpclient包发生版本冲突
导包,采用高版本,不然会出现获取不到总数问题,详情见: https://blog.csdn.net/m0_46869111/article/details/121442853?spm=1001.2014.3001.5501
org.elasticsearch.client elasticsearch-rest-high-level-client6.8.4
client配置
public static RestHighLevelClient getClient1() { HttpHost http = new HttpHost("127.0.0.1", 9200, "http"); final CredentialsProvider credentialsProvider = new BasicCredentialsProvider(); credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials("username", "password")); RestClientBuilder builder = RestClient.builder(http).setRequestConfigCallback(new RestClientBuilder.RequestConfigCallback() { @Override public RequestConfig.Builder customizeRequestConfig(RequestConfig.Builder requestConfigBuilder) { requestConfigBuilder.setConnectTimeout(-1); requestConfigBuilder.setSocketTimeout(-1); requestConfigBuilder.setConnectionRequestTimeout(-1); return requestConfigBuilder; } }).setHttpClientConfigCallback(new RestClientBuilder.HttpClientConfigCallback() { @Override public HttpAsyncClientBuilder customizeHttpClient(HttpAsyncClientBuilder httpClientBuilder) { httpClientBuilder.disableAuthCaching(); return httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider); } }).setMaxRetryTimeoutMillis(5*60*1000); RestHighLevelClient client = new RestHighLevelClient(builder); return client; }
es使用
public static List search(String sortTime, String index, String keyword, String keyword2, int start, int count) throws IOException { RestHighLevelClient client = getClient1(); SearchRequest searchRequest = new SearchRequest(index); SearchSourceBuilder sourceBuilder = new SearchSourceBuilder(); sourceBuilder.trackTotalHits(true); //bool多条件匹配 BoolQueryBuilder boolQueryBuilder=new BoolQueryBuilder(); //关键字匹配 if (StringUtils.isNotBlank(keyword)){ // TermQueryBuilder termQueryBuilder=new TermQueryBuilder("message",keyword); // //将多个条件加到bool匹配中 // boolQueryBuilder.must(termQueryBuilder); MatchQueryBuilder matchQueryBuilder1 = new MatchQueryBuilder("message", keyword); //将多个条件加到bool匹配中 boolQueryBuilder.must(matchQueryBuilder1); } //全文检索匹配对应字段 if (StringUtils.isNotBlank(keyword2)){ MatchQueryBuilder matchQueryBuilder = new MatchQueryBuilder("message", keyword2); //将多个条件加到bool匹配中 boolQueryBuilder.must(matchQueryBuilder); } sourceBuilder.query(boolQueryBuilder); //第几页 sourceBuilder.from(start); //第几条 sourceBuilder.size(count); searchRequest.source(sourceBuilder); //根据前端选择的排序来排序 if (sortTime.equals("1")){ sourceBuilder.sort(SortBuilders.fieldSort("排序字段").order(SortOrder.DESC)); }else { sourceBuilder.sort(SortBuilders.fieldSort("排序字段").order(SortOrder.ASC)); } //执行查询 SearchResponse searchResponse = client.search(searchRequest,RequestOptions.DEFAULT); //获取查询总数 long totalHits = searchResponse.getHits().getTotalHits(); sumSize = totalHits; SearchHits hits = searchResponse.getHits(); List
**
这个包可能会与httpclient包发生版本冲突**
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)