elasticsearch 学习之路-过滤后聚合(FilterAggregationBuilder)
elasticsearch 学习之路-过滤后聚合(FilterAggregationBuilder)
翱翔fly 发表于7个月前
elasticsearch 学习之路-过滤后聚合(FilterAggregationBuilder)
  • 发表于 7个月前
  • 阅读 547
  • 收藏 18
  • 点赞 0
  • 评论 0

腾讯云 学生专属云服务套餐 10元起购>>>   

FilterAggregationBuilder:是elasticsearch 中过滤后,聚合一个神奇工具,官网例子:

POST /sales/_search?size=0
{
    "aggs" : {
        "t_shirts" : {
            "filter" : { "term": { "type": "t-shirt" } },
            "aggs" : {
                "avg_price" : { "avg" : { "field" : "price" } }
            }
        }
    }
}

显示 类型 t-shirt 的平均价格,先根据类型过滤,然后在进行聚合计算

在java api 中使用的例子:其中某些值需要使用者,修改,

AggregationBuilder aggregation= AggregationBuilders.terms("customer").field("province.raw");
//新增客户
BoolQueryBuilder addCustomer=QueryBuilders.boolQuery();
addCustomer.must(QueryBuilders.rangeQuery("start_time").from(System.currentTimeMillis()-(10*1000 * 60 * 60 * 24)).to(
        Instant.now().toEpochMilli()));
FilterAggregationBuilder addAggregationBuilder= AggregationBuilders.filter("add",
        addCustomer).subAggregation(aggregation);
//新增签约
BoolQueryBuilder addSignBuilder=QueryBuilders.boolQuery();
//客户id
addSignBuilder.must(QueryBuilders.termsQuery("id.raw","36v91l0mrP_76359904-5274-4a0a-a4ac-9cffb64ef079"));
//客户创建时间
addSignBuilder.must(QueryBuilders.rangeQuery("start_time").from(System.currentTimeMillis()-(10*1000 * 60 * 60 * 24)).to(
        Instant.now().toEpochMilli()));
FilterAggregationBuilder  addSignAggBuilder= AggregationBuilders.filter("add_sign",
        addSignBuilder).subAggregation(aggregation);
//签约客户
BoolQueryBuilder sign=QueryBuilders.boolQuery();
//客户id
sign.must(QueryBuilders.termsQuery("id.raw","36v91l0mrP_76359904-5274-4a0a-a4ac-9cffb64ef079"));
//
FilterAggregationBuilder signAggBuilder= AggregationBuilders.filter("sign",
        sign).subAggregation(aggregation);
SearchRequestBuilder searchRequestBuilder=EsClient.client.filterWithHeader(headers).prepareSearch("customer_36v91l0mrp*").setTypes("customer").
        setSize(5).setQuery(boolQueryBuilder).setFetchSource(new String[]{"customer_id"},null)
        .addAggregation(addSignAggBuilder).addAggregation(addAggregationBuilder).addAggregation(signAggBuilder);
LOGGER.info("查询条件:"+searchRequestBuilder.toString());
SearchResponse searchResponse=searchRequestBuilder.execute().actionGet();
共有 人打赏支持
粉丝 13
博文 35
码字总数 5703
×
翱翔fly
如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!
* 金额(元)
¥1 ¥5 ¥10 ¥20 其他金额
打赏人
留言
* 支付类型
微信扫码支付
打赏金额:
已支付成功
打赏金额: