• 谷粒商城学习——P118聚合aggregation


    @es7.5聚合官网文档

    ES1:搜索address中包含mill的所有人的年龄分布以及平均年龄,但不显示这些人的详情

    ##搜索address中包含mill的所有人的年龄分布以及平均年龄
    GET /bank/_search
    {
      "query": {
        "match": {
          "address": "mill"
        }
      },
      "aggs": {##针对query查询结果做聚合
        "ageAgg": {##自定义的聚合名字
          "terms": {##聚合类型为terms
            "field": "age",##针对age聚合
            "size": 10##只取聚合结果的前10条
          }
        }
      }
    }

    返回结果:

    {
      "took" : 8,
      "timed_out" : false,
      "_shards" : {
        "total" : 1,
        "successful" : 1,
        "skipped" : 0,
        "failed" : 0
      },
      "hits" : {
        "total" : {
          "value" : 4,
          "relation" : "eq"
        },
        "max_score" : 5.4032025,
        "hits" : [
          {
            "_index" : "bank",
            "_type" : "account",
            "_id" : "970",
            "_score" : 5.4032025,
            "_source" : {
              "account_number" : 970,
              "balance" : 19648,
              "firstname" : "Forbes",
              "lastname" : "Wallace",
              "age" : 28,
              "gender" : "M",
              "address" : "990 Mill Road",
              "employer" : "Pheast",
              "email" : "forbeswallace@pheast.com",
              "city" : "Lopezo",
              "state" : "AK"
            }
          },
          {
            "_index" : "bank",
            "_type" : "account",
            "_id" : "136",
            "_score" : 5.4032025,
            "_source" : {
              "account_number" : 136,
              "balance" : 45801,
              "firstname" : "Winnie",
              "lastname" : "Holland",
              "age" : 38,
              "gender" : "M",
              "address" : "198 Mill Lane",
              "employer" : "Neteria",
              "email" : "winnieholland@neteria.com",
              "city" : "Urie",
              "state" : "IL"
            }
          },
          {
            "_index" : "bank",
            "_type" : "account",
            "_id" : "345",
            "_score" : 5.4032025,
            "_source" : {
              "account_number" : 345,
              "balance" : 9812,
              "firstname" : "Parker",
              "lastname" : "Hines",
              "age" : 38,
              "gender" : "M",
              "address" : "715 Mill Avenue",
              "employer" : "Baluba",
              "email" : "parkerhines@baluba.com",
              "city" : "Blackgum",
              "state" : "KY"
            }
          },
          {
            "_index" : "bank",
            "_type" : "account",
            "_id" : "472",
            "_score" : 5.4032025,
            "_source" : {
              "account_number" : 472,
              "balance" : 25571,
              "firstname" : "Lee",
              "lastname" : "Long",
              "age" : 32,
              "gender" : "F",
              "address" : "288 Mill Street",
              "employer" : "Comverges",
              "email" : "leelong@comverges.com",
              "city" : "Movico",
              "state" : "MT"
            }
          }
        ]
      },
      "aggregations" : {
        "ageAgg" : {##聚合名字
          "doc_count_error_upper_bound" : 0,
          "sum_other_doc_count" : 0,
          "buckets" : [
            {
              "key" : 38,##age为38的有2个
              "doc_count" : 2
            },
            {
              "key" : 28,##age为28的有1个
              "doc_count" : 1
            },
            {
              "key" : 32,
              "doc_count" : 1
            }
          ]
        }
      }
    }
    View Code

    ES2:在es1的基础上,查返回结果的所有人的(那4个人)平均年龄

    GET /bank/_search
    {
      "query": {
        "match": {
          "address": "mill"
        }
      },
      "aggs": {
        "ageAgg": {
          "terms": {
            "field": "age",
            "size": 10
          }
        },
        "ageAvg":{
          "avg": {##聚合类型avg
            "field": "age"
          }
        }
      }
    }

    返回结果:

    ES3:在es2的基础上,查所有人的平均薪资

    GET /bank/_search
    {
      "query": {
        "match": {
          "address": "mill"
        }
      },
      "aggs": {
        "ageAgg": {
          "terms": {
            "field": "age",
            "size": 10
          }
        },
        "ageAvg":{
          "avg": {
            "field": "age"
          }
        },
        "balanceAvg":{
          "avg": {
            "field": "balance"
          }
        }
      }
    }
    View Code

     ES4:不想看查到的结果只看聚合结果:指定分页size为0

    GET /bank/_search
    {
      "query": {
        "match": {
          "address": "mill"
        }
      },
      "aggs": {
        "ageAgg": {
          "terms": {
            "field": "age",
            "size": 10
          }
        },
        "ageAvg":{
          "avg": {
            "field": "age"
          }
        },
        "balanceAvg":{
          "avg": {
            "field": "balance"
          }
        }
      },
      "size": 0##返回数据0条
    }

    返回结果:

    复杂聚合:按照年龄聚合,并查询这些年龄段的这些人的平均薪资

      在年龄聚合里,添加子聚合查平均薪资

    GET /bank/_search
    {
      "query": {
        "match_all": {}
      },
      "aggs": {
        "ageAgg": {
          "terms": {
            "field": "age",
            "size": 3
          },
          "aggs": {##基于ageAgg继续聚合
            "ageAvg": {
              "avg": {
                "field": "balance"
              }
            }
          }
        }
      },
      "size": 0
    }

    返回结果

    {
      "took" : 5,
      "timed_out" : false,
      "_shards" : {
        "total" : 1,
        "successful" : 1,
        "skipped" : 0,
        "failed" : 0
      },
      "hits" : {
        "total" : {
          "value" : 1000,
          "relation" : "eq"
        },
        "max_score" : null,
        "hits" : [ ]
      },
      "aggregations" : {
        "ageAgg" : {
          "doc_count_error_upper_bound" : 0,
          "sum_other_doc_count" : 820,
          "buckets" : [
            {
              "key" : 31,
              "doc_count" : 61,
              "ageAvg" : {
                "value" : 28312.918032786885##年龄为31的61个人的平均薪资
              }
            },
            {
              "key" : 39,
              "doc_count" : 60,
              "ageAvg" : {
                "value" : 25269.583333333332
              }
            },
            {
              "key" : 26,
              "doc_count" : 59,
              "ageAvg" : {
                "value" : 23194.813559322032
              }
            }
          ]
        }
      }
    }

      ##查出所有年龄分布,并且这些年龄段中M的平均薪资和F的平均薪资以及这个年龄段的总体平均薪资

    GET /bank/_search
    {
      "query": {
        "match_all": {}
      },
      "size": 0,
      "aggs": {
        "ageAgg": {
          "terms": {
            "field": "age",
            "size": 3##按照年龄聚合,结果只取前3个,多了看着不方便
          },
          "aggs": {#ageAgg的子聚合
            "genderAgg": {
              "terms": {
                "field": "gender.keyword"##按照性别进行子聚合
              },
              "aggs": {
                "balanceAvg": {
                  "avg": {
                    "field": "balance"##性别基础上的平均
                  }
                }
              }
            },
            "balanceAvg": {##这个年龄段的总体平均薪资
              "avg": {
                "field": "balance"
              }
            }
          }
        }
      }
    }

    结果

    {
      "took" : 22,
      "timed_out" : false,
      "_shards" : {
        "total" : 1,
        "successful" : 1,
        "skipped" : 0,
        "failed" : 0
      },
      "hits" : {
        "total" : {
          "value" : 1000,
          "relation" : "eq"
        },
        "max_score" : null,
        "hits" : [ ]
      },
      "aggregations" : {
        "ageAgg" : {
          "doc_count_error_upper_bound" : 0,
          "sum_other_doc_count" : 820,
          "buckets" : [
            {
              "key" : 31,
              "doc_count" : 61,
              "genderAgg" : {
                "doc_count_error_upper_bound" : 0,
                "sum_other_doc_count" : 0,
                "buckets" : [
                  {
                    "key" : "M",
                    "doc_count" : 35,
                    "balanceAvg" : {
                      "value" : 29565.628571428573
                    }
                  },
                  {
                    "key" : "F",
                    "doc_count" : 26,
                    "balanceAvg" : {
                      "value" : 26626.576923076922
                    }
                  }
                ]
              },
              "balanceAvg" : {
                "value" : 28312.918032786885
              }
            },
            {
              "key" : 39,
              "doc_count" : 60,
              "genderAgg" : {
                "doc_count_error_upper_bound" : 0,
                "sum_other_doc_count" : 0,
                "buckets" : [
                  {
                    "key" : "F",
                    "doc_count" : 38,
                    "balanceAvg" : {
                      "value" : 26348.684210526317
                    }
                  },
                  {
                    "key" : "M",
                    "doc_count" : 22,
                    "balanceAvg" : {
                      "value" : 23405.68181818182
                    }
                  }
                ]
              },
              "balanceAvg" : {
                "value" : 25269.583333333332
              }
            },
            {
              "key" : 26,
              "doc_count" : 59,
              "genderAgg" : {
                "doc_count_error_upper_bound" : 0,
                "sum_other_doc_count" : 0,
                "buckets" : [
                  {
                    "key" : "M",
                    "doc_count" : 32,
                    "balanceAvg" : {
                      "value" : 25094.78125
                    }
                  },
                  {
                    "key" : "F",
                    "doc_count" : 27,
                    "balanceAvg" : {
                      "value" : 20943.0
                    }
                  }
                ]
              },
              "balanceAvg" : {
                "value" : 23194.813559322032
              }
            }
          ]
        }
      }
    }
    View Code
  • 相关阅读:
    html_table表格
    Spark 编程模型(上)
    hbase建表时 ERROR: java.io.IOException: Table Namespace Manager not ready yet, try again later
    ElasticSearch 优化
    ElasticSearch 索引模块——全文检索
    ElasticSearch 索引模块——集成IK中文分词
    elastisSearch-aggregations
    ElasticSearch 搜索原理
    ElasticSearch Document API
    ElasticSearch client API
  • 原文地址:https://www.cnblogs.com/yanan7890/p/15017803.html
Copyright © 2020-2023  润新知