简体   繁体   中英

find doc count for nested aggregation of elasticsearch using java api

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

above is my Aggregation code

I am trying find docCount of gender as follow

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

I am getting irrelevant ans.

what I am doing wrong?

Change

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

to

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM