使用java api查找elasticsearch的嵌套聚合的doc count

时间:2016-03-30 04:52:44

标签: java elasticsearch

searchRequestBuilder.addAggregation(
            AggregationBuilders.nested("skuFilter").path("skus")
            .subAggregation(AggregationBuilders
                .filter("sizeFilterCondition")
                .filter(FilterBuilders.boolFilter().must(FilterBuilders.termsFilter("skus.gender", "Dad\'s", "Mom\'s")))
                .subAggregation(
                    AggregationBuilders.nested("sizeValues").path("skus.attribute")
                        .subAggregation(AggregationBuilders.terms("attributeId").field("skus.attribute.attribute_id").size(AGGREGATION_SIZE)
                            .subAggregation(AggregationBuilders.terms("attributeValue").field("skus.attribute.attribute_value").size(AGGREGATION_SIZE)))))
            .subAggregation(AggregationBuilders.terms("fromAge").field("skus.from_age").size(AGGREGATION_SIZE)
                .subAggregation(AggregationBuilders.terms("toAge").field("skus.to_age").size(AGGREGATION_SIZE)))
            .subAggregation(AggregationBuilders.terms("gender").field("skus.gender").size(AGGREGATION_SIZE))
            .subAggregation(AggregationBuilders.min("min_price").field("skus.sale_price"))
            .subAggregation(AggregationBuilders.max("max_price").field("skus.sale_price"))
    );

以上是我的聚合代码

我正在尝试查找性别docCount,如下所示

Nested agg = searchResponse.getAggregations().get("skuFilter");
        StringTerms terms = (StringTerms) agg.getAggregations().getAsMap().get("gender");

    List<Bucket> termsList = terms.getBuckets();
    for (Bucket bucket : termsList) {
        System.out.println(bucket.getKey());
        System.out.println(bucket.getDocCount());
    }

我变得无关紧要了。

我做错了什么?

1 个答案:

答案 0 :(得分:0)

更改

Nested agg = searchResponse.getAggregations().get("skuFilter");  

Nested agg = searchResponse.getAggregations().getAsMap().get("skuFilter");