第73节:Java API初步使用_员工管理案例:基于Java对员工信息进行聚合分析

课程大纲

 

SearchResponse sr = node.client().prepareSearch()

    .addAggregation(

        AggregationBuilders.terms("by_country").field("country")

        .subAggregation(AggregationBuilders.dateHistogram("by_year")

            .field("dateOfBirth")

            .dateHistogramInterval(DateHistogramInterval.YEAR)

            .subAggregation(AggregationBuilders.avg("avg_children").field("children"))

        )

    )

    .execute().actionGet();

 

我们先给个需求:

 

(1)首先按照country国家来进行分组

(2)然后在每个country分组内,再按照入职年限进行分组

(3)最后计算每个分组内的平均薪资

 

PUT /company

{

  "mappings": {

      "employee": {

        "properties": {

          "age": {

            "type": "long"

          },

          "country": {

            "type": "text",

            "fields": {

              "keyword": {

                "type": "keyword",

                "ignore_above": 256

              }

            },

            "fielddata": true

          },

          "join_date": {

            "type": "date"

          },

          "name": {

            "type": "text",

            "fields": {

              "keyword": {

                "type": "keyword",

                "ignore_above": 256

              }

            }

          },

          "position": {

            "type": "text",

            "fields": {

              "keyword": {

                "type": "keyword",

                "ignore_above": 256

              }

            }

          },

          "salary": {

            "type": "long"

          }

        }

      }

    }

}

 

GET /company/employee/_search

{

  "size": 0,

  "aggs": {

    "group_by_country": {

      "terms": {

        "field": "country"

      },

      "aggs": {

        "group_by_join_date": {

          "date_histogram": {

            "field": "join_date",

            "interval": "year"

          },

          "aggs": {

            "avg_salary": {

              "avg": {

                "field": "salary"

              }

            }

          }

        }

      }

    }

  }

}

 

Map aggrMap = searchResponse.getAggregations().asMap();

              StringTerms groupByCountry = (StringTerms) aggrMap.get("group_by_country");

              Iterator groupByCountryBucketIterator = groupByCountry.getBuckets().iterator();

             

              while(groupByCountryBucketIterator.hasNext()) {

                     Bucket groupByCountryBucket = groupByCountryBucketIterator.next();

                    

                     System.out.println(groupByCountryBucket.getKey() + "\t" + groupByCountryBucket.getDocCount());

                    

                     Histogram groupByJoinDate = (Histogram) groupByCountryBucket.getAggregations().asMap().get("group_by_join_date");

              Iterator groupByJoinDateBucketIterator = groupByJoinDate.getBuckets().iterator();

                      

                     while(groupByJoinDateBucketIterator.hasNext()) {

                            org.elasticsearch.search.aggregations.bucket.histogram.Histogram.Bucket groupByJoinDateBucket = groupByJoinDateBucketIterator.next();

                           

                            System.out.println(groupByJoinDateBucket.getKey() + "\t" + groupByJoinDateBucket.getDocCount());

                           

                            Avg avgSalary = (Avg) groupByJoinDateBucket.getAggregations().asMap().get("avg_salary");

                            System.out.println(avgSalary.getValue());

                     }

              }

             

              client.close();

       }

你可能感兴趣的:(elasticsearch)