java Elasticsearch nested sub polymerization

 

Aggregation subquery:

TermsAggregationBuilder aggregation = AggregationBuilders.terms("dt_id").field("dt_ids").size(30000);
        String agg_area_field = "city_code";
        TermsAggregationBuilder area_field_builder = AggregationBuilders.terms("area_field").field(agg_area_field).size(30000);
        area_field_builder.subAggregation(aggregation);
        SearchResponse response = client.prepareSearch(index_name).setTypes("lw_devices")
                .setQuery(boolQuery)
                .addAggregation(area_field_builder)
                .execute()
                .actionGet();

 

The above means that substantially demo, the first field of city_code polymerization, and the polymerization results and then nested fields dt_ids polymerization (polymerization promoter), the equivalent of two sql group by, polymerizing a polymerizable nested in another within, you may be associated with subAggregation method.

 

The polymerization is then traversal results:

 

        Terms terms = response.getAggregations().get("area_field");
        List<Terms.Bucket> buckets = terms.getBuckets();for (Terms.Bucket bucket : buckets) {
            //地区
            String code = (String) bucket.getKey();
            System.out.println(code);
            Aggregations aggregations = bucket.getAggregations();
            Terms dt_id = aggregations.get("dt_id");
            List<Terms.Bucket> buckets1 = dt_id.getBuckets();
            for (Terms.Bucket bucket1 : buckets1) {
                System.out.println(bucket1.getKey() + ":" + bucket1.getDocCount());
            }
        }

 

The idea is to first get the response according aggregation, based on the identifier and then get the corresponding type of polymerization, and the resulting collection bucket. Then get the bucket by aggregation, and results of the aggregation down this cycle.

Guess you like

Origin www.cnblogs.com/chenmz1995/p/11583915.html