elasticsearch 学习之路-过滤后聚合(FilterAggregationBuilder) - 翱翔fly的个人空间 - 开源中国

【新睿云 】云服务器0元/年 >>>   

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();


Original url: Access
Created at: 2018-10-18 15:03:13
Category: default
Tags: none

请先后发表评论
  • 最新评论
  • 总共0条评论