ElasticSearch6.x 基于SpringBoot 实现ElasticSearch之分组统计查询解析

你猜 阅读:389 2021-03-31 17:02:33 评论:0

AggregationUtil.java中添加如下方法,

 /** 
	     * 分组统计 
	     * @param index 索引 
	     * @param field 文档属性 
	     * @return 
	     */ 
	    public Terms terms(String index,String field){ 
	    	String name = "terms"; 
	        AggregationBuilder agg=AggregationBuilders.terms(name).field(field); 
	        SearchResponse response=client.prepareSearch(index).addAggregation(agg).execute().actionGet(); 
	        return response.getAggregations().get(name); 
	    } 
	     
	    /** 
	     * 过滤器统计 
	     * @param index 索引 
	     * @param field 文档属性 
	     * @return 
	     */ 
	    public Filter filter(String index, String field, String key){ 
	        QueryBuilder query=QueryBuilders.termQuery(field,key); 
	        String name ="filter"; 
	        AggregationBuilder agg=AggregationBuilders.filter(name,query); 
	        SearchResponse response=client.prepareSearch(index).addAggregation(agg).execute().actionGet(); 
	        return response.getAggregations().get(name); 
	    } 
	     
	    /** 
	     * 多过滤器统计 
	     * @param index 索引 
	     * @param list  过滤条件队列 
	     * @return 
	     */ 
	    public Filters filters(String index, List<TermQueryBuilder> list){ 
	    	String name = "filters"; 
	    	 
	    	FiltersAggregator.KeyedFilter[] filters = new FiltersAggregator.KeyedFilter[list.size()]; 
	    	for(int i=0; i <list.size(); i++){ 
	    		TermQueryBuilder obj = list.get(i); 
	    		FiltersAggregator.KeyedFilter item = new FiltersAggregator.KeyedFilter(obj.fieldName(),obj); 
	    		filters[i] = item; 
	    	} 
	    	 
	        AggregationBuilder agg=AggregationBuilders.filters(name, filters); 
	        SearchResponse response=client.prepareSearch(index).addAggregation(agg).execute().actionGet(); 
	        return response.getAggregations().get(name); 
	    } 
	     
	    /** 
	     * 区间统计 
	     * @param index 索引 
	     * @param field 文档属性 
	     * @return 
	     */ 
	    public Range range(String index, String field, double to, double from){ 
	    	String name ="range"; 
	        AggregationBuilder agg=AggregationBuilders 
	                .range(name) 
	                .field(field) 
	                .addUnboundedTo(to)//第1个范围 ( ,to) 
	                .addRange(to,from)//第2个范围[to,from) 
	                .addUnboundedFrom(from);//第3个范围[from,) 
	        SearchResponse response=client.prepareSearch(index).addAggregation(agg).execute().actionGet(); 
	        return response.getAggregations().get(name); 
	    } 
 
	 
 
	    /** 
	     * 日期区间统计 
	     * @param index 索引 
	     * @param field 文档属性 
	     * @return 
	     */ 
	    public Range dateRange(String index, String field,String to,String from, String formated){ 
	    	String name ="dateRange"; 
	        AggregationBuilder agg=AggregationBuilders 
	                .dateRange(name) 
	                .field(field) 
	                .format(formated) 
	                .addUnboundedTo(to) 
	                .addUnboundedFrom(from); 
	        SearchResponse response=client.prepareSearch(index).addAggregation(agg).execute().actionGet(); 
	        return response.getAggregations().get(name); 
	    } 
	     
	    /** 
	     * Missing统计 
	     * @param field 
	     * @return 
	     */ 
	    public Missing missing(String index, String field){ 
	    	String name ="missing"; 
	        AggregationBuilder agg=AggregationBuilders.missing(name).field(field); 
	        SearchResponse response=client.prepareSearch(index).addAggregation(agg).execute().actionGet(); 
	        return response.getAggregations().get(name); 
	    } 
	

 

声明

1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。

我的关注

搜索
排行榜
关注我们