public class EsUtils { public static long queryCount(RestHighLevelClient client,QueryBuilder queryBuilder,String indices){ long count =0; CountRequest countRequest = new CountRequest(); countRequest.query(queryBuilder); countRequest.indices(indices); try { count=client.count(countRequest, RequestOptions.DEFAULT).getCount(); } catch (IOException e) { log.error("ES查询信息总数异常:{}", e.getMessage()); } return count; } public static SearchResponse find(RestHighLevelClient client,String indices, SearchSourceBuilder sourceBuilder) throws IOException { SearchRequest request = new SearchRequest().indices(indices); request.source(sourceBuilder); return client.search(request, RequestOptions.DEFAULT); } public static SearchResponse find(RestHighLevelClient client,String indices, QueryBuilder queryBuilder, int page, int size, String sort) throws IOException { SearchRequest request = new SearchRequest().indices(indices); SearchSourceBuilder sourceBuilder = new SearchSourceBuilder(); sourceBuilder.from((page - 1) * size); sourceBuilder.size(size); if (StringUtils.isNotBlank(sort)) { sourceBuilder.sort(sort, SortOrder.DESC); } sourceBuilder.query(queryBuilder); request.source(sourceBuilder); return client.search(request, RequestOptions.DEFAULT); } public SearchResponse findAndSort(RestHighLevelClient client,String indices, QueryBuilder queryBuilder, int from, int size, String sort, SortOrder order) throws IOException { SearchRequest request = new SearchRequest().indices(indices); SearchSourceBuilder sourceBuilder = new SearchSourceBuilder(); sourceBuilder.from((from - 1) * size); sourceBuilder.size(size); sourceBuilder.sort(sort, order); sourceBuilder.query(queryBuilder); request.source(sourceBuilder); return client.search(request, RequestOptions.DEFAULT); } public static boolean exists(RestHighLevelClient client, String indices, GetRequest getRequest){ boolean b=true; try { b = client.exists(getRequest, RequestOptions.DEFAULT); } catch (IOException e) { log.error("ES查询单条数是否存在,抛出异常:{}", e.getMessage()); } return b; } public SearchResponse agg(RestHighLevelClient client, String indices, QueryBuilder queryBuilder, AggregationBuilder aggregationBuilder) throws IOException { SearchRequest request = new SearchRequest().indices(indices); SearchSourceBuilder sourceBuilder = new SearchSourceBuilder(); sourceBuilder.size(0); sourceBuilder.query(queryBuilder); sourceBuilder.aggregation(aggregationBuilder); request.source(sourceBuilder); return client.search(request, RequestOptions.DEFAULT); } public MapaggoneArgs(RestHighLevelClient client,String indices, QueryBuilder queryBuilder, String args1, int size) { Map map = null; try { map = new HashMap<>(); SearchRequest request = new SearchRequest().indices(indices); SearchSourceBuilder sourceBuilder = new SearchSourceBuilder(); sourceBuilder.size(0); sourceBuilder.query(queryBuilder); TermsAggregationBuilder agg1 = AggregationBuilders.terms(args1).field(args1 + ".keyword").size(size); sourceBuilder.aggregation(agg1); request.source(sourceBuilder); SearchResponse response = client.search(request, RequestOptions.DEFAULT); Terms terms = response.getAggregations().get(args1); for (Terms.Bucket bucket : terms.getBuckets()) { map.put(bucket.getKeyAsString(), bucket.getDocCount()); } } catch (IOException e) { e.printStackTrace(); } return map; } public Map > aggTwoArgs(RestHighLevelClient client,String indices, QueryBuilder queryBuilder, String args1, String args2, int size) { Map > map = null; try { map = new HashMap<>(); SearchRequest request = new SearchRequest().indices(indices); SearchSourceBuilder sourceBuilder = new SearchSourceBuilder(); sourceBuilder.size(0); sourceBuilder.query(queryBuilder); TermsAggregationBuilder agg1 = AggregationBuilders.terms(args1).field(args1 + ".keyword").size(size); TermsAggregationBuilder agg2 = AggregationBuilders.terms(args2).field(args2 + ".keyword").size(size); agg1.subAggregation(agg2); sourceBuilder.aggregation(agg1); request.source(sourceBuilder); SearchResponse response = client.search(request, RequestOptions.DEFAULT); Aggregations aggregations = response.getAggregations(); Terms terms1 = aggregations.get(args1); for (Terms.Bucket bucket1 : terms1.getBuckets()) { Terms terms2 = bucket1.getAggregations().get(args2); Map map1 = new HashMap<>(); for (Terms.Bucket bucket2 : terms2.getBuckets()) { map1.put(bucket2.getKeyAsString(), bucket2.getDocCount()); } map.put(bucket1.getKeyAsString(), map1); } } catch (IOException e) { e.printStackTrace(); } return map; } public Map scrollQuery1(RestHighLevelClient client,String indices,int page,int size) throws IOException { ArrayList strings = new ArrayList<>(); HashMap dataMap = new HashMap<>(); SearchRequest searchRequest = new SearchRequest(indices); Scroll scroll = new Scroll(Timevalue.timevalueMinutes(5L)); searchRequest.scroll(scroll); SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder(); MatchAllQueryBuilder matchAllQueryBuilder = QueryBuilders.matchAllQuery(); searchSourceBuilder.query(matchAllQueryBuilder); searchSourceBuilder.size(size); searchRequest.source(searchSourceBuilder); SearchResponse response = client.search(searchRequest, RequestOptions.DEFAULT); String scrollId = response.getScrollId(); SearchHit[] searchHits = response.getHits().getHits(); System.out.println(response.getHits().getTotalHits()); for (SearchHit searchHit : searchHits) { strings.add(searchHit.getSourceAsString()); } dataMap.put("scroll",scrollId); dataMap.put("data",strings); return dataMap; } public List scrollQuery2(RestHighLevelClient client, String scrollId) throws IOException { ArrayList strings = new ArrayList<>(); Scroll scroll = new Scroll(Timevalue.timevalueMinutes(5L)); SearchScrollRequest scrollRequest = new SearchScrollRequest(scrollId); scrollRequest.scroll(scroll); SearchResponse response = client.scroll(scrollRequest, RequestOptions.DEFAULT); SearchHit[] hits = response.getHits().getHits(); for (SearchHit searchHit : hits) { strings.add(searchHit.getSourceAsString()); } return strings; } }
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)