ElasticSearch7.3學習(二十七)----聚合概念(bucket和metric)及其示例

|舊市拾荒| 發表於 2022-05-22
ElasticSearch

一、兩個核心概念:bucket和metric

1.1 bucket

有如下資料

city name 
北京 張三 
北京 李四
天津 王五
天津 趙六
天津 王麻子

劃分出來兩個bucket,一個是北京bucket,一個是天津bucket

北京bucket:包含了2個人,張三,李四

上海bucket:包含了3個人,王五,趙六,王麻子

1.2 metric

metric,就是對一個bucket執行的某種聚合分析的操作,比如說求平均值,求最大值,求最小值

比如下面的一個sql語句

select count(*) from book group studymodel

bucket:group by studymodel --> 那些studymodel相同的資料,就會被劃分到一個bucket中

metric:count(*),對每個bucket中所有的資料,計算一個數量。例如avg(),sum(),max(),min()

二、聚合示例

2.1 資料準備

首先建立book索引

PUT /book/
{
  "settings": {
    "number_of_shards": 1,
    "number_of_replicas": 0
  },
  "mappings": {
    "properties": {
      "name": {
        "type": "text",
        "analyzer": "ik_max_word",
        "search_analyzer": "ik_smart"
      },
      "description": {
        "type": "text",
        "analyzer": "ik_max_word",
        "search_analyzer": "ik_smart"
      },
      "studymodel": {
        "type": "keyword"
      },
      "price": {
        "type": "double"
      },
      "timestamp": {
        "type": "date",
        "format": "yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis"
      },
      "pic": {
        "type": "text",
        "index": false
      }
    }
  }
}

新增測試資料

PUT /book/_doc/1
{
  "name": "Bootstrap開發",
  "description": "Bootstrap是一個非常流行的開發框架。此開發框架可以幫助不擅長css頁面開發的程式人員輕鬆的實現一個css,不受瀏覽器限制的精美介面css效果。",
  "studymodel": "201002",
  "price": 38.6,
  "timestamp": "2019-08-25 19:11:35",
  "pic": "group1/M00/00/00/wKhlQFs6RCeAY0pHAAJx5ZjNDEM428.jpg",
  "tags": [
    "bootstrap",
    "dev"
  ]
}

PUT /book/_doc/2
{
  "name": "java程式設計思想",
  "description": "java語言是世界第一程式語言,在軟體開發領域使用人數最多。",
  "studymodel": "201001",
  "price": 68.6,
  "timestamp": "2019-08-25 19:11:35",
  "pic": "group1/M00/00/00/wKhlQFs6RCeAY0pHAAJx5ZjNDEM428.jpg",
  "tags": [
    "java",
    "dev"
  ]
}

PUT /book/_doc/3
{
  "name": "spring開發基礎",
  "description": "spring 在java領域非常流行,java程式設計師都在用。",
  "studymodel": "201001",
  "price": 88.6,
  "timestamp": "2019-08-24 19:11:35",
  "pic": "group1/M00/00/00/wKhlQFs6RCeAY0pHAAJx5ZjNDEM428.jpg",
  "tags": [
    "spring",
    "java"
  ]
}

2.2 計算每個studymodel下的商品數量

sql語句: select studymodel,count(*) from book group by studymodel

"size": 0,   ==>  作用 :只需要聚合的資料,不需要查詢的資料

GET /book/_search
{
  "size": 0,
  "query": {
    "match_all": {}
  },
  "aggs": {
    "group_by_model": {
      "terms": {
        "field": "studymodel"
      }
    }
  }
}

結果:

{
  "took" : 2,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 3,
      "relation" : "eq"
    },
    "max_score" : null,
    "hits" : [ ]
  },
  "aggregations" : {
    "group_by_model" : {
      "doc_count_error_upper_bound" : 0,
      "sum_other_doc_count" : 0,
      "buckets" : [
        {
          "key" : "201001",
          "doc_count" : 2
        },
        {
          "key" : "201002",
          "doc_count" : 1
        }
      ]
    }
  }
}

2.3 計算每個tags下的商品數量

設定欄位"fielddata": true,不設定會報錯

PUT /book/_mapping/
{
  "properties": {
    "tags": {
      "type": "text",
      "fielddata": true
    }
  }
}

查詢

GET /book/_search
{
  "size": 0, 
  "query": {
    "match_all": {}
  }, 
  "aggs": {
    "group_by_tags": {
      "terms": { "field": "tags" }
    }
  }
}

結果:

{
  "took" : 2,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 3,
      "relation" : "eq"
    },
    "max_score" : null,
    "hits" : [ ]
  },
  "aggregations" : {
    "group_by_tags" : {
      "doc_count_error_upper_bound" : 0,
      "sum_other_doc_count" : 0,
      "buckets" : [
        {
          "key" : "dev",
          "doc_count" : 2
        },
        {
          "key" : "java",
          "doc_count" : 2
        },
        {
          "key" : "bootstrap",
          "doc_count" : 1
        },
        {
          "key" : "spring",
          "doc_count" : 1
        }
      ]
    }
  }
}

2.4 加上搜尋條件,計算每個tags下的商品數量

GET /book/_search
{
  "size": 0, 
  "query": {
    "match": {
      "description": "java程式設計師"
    }
  }, 
  "aggs": {
    "group_by_tags": {
      "terms": { "field": "tags" }
    }
  }
}

結果:

{
  "took" : 70,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 2,
      "relation" : "eq"
    },
    "max_score" : null,
    "hits" : [ ]
  },
  "aggregations" : {
    "group_by_tags" : {
      "doc_count_error_upper_bound" : 0,
      "sum_other_doc_count" : 0,
      "buckets" : [
        {
          "key" : "java",
          "doc_count" : 2
        },
        {
          "key" : "dev",
          "doc_count" : 1
        },
        {
          "key" : "spring",
          "doc_count" : 1
        }
      ]
    }
  }
}

2.5 計算每個tag下的商品的平均價格

子聚合

GET /book/_search
{
  "size": 0,
  "aggs": {
    "group_by_tags": {
      "terms": {
        "field": "tags"
      },
      "aggs": {
        "avg_price": {
          "avg": {
            "field": "price"
          }
        }
      }
    }
  }
}

結果:

{
  "took" : 0,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 3,
      "relation" : "eq"
    },
    "max_score" : null,
    "hits" : [ ]
  },
  "aggregations" : {
    "group_by_tags" : {
      "doc_count_error_upper_bound" : 0,
      "sum_other_doc_count" : 0,
      "buckets" : [
        {
          "key" : "dev",
          "doc_count" : 2,
          "avg_price" : {
            "value" : 53.599999999999994
          }
        },
        {
          "key" : "java",
          "doc_count" : 2,
          "avg_price" : {
            "value" : 78.6
          }
        },
        {
          "key" : "bootstrap",
          "doc_count" : 1,
          "avg_price" : {
            "value" : 38.6
          }
        },
        {
          "key" : "spring",
          "doc_count" : 1,
          "avg_price" : {
            "value" : 88.6
          }
        }
      ]
    }
  }
}

2.6 計算每個tag下的商品的平均價格,按照平均價格降序排序

小技巧,如果是查詢全部,match_all可省略

GET /book/_search
{
  "size": 0,
  "aggs": {
    "group_by_tags": {
      "terms": {
        "field": "tags",
        "order": {
          "avg_price": "desc"
        }
      },
      "aggs": {
        "avg_price": {
          "avg": {
            "field": "price"
          }
        }
      }
    }
  }
}

結果:

{
  "took" : 4,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 3,
      "relation" : "eq"
    },
    "max_score" : null,
    "hits" : [ ]
  },
  "aggregations" : {
    "group_by_tags" : {
      "doc_count_error_upper_bound" : 0,
      "sum_other_doc_count" : 0,
      "buckets" : [
        {
          "key" : "spring",
          "doc_count" : 1,
          "avg_price" : {
            "value" : 88.6
          }
        },
        {
          "key" : "java",
          "doc_count" : 2,
          "avg_price" : {
            "value" : 78.6
          }
        },
        {
          "key" : "dev",
          "doc_count" : 2,
          "avg_price" : {
            "value" : 53.599999999999994
          }
        },
        {
          "key" : "bootstrap",
          "doc_count" : 1,
          "avg_price" : {
            "value" : 38.6
          }
        }
      ]
    }
  }
}

2.7 按照指定的價格範圍區間進行分組,然後在每組內再按照tag進行分組,最後再計算每組的平均價格

GET /book/_search
{
  "size": 0,
  "aggs": {
    "group_by_price": {
      "range": {
        "field": "price",
        "ranges": [
          {
            "from": 0,
            "to": 40
          },
          {
            "from": 40,
            "to": 60
          },
          {
            "from": 60,
            "to": 80
          }
        ]
      },
      "aggs": {
        "group_by_tags": {
          "terms": {
            "field": "tags"
          },
          "aggs": {
            "average_price": {
              "avg": {
                "field": "price"
              }
            }
          }
        }
      }
    }
  }
}

結果:

{
  "took" : 5,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 3,
      "relation" : "eq"
    },
    "max_score" : null,
    "hits" : [ ]
  },
  "aggregations" : {
    "group_by_price" : {
      "buckets" : [
        {
          "key" : "0.0-40.0",
          "from" : 0.0,
          "to" : 40.0,
          "doc_count" : 1,
          "group_by_tags" : {
            "doc_count_error_upper_bound" : 0,
            "sum_other_doc_count" : 0,
            "buckets" : [
              {
                "key" : "bootstrap",
                "doc_count" : 1,
                "average_price" : {
                  "value" : 38.6
                }
              },
              {
                "key" : "dev",
                "doc_count" : 1,
                "average_price" : {
                  "value" : 38.6
                }
              }
            ]
          }
        },
        {
          "key" : "40.0-60.0",
          "from" : 40.0,
          "to" : 60.0,
          "doc_count" : 0,
          "group_by_tags" : {
            "doc_count_error_upper_bound" : 0,
            "sum_other_doc_count" : 0,
            "buckets" : [ ]
          }
        },
        {
          "key" : "60.0-80.0",
          "from" : 60.0,
          "to" : 80.0,
          "doc_count" : 1,
          "group_by_tags" : {
            "doc_count_error_upper_bound" : 0,
            "sum_other_doc_count" : 0,
            "buckets" : [
              {
                "key" : "dev",
                "doc_count" : 1,
                "average_price" : {
                  "value" : 68.6
                }
              },
              {
                "key" : "java",
                "doc_count" : 1,
                "average_price" : {
                  "value" : 68.6
                }
              }
            ]
          }
        }
      ]
    }
  }
}