精华内容
下载资源
问答
  • elasticsearch聚合查询

    2021-03-30 17:39:31
    关于elasticsearch聚合查询 命令如下 #查看每个年龄段的分布情况 GET bank/_search { "aggregations": { "ageAgg": { "terms": { "field": "age", "size": 10 } } } } #查看每个年龄段的分布情况。 查看...

    关于elasticsearch聚合查询

    命令如下
    #查看每个年龄段的分布情况
    GET bank/_search
    {
      "aggregations": {
        "ageAgg": {
          "terms": {
            "field": "age", 
            "size": 10
          }
        }
      }
    }
    
    #查看每个年龄段的分布情况。 查看整体的平均工资
    GET bank/_search
    {
      "aggregations": {
        "ageAgg": {
          "terms": {
            "field": "age", 
            "size": 10,
            "min_doc_count": 1,
            "shard_min_doc_count": 0,
            "show_term_doc_count_error": false,
            "order": [
              {
                "_count": "desc"
              },
              {
                "_key": "asc"
              }
            ]
          }
        },
        "balanceAvg":{
          "avg": {
            "field": "balance"
          }
        }
      }
    }
    
    #聚合中的聚合 查看每个年龄段的平均薪资
    GET bank/_search
    {
      "aggs": {
        "ageAgg": {
          "terms": {
            "field": "age",
            "size": 10
          },
          "aggs": {
            "balanceAvg": {
              "avg": {
                "field": "balance"
              }
            }
          }
        }
      }
    }
    
    Java客户端代码如下
    @SpringBootTest
    public class AggregationsTest {
    
        private String indexName = "bank";
    
        @Autowired
        RestHighLevelClient restHighLevelClient;
    
        /**
         * term聚合 查看每个年龄段的分布情况
         */
        @Test
        public void test1() throws IOException {
            String ageAggName = "ageAgg";
            SearchRequest searchRequest = new SearchRequest();
            searchRequest.indices(indexName);
            SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
            TermsAggregationBuilder termsBuilder = AggregationBuilders.terms(ageAggName).field("age");
            searchSourceBuilder.aggregation(termsBuilder);
            System.out.println("输出查询参数:"+searchSourceBuilder.toString());
            searchRequest.source(searchSourceBuilder);
            SearchResponse searchResponse = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);
            Terms term = searchResponse.getAggregations().get(ageAggName);
            for (Terms.Bucket bucket : term.getBuckets()) {
                System.out.println("文档的key:"+bucket.getKeyAsString());
                System.out.println("文档的值:"+bucket.getDocCount());
            }
        }
    
        /**
         * avg 平均聚合 查看整体的平均薪资
         * @throws IOException
         */
        @Test
        public void test2() throws IOException {
            String ageAggName = "ageAgg";
            String balanceAvgName = "balanceAvg";
            SearchRequest searchRequest = new SearchRequest();
            searchRequest.indices(indexName);
            SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
            searchSourceBuilder.aggregation(AggregationBuilders.terms(ageAggName).field("age"));
            searchSourceBuilder.aggregation(AggregationBuilders.avg(balanceAvgName).field("balance"));
            searchRequest.source(searchSourceBuilder);
            SearchResponse searchResponse = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);
            Terms terms = searchResponse.getAggregations().get(ageAggName);
            if (terms != null) {
                for (Terms.Bucket bucket : terms.getBuckets()) {
                    System.out.println("文档的key:"+bucket.getKeyAsString());
                    System.out.println("文档的值:"+bucket.getDocCount());
                }
            }
    
            //查询整体平均薪资
            Avg balanceAvg = searchResponse.getAggregations().get(balanceAvgName);
            if (balanceAvg != null) {
                System.out.println("平均薪资:"+balanceAvg.getValue());
            }
        }
    
        /**
         * 聚合中的聚合 查看每个年龄段的平均薪资
         */
        @Test
        public void test3() throws IOException {
            String ageAggName = "ageAgg";
            String balanceAvgName = "balanceAvg";
            SearchRequest searchRequest = new SearchRequest(indexName);
            SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
            searchSourceBuilder.aggregation(AggregationBuilders.terms(ageAggName).field("age").subAggregation(AggregationBuilders.avg(balanceAvgName).field("balance")));
            searchRequest.source(searchSourceBuilder);
            SearchResponse search = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);
            Terms terms = search.getAggregations().get(ageAggName);
            if (terms != null) {
                for (Terms.Bucket bucket : terms.getBuckets()) {
                    System.out.println("文档的key:"+bucket.getKeyAsString()+"-----"+"文档的值:"+bucket.getDocCount());
                    Avg balanceAvg = bucket.getAggregations().get(balanceAvgName);
                    System.out.println("平均薪资:"+balanceAvg.getValue());
                }
            }
        }
    }
    
    展开全文
  • Elasticsearch聚合查询

    2021-10-19 15:35:52
    Elasticsearch 聚合查询(aggs) elasticsearch es sum求和统计用法 在查询出来的记录中,如果需要对记录的数据进行再次操作(比如求和、平均值、最大值。。。),就需要用到聚合查询。 1、求和 第一种方法: GET ...

    Elasticsearch 聚合查询(aggs)
    elasticsearch es sum求和统计用法
    在查询出来的记录中,如果需要对记录的数据进行再次操作(比如求和、平均值、最大值。。。),就需要用到聚合查询。

    1、求和

    第一种方法

    GET qrydev-*/_search
    {
    "query": {
    		//这里根据业务查询的要求	
     },
     "aggs": {
     	"my_sum": {
     		"sum": {
    		"field": "TranNum"
    	}
     	}
    }
    }
    

    第二种方法

    GET qrydev-*/_search
    {
    "query": {
    		//这里根据业务查询的要求	
     },
     "aggs": {
     	"my_sum": {
     		"stats": {
    		"field": "TranNum"
    	}
     	}
    }
    }
    

    第三种方法

    GET qrydev-*/_search
    {
    "query": {
    		//这里根据业务查询的要求	
     },
     "aggs": {
     	"my_sum": {
     		"extendedstats": {
    		"field": "TranNum"
    	}
     	}
    }
    }
    

    但是三种方法得出的结果有些差别,包含的信息不一样。
    包含信息:extendedstats>stats>sum

    展开全文
  • ES聚合查询

    2021-06-09 13:42:21
    ES聚合查询 文章目录ES聚合查询1. 聚合查询2. 指标聚合(Metrics aggregations)2.1 平均值2.2 最大值2.3 最小值2.4 求和2.5 统计2.6 多个指标聚合3. 桶聚合3.1 分组聚合3.2 日期直方图聚合3.3 直方图聚合3.4 缺失...

    ES聚合查询

    1. 聚合查询

    ES 6.3.2中聚合查询分为:桶聚合(Bucket aggregations)、指标聚合(Metrics aggregations)、流水线聚合(Pipeline aggregations)、矩阵聚合(Matrix Aggregations),下面介绍几种常见的聚合方式。

    2. 指标聚合(Metrics aggregations)

    2.1 平均值

    求某个指标的平均值:

    // 查询条件,年级为高二、高三的数据
    BoolQueryBuilder queryBuilder = QueryBuilders.boolQuery()
            .filter(QueryBuilders.termsQuery("grade", "高二", "高三"));
    
    // params集合
    Map<String, Object> params = new HashMap<>();
    params.put("correction", 2);
    // 构建脚本
    Script script = new Script(ScriptType.INLINE, "painless", "_value * params.correction", params);
    // 平均值聚合语句
    AvgAggregationBuilder avg = AggregationBuilders
            .avg("avg_score")
            .field("score")
            .missing(0)
            .script(script);
    

    以上代码相当于以下命令:

    {
      "query": {
        "bool": {
          "adjust_pure_negative": true,
          "filter": [{
            "terms": {
              "boost": 1.0,
              "grade": ["高三", "高二"]
            }
          }],
          "boost": 1.0
        }
      },
      "aggs": {
        "avg_grade": {
          "avg": {
            "field": "score",
            "missing": 0,
            "script": {
              "lang": "painless",
              "source": "_value * params.correction",
              "params": {
                "correction": 2
              }
            }
          }
        }
      }
    }
    

    以下为对上面命令关键词的解释
    query: 查询条件,用于圈定数据范围
    aggs: 聚合条件
    avg_grade: 聚合名称,聚合结果以此进行展示
    avg: 命令,求平均值
    field: 求平局值的字段
    missing: 对于缺失值的数据采用的默认值
    script: 脚本
    lang: 脚本语言,painless
    source: 脚本内容,此处表示数据的value值乘以params.correction,表示参数集合中的某个值
    params: 参数集合,其中correction 值为2,此脚本表示数据值乘以2

    Java代码解析返回值:

    Map<String, Aggregation> map = response.getAggregations().getAsMap();
    Aggregation aggregation = map.get("avg_score");
    InternalAvg internalAvg = (InternalAvg) aggregation;
    double avgScore = internalAvg.getValue();
    

    2.2 最大值

    求一批数据中某个字段的最大值
    Java代码如下:

    BoolQueryBuilder queryBuilder = QueryBuilders.boolQuery()
            .filter(QueryBuilders.termsQuery("grade", "高二", "高三"));
    
    MaxAggregationBuilder max = AggregationBuilders
    		.max("max_score")
    		.field("score")
    		.missing(0);
    

    等效查询命令如下:

    {
      "query": {
        "bool": {
          "adjust_pure_negative": true,
          "filter": [{
            "terms": {
              "boost": 1.0,
              "grade": ["高三", "高二"]
            }
          }],
          "boost": 1.0
        }
      },
      "aggs": {
        "max_score": {
          "max": {
            "field": "score",
            "missing": 0
          }
        }
      }
    }
    

    Java代码解析结果:

    Map<String, Aggregation> map = response.getAggregations().getAsMap();
    Aggregation aggregation = map.get("max_score");
    InternalMax internalMax = (InternalMax) aggregation;
    double maxScore = internalMax.getValue();
    

    2.3 最小值

    求一批数据中某个字段的最小值
    Java代码如下:

    BoolQueryBuilder queryBuilder = QueryBuilders.boolQuery()
            .filter(QueryBuilders.termsQuery("grade", "高二", "高三"));
    
    MinAggregationBuilder count = AggregationBuilders
    		.min("min_score")
    		.field("score")
    		.missing(0);
    

    等效查询命令如下:

    {
      "query": {
        "bool": {
          "adjust_pure_negative": true,
          "filter": [{
            "terms": {
              "boost": 1.0,
              "grade": ["高三", "高二"]
            }
          }],
          "boost": 1.0
        }
      },
      "aggs": {
        "min_score": {
          "min": {
            "field": "score",
            "missing": 0
          }
        }
      }
    }
    

    Java代码解析结果:

    Map<String, Aggregation> map = response.getAggregations().getAsMap();
    Aggregation aggregation = map.get("min_score");
    InternalMin internalMin = (InternalMin) aggregation;
    double minScore = internalMin.getValue();
    

    2.4 求和

    求一批数据中某个字段的和
    Java代码如下:

    BoolQueryBuilder queryBuilder = QueryBuilders.boolQuery()
    		.filter(QueryBuilders.termsQuery("grade", "高二", "高三"));
    
    SumAggregationBuilder min = AggregationBuilders
    		.sum("sum_score")
    		.field("score")
    		.missing(0);
    

    等效查询命令如下:

    {
      "query": {
        "bool": {
          "adjust_pure_negative": true,
          "filter": [{
            "terms": {
              "boost": 1.0,
              "grade": ["高三", "高二"]
            }
          }],
          "boost": 1.0
        }
      },
      "aggs": {
        "sum_score": {
          "sum": {
            "field": "score",
            "missing": 0
          }
        }
      }
    }
    

    Java代码解析结果:

    Map<String, Aggregation> map = response.getAggregations().getAsMap();
    Aggregation aggregation = map.get("sum_score");
    InternalSum internalMin = (InternalSum) aggregation;
    double sumScore = internalMin.getValue();
    

    2.5 统计

    求一批数据中的个数
    Java代码如下:

    BoolQueryBuilder queryBuilder = QueryBuilders.boolQuery()
    		.filter(QueryBuilders.termsQuery("grade", "高二", "高三"));
    
    ValueCountAggregationBuilder min = AggregationBuilders
    		.count("count_score")
    		.field("score")
    		.missing(0);
    

    等效查询命令如下:

    {
      "query": {
        "bool": {
          "adjust_pure_negative": true,
          "filter": [{
            "terms": {
              "boost": 1.0,
              "grade": ["高三", "高二"]
            }
          }],
          "boost": 1.0
        }
      },
      "aggs": {
        "count_score": {
          "value_count": {
            "field": "score",
            "missing": 0
          }
        }
      }
    }
    

    Java代码解析结果:

    Map<String, Aggregation> map = response.getAggregations().getAsMap();
    Aggregation aggregation = map.get("count_score");
    InternalValueCount internalValueCount = (InternalValueCount) aggregation;
    double countValue = internalValueCount.getValue();
    

    2.6 多个指标聚合

    ES支持多个指标聚合,例如即查询平均值,又查询最大值、最小值
    Java代码如下:

    BoolQueryBuilder queryBuilder = QueryBuilders.boolQuery()
    		.filter(QueryBuilders.termsQuery("grade", "高二", "高三"));
    
    MinAggregationBuilder min = AggregationBuilders
    		.min("min_score")
    		.field("score")
    		.missing(0);
    MaxAggregationBuilder max = AggregationBuilders
    		.max("max_score")
    		.field("score")
    		.missing(0);
    AvgAggregationBuilder avg = AggregationBuilders
    		.avg("avg_score")
    		.field("score")
    		.missing(0);
    
    SearchResponse response = ES_DAO.getTransportClient()
    		.prepareSearch("student")
    		.setTypes("student")
    		.setSearchType(SearchType.QUERY_THEN_FETCH)
    		.setQuery(queryBuilder)
    		.addAggregation(avg)
    		.addAggregation(max)
    		.addAggregation(min)
    		.setExplain(false)
    		.execute()
    		.actionGet();
    

    等效查询命令如下,查询命令包含多个聚合函数:

    {
      "query": {
        "bool": {
          "adjust_pure_negative": true,
          "filter": [{
            "terms": {
              "boost": 1.0,
              "grade": ["高三", "高二"]
            }
          }],
          "boost": 1.0
        }
      },
      "aggs": {
        "max_score": {
          "max": {
            "field": "score",
            "missing": 0
          }
        },
        "min_score": {
          "min": {
            "field": "score",
            "missing": 0
          }
        },
        "avg_score": {
          "avg": {
            "field": "score",
            "missing": 0
          }
        }
      }
    }
    

    Java代码解析结果:

    // 获取最大值
    Map<String, Aggregation> map = response.getAggregations().getAsMap();
    Aggregation maxAgg = map.get("max_score");
    InternalMax internalMax = (InternalMax) maxAgg;
    double maxValue = internalMax.getValue();
    System.out.println(maxValue);
    // 获取最小值
    Aggregation minAgg = map.get("min_score");
    InternalMin internalMin = (InternalMin) minAgg;
    double minValue = internalMin.getValue();
    System.out.println(minValue);
    // 获取平均值
    Aggregation avgAgg = map.get("avg_score");
    InternalAvg internalAvg = (InternalAvg) avgAgg;
    double avgValue = internalAvg.getValue();
    System.out.println(avgValue);
    

    以上单个指标进行聚合的返回值,全部都继承了 SingleValue 这个抽象类,例如:InternalMax、InternalMin、InternalAvg

    3. 桶聚合

    3.1 分组聚合

    ES可以按照某些字段进行分组统计,java代码如下:

    BoolQueryBuilder queryBuilder = QueryBuilders.boolQuery()
    		.filter(QueryBuilders.termsQuery("grade", "高二", "高三"));
    
    // 按性别进行分组,然后指定排序规则
    TermsAggregationBuilder sexTerm = AggregationBuilders.terms("sex_count").field("sex")
    		.missing("未知")
    		.size(3)
    		.order(BucketOrder.count(false));
    
    // 按年龄进行分组,然后指定排序规则
    TermsAggregationBuilder ageTerm = AggregationBuilders.terms("age_count").field("age")
    		.size(100)
    		.order(BucketOrder.count(false));
    
    // 年龄分组是性别分组的子聚合
    sexTerm.subAggregation(ageTerm);
    
    // 执行ES查询统计
    SearchResponse response = ES_DAO.getTransportClient()
    		.prepareSearch("student")
    		.setTypes("student")
    		.setSearchType(SearchType.QUERY_THEN_FETCH)
    		.setQuery(queryBuilder)
    		.addAggregation(sexTerm)
    		.setExplain(false)
    		.execute()
    		.actionGet();
    
    // 解析分组结果
    Map<String, Aggregation> map = response.getAggregations().getAsMap();
    Aggregation sexAgg = map.get("sex_count");
    StringTerms stringTerms = (StringTerms) sexAgg;
    for (Terms.Bucket teamBucket : stringTerms.getBuckets()) {
    	String sex = (String) teamBucket.getKey();
    	long count = teamBucket.getDocCount();
    	System.out.println("性别分组结果:" + sex + " " + count);
    	Map<String, Aggregation> subAggMap = teamBucket.getAggregations().getAsMap();
    	for (Map.Entry<String, Aggregation> entry : subAggMap.entrySet()) {
    		String key = entry.getKey();
    		Aggregation ageAgg = entry.getValue();
    		LongTerms terms = (LongTerms) ageAgg;
    
    		for (Terms.Bucket bucket : terms.getBuckets()) {
    			Long age = (Long) bucket.getKey();
    			long count1 = bucket.getDocCount();
    			System.out.println("性别年龄分组结果:" + sex + " " + age + " " + count1);
    		}
    	}
    }
    

    注意,分组时ES有一个默认返回数据个数,如果数据量大的话,最好通过size(n)指定一个分组返回的数据个数,否则会出现返回数据丢失的情况。

    同kibana命令如下:

    {
      "query": {
        "bool": {
          "adjust_pure_negative": true,
          "filter": [{
            "terms": {
              "boost": 1.0,
              "grade": ["高三", "高二"]
            }
          }],
          "boost": 1.0
        }
      },
      "aggs": {
        "sex_count": {
          "terms": {
            "shard_min_doc_count": 0,
            "field": "sex",
            "size": 3,
            "missing": "未知",
            "show_term_doc_count_error": false,
            "min_doc_count": 1,
            "order": [{
              "_count": "desc"
            }, {
              "_key": "asc"
            }]
          },
          "aggregations": {
            "age_count": {
              "terms": {
                "shard_min_doc_count": 0,
                "field": "age",
                "size": 100,
                "show_term_doc_count_error": false,
                "min_doc_count": 1,
                "order": [{
                  "_count": "desc"
                }, {
                  "_key": "asc"
                }]
              }
            }
          }
        }
      }
    }
    

    3.2 日期直方图聚合

    这种多桶聚合类似于普通直方图,但它只能用于日期值。因为在Elasticsearch中,日期在内部表示为长值,所以在日期上也可以使用正态直方图,但不太准确。这两个api的主要区别在于,这里可以使用日期/时间表达式指定间隔。基于时间的数据需要特殊的支持,因为基于时间的间隔并不总是固定的长度。java代码如下:

    // 查询条件
    BoolQueryBuilder queryBuilder = QueryBuilders.boolQuery()
    		.filter(QueryBuilders.termsQuery("grade", "高二", "高三"));
    
    // 按日期进行分组,然后指定排序规则
    DateHistogramAggregationBuilder dateHis = AggregationBuilders
    		.dateHistogram("date_histogram")
    		.field("create_time")
    		.interval(1000 * 60 * 60 * 24)
    		.minDocCount(1)
    		.order(BucketOrder.key(true))
    		;
    
    // 执行ES查询统计
    SearchResponse response = ES_DAO.getTransportClient()
    		.prepareSearch("student")
    		.setTypes("student")
    		.setSearchType(SearchType.QUERY_THEN_FETCH)
    		.setQuery(queryBuilder)
    		.addAggregation(dateHis)
    		.setExplain(false)
    		.execute()
    		.actionGet();
    
    // 解析分组结果
    Map<String, Aggregation> map = response.getAggregations().getAsMap();
    Aggregation dateAgg = map.get("date_histogram");
    InternalDateHistogram dateHistogram = (InternalDateHistogram) dateAgg;
    for (InternalDateHistogram.Bucket entry : dateHistogram.getBuckets()) {
    	DateTime date = (DateTime) entry.getKey();
    	long count = entry.getDocCount();
    	System.out.println(date + " " + count); 
    }
    

    interval: 统计日期间隔,单位为毫秒值,1000 * 60 * 60 * 24 表示按天进行统计
    minDocCount: 最小文档数量,默认为0,会将当天没有数据的统计出来显示为0,如果需要连续这里可以填0,如无必要可以填一个所需的最小值。
    order: 排序规则,按照key值即日期进行升序排列

    同kibana命令如下:

    {
      "query": {
        "bool": {
          "adjust_pure_negative": true,
          "filter": [{
            "terms": {
              "boost": 1.0,
              "grade": ["高三", "高二"]
            }
          }],
          "boost": 1.0
        }
      },
      "aggs": {
        "date_histogram": {
          "date_histogram": {
            "field": "create_time",
            "offset": 0,
            "interval": 86400000,
            "keyed": false,
            "min_doc_count": 1,
            "order": {
              "_key": "asc"
            }
          }
        }
      }
    }
    

    interval: 时间间隔还可以填:minute、hour、day、month、year等。

    3.3 直方图聚合

    可应用于从文档中提取的数值。它在这些值上动态地构建固定大小(也称为间隔)的bucket。例如,如果文档有一个包含价格(数字)的字段,我们可以将此聚合配置为动态构建间隔为5的bucket(如果价格为5美元)。当执行聚合时,将对每个文档的price字段进行求值,并将其向下舍入到最近的bucket中—例如,如果price为32,bucket size为5,则舍入结果将为30,因此文档将“落入”与键30关联的bucket中。下面是使用的舍入函数:

    bucket_key = Math.floor((value - offset) / interval) * interval + offset
    

    java代码如下:

    // 查询条件
    BoolQueryBuilder queryBuilder = QueryBuilders.boolQuery()
    		.filter(QueryBuilders.termsQuery("grade", "高二", "高三"));
    
    // 直方图聚合,分数按照 0.5分一个梯度进行统计,按照分值高低倒序排列
    HistogramAggregationBuilder histogram = AggregationBuilders
    		.histogram("score_histogram")
    		.field("score")
    		.interval(0.5)
    		.order(BucketOrder.key(false))
    		.minDocCount(0);
    
    // 执行ES查询统计
    SearchResponse response = ES_DAO.getTransportClient()
    		.prepareSearch("student")
    		.setTypes("student")
    		.setSearchType(SearchType.QUERY_THEN_FETCH)
    		.setQuery(queryBuilder)
    		.addAggregation(histogram)
    		.setExplain(false)
    		.execute()
    		.actionGet();
    
    // 解析分组结果
    Map<String, Aggregation> map = response.getAggregations().getAsMap();
    Aggregation scoreHistogram = map.get("score_histogram");
    InternalHistogram internalDateRange = (InternalHistogram) scoreHistogram;
    for (InternalHistogram.Bucket entry : internalDateRange.getBuckets()) {
    	double key = (double) entry.getKey();
    	long count = entry.getDocCount();
    	System.out.println(key + " " + count);
    }
    

    同kibana命令如下:

    {
      "query": {
        "bool": {
          "adjust_pure_negative": true,
          "filter": [{
            "terms": {
              "boost": 1.0,
              "grade": ["高三", "高二"]
            }
          }],
          "boost": 1.0
        }
      },
      "size": 1,
      "aggs": {
        "score_histogram": {
          "histogram": {
            "field": "score",
            "offset": 0.0,
            "interval": 0.5,
            "keyed": false,
            "min_doc_count": 0,
            "order": {
              "_key": "desc"
            }
          }
        }
      }
    }
    

    3.4 缺失聚合

    一种基于字段数据的单bucket聚合,它创建一个bucket,其中包含当前文档集上下文中缺少字段值(实际上是缺少字段或配置了空值集)的所有文档。此聚合器通常与其他字段数据桶聚合器(例如范围)一起使用,以返回由于缺少字段数据值而无法放入任何其他桶中的所有文档的信息。即统计数据中某个字段值为空的数据量。

    java代码如下:

    // 查询条件
    BoolQueryBuilder queryBuilder = QueryBuilders.boolQuery()
    		.filter(QueryBuilders.termsQuery("grade", "高二", "高三"));
    
    // 聚合性别字段缺失的数据
    MissingAggregationBuilder missingAggregationBuilder = AggregationBuilders
    		.missing("sex_miss")
    		.field("sex");
    
    // 执行ES查询统计
    SearchResponse response = ES_DAO.getTransportClient()
    		.prepareSearch("student")
    		.setTypes("student")
    		.setSearchType(SearchType.QUERY_THEN_FETCH)
    		.setQuery(queryBuilder)
    		.addAggregation(missingAggregationBuilder)
    		.setExplain(false)
    		.execute()
    		.actionGet();
    
    // 解析分组结果
    Map<String, Aggregation> map = response.getAggregations().getAsMap();
    Aggregation aggregation = map.get("sex_miss");
    InternalMissing internalMissing = (InternalMissing) aggregation;
    String name = internalMissing.getName();
    long count = internalMissing.getDocCount();
    System.out.println(name + " " + count);
    

    同kibana命令如下:

    {
      "query": {
        "bool": {
          "adjust_pure_negative": true,
          "filter": [{
            "terms": {
              "boost": 1.0,
              "grade": ["高三", "高二"]
            }
          }],
          "boost": 1.0
        }
      },
      "size": 0,
      "aggs": {
        "sex_miss": {
          "missing": {
            "field": "sex"
          }
        }
      }
    }
    
    展开全文
  • elasticsearch 聚合查询无效

    千次阅读 多人点赞 2019-05-11 17:37:28
    elasticsearch 聚合查询无效 可能的原因: 数据迁移的时候,未考虑字段类型,导致ES 聚合查询时,对应字段的类型“text”,聚合查询是无法生效的, 生效类型只能是整形等 ...

    elasticsearch 聚合查询无效

    可能的原因:
    1. 数据迁移的时候,未考虑字段类型,导致ES 聚合查询时,对应字段的类型“text”,聚合查询是无法生效的,
      生效类型只能是整形等

    在这里插入图片描述

    最后,建了一个微信群,本来想大家一起共同学习,目前看来基本上每天都在聊天,加群主微信进群!

    展开全文
  • ElasticSearch聚合查询
  • 这里主要介绍PHP Elasticsearch 聚合查询的写法,如果不了解ES聚合查询,请参考ES聚合查询基本概念和语法。提示:ES聚合查询语法使用的是json格式描述,在php中使用需要转换成php数组描述例子下面是ES通过rest api...
  • Elasticsearch 聚合查询

    千次阅读 2018-01-25 18:05:05
    聚合字段需要为keyword类型、text类型需设置fielddata为true,将按照分词聚合 GET /my_index/_search { "query": { "size": 0, "aggs": { "models": { "terms":...
  • es聚合查询

    千次阅读 2017-08-22 19:51:21
    es常用聚合查询
  • ElasticSearch 聚合查询-脚本 •指标聚合:相当于MySQL的聚合函数。max、min、avg、sum等 •桶聚合:相当于MySQL的 group by 操作。不要对text类型的数据进行分组,会失败。 就是将 查询结果进行分组显示在控制台,...
  • ES聚合查询教程

    2020-07-18 16:34:06
    个人在学习聚合查询时,发现网上的一些博客写的对es新手不太友好,经过一番查找,发现了一个网站对于es聚合查询讲的很透彻和清晰,现在特此分享一下: Elasticsearch 聚合查询(aggs)基本概念:...
  • ElasticSearch聚合查询返回结果buckets取值 1.聚合查询如下: { "size":0, "query":{ "bool":{ "must":[ { "wildcard":{ "county_company.keyword":{ "wildcard":"*3*",
  • ElasticSearch(7.2.2)-es聚合查询之桶聚合

    千次阅读 2019-10-30 09:36:32
    简介:⼿把⼿玩转es聚合查询之桶聚合 ES聚合分析 聚合分析是数据库中重要的功能特性,完成对⼀个查询的数据集中数据的聚合计算,如:找出某字段(或计算表达式的结果)的最⼤值、最⼩值,计算和、平均值等。ES...
  • 简介:⼿把⼿玩转es聚合查询之指标聚合 ES聚合分析 聚合分析是数据库中重要的功能特性,完成对⼀个查询的数据集中数据的聚合计算,如:找出某字段(或计算表达式的结果)的最⼤值、最⼩值,计算和、平均值等。ES...
  • Elasticsearch教程(3) ES聚合查询DSL Elasticsearch教程(4) High Level REST Client API 查询 聚合 分组 Elasticsearch教程(5) 指标聚合 SQL DSL JavaAPI Elasticsearch教程(6) 桶聚合Query DSL-Terms Aggregation ...

空空如也

空空如也

1 2 3 4 5 ... 20
收藏数 35,051
精华内容 14,020
关键字:

es聚合查询