Elasticsearch 系列(五)- 資料聚合

谢友海發表於2024-03-31

本章將和大家分享 Elasticsearch 中的資料聚合功能,透過聚合(aggregations)可以實現對文件資料的統計、分析、運算。

一、資料聚合-聚合的分類

聚合(aggregations)可以實現對文件資料的統計、分析、運算。聚合的官方文件地址:https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations.html

聚合常見的有三類:

1)桶(Bucket)聚合:用來對文件做分組

  • TermAggregation:按照文件欄位值分組
  • Date Histogram:按照日期階梯分組,例如:一週為一組,或者一月為一組。

2)度量(Metric)聚合:用於計算一些值,比如:最大值、最小值、平均值等。

  • Avg:求平均值
  • Max:求最大值
  • Min:求最小值
  • Stats:同時求max、min、avg、sum等。

3)管道(Pipeline)聚合:以其它聚合的結果為基礎做聚合。

總結:

1)什麼是聚合?

  • 聚合是對文件資料的統計、分析、計算

2)聚合的常見種類有哪些?

  • Bucket:對文件資料分組,並統計每組數量
  • Metric:對文件資料做計算,例如:avg
  • Pipeline:基於其它聚合結果再做聚合

3)參與聚合的欄位型別不能是 text(可分詞的文字)型別,可以是:keyword、數值、日期、布林型別。

二、資料聚合-DSL實現Bucket聚合

1、DSL實現Bucket聚合

現在,我們要統計所有資料中的酒店品牌有幾種,此時可以根據酒店品牌的名稱做聚合。

型別為term型別,DSL示例:

# 聚合功能
GET /hotel/_search
{
  "size": 0, //設定size為0,結果中不包含文件,只包含聚合結果
  "aggs": { //定義聚合
    "brandAgg": { //給聚合起個名字
      "terms": { //聚合的型別,按照品牌值聚合,所以選擇term
        "field": "brand", //參與聚合的欄位
        "size": 10 //希望獲取的聚合結果數量
      }
    }
  }
}

執行結果如下:

{
  "took" : 1,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 201,
      "relation" : "eq"
    },
    "max_score" : null,
    "hits" : [ ]
  },
  "aggregations" : {
    "brandAgg" : {
      "doc_count_error_upper_bound" : 0,
      "sum_other_doc_count" : 39,
      "buckets" : [
        {
          "key" : "7天酒店",
          "doc_count" : 30
        },
        {
          "key" : "如家",
          "doc_count" : 30
        },
        {
          "key" : "皇冠假日",
          "doc_count" : 17
        },
        {
          "key" : "速8",
          "doc_count" : 15
        },
        {
          "key" : "萬怡",
          "doc_count" : 13
        },
        {
          "key" : "華美達",
          "doc_count" : 13
        },
        {
          "key" : "和頤",
          "doc_count" : 12
        },
        {
          "key" : "萬豪",
          "doc_count" : 11
        },
        {
          "key" : "喜來登",
          "doc_count" : 11
        },
        {
          "key" : "希爾頓",
          "doc_count" : 10
        }
      ]
    }
  }
}

2、Bucket聚合-聚合結果排序

預設情況下,Bucket聚合會統計Bucket內的文件數量,記為_count,並且按照_count降序排序。

我們可以修改結果排序方式:

# 聚合功能,自定義排序規則
GET /hotel/_search
{
  "size": 0,
  "aggs": {
    "brandAgg": {
      "terms": {
        "field": "brand",
        "size": 10,
        "order": {
          "_count": "asc" //按照_count升序排列
        }
      }
    }
  }
}

執行結果如下:

{
  "took" : 0,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 201,
      "relation" : "eq"
    },
    "max_score" : null,
    "hits" : [ ]
  },
  "aggregations" : {
    "brandAgg" : {
      "doc_count_error_upper_bound" : 0,
      "sum_other_doc_count" : 130,
      "buckets" : [
        {
          "key" : "萬麗",
          "doc_count" : 2
        },
        {
          "key" : "麗笙",
          "doc_count" : 2
        },
        {
          "key" : "君悅",
          "doc_count" : 4
        },
        {
          "key" : "豪生",
          "doc_count" : 6
        },
        {
          "key" : "維也納",
          "doc_count" : 7
        },
        {
          "key" : "凱悅",
          "doc_count" : 8
        },
        {
          "key" : "希爾頓",
          "doc_count" : 10
        },
        {
          "key" : "漢庭",
          "doc_count" : 10
        },
        {
          "key" : "萬豪",
          "doc_count" : 11
        },
        {
          "key" : "喜來登",
          "doc_count" : 11
        }
      ]
    }
  }
}

3、Bucket聚合-限定聚合範圍

預設情況下,Bucket聚合是對索引庫的所有文件做聚合,我們可以限定要聚合的文件範圍,只要新增query條件即可。

示例:

# 聚合功能,限定聚合範圍
GET /hotel/_search
{
  "query": {
    "range": {
      "price": {
        "lte": 200 //只對200元以下的文件聚合
      }
    }
  },
  "size": 0,
  "aggs": {
    "brandAgg": {
      "terms": {
        "field": "brand",
        "size": 10,
        "order": {
          "_count": "asc"
        }
      }
    }
  }
}

執行結果如下:

{
  "took" : 0,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 17,
      "relation" : "eq"
    },
    "max_score" : null,
    "hits" : [ ]
  },
  "aggregations" : {
    "brandAgg" : {
      "doc_count_error_upper_bound" : 0,
      "sum_other_doc_count" : 0,
      "buckets" : [
        {
          "key" : "7天酒店",
          "doc_count" : 1
        },
        {
          "key" : "漢庭",
          "doc_count" : 1
        },
        {
          "key" : "速8",
          "doc_count" : 2
        },
        {
          "key" : "如家",
          "doc_count" : 13
        }
      ]
    }
  }
}

4、總結

1)aggs代表聚合,與query同級,此時query的作用是?

  • 限定聚合的的文件範圍

2)聚合必須的三要素是什麼?

  • 聚合名稱
  • 聚合型別
  • 聚合欄位

3)聚合可配置屬性有哪些?

  • size:指定聚合結果數量
  • order:指定聚合結果排序方式
  • field:指定聚合欄位

三、資料聚合-DSL實現Metric聚合

例如:我們要求獲取每個品牌的使用者評分的min、max、avg等值。

我們可以利用stats聚合:

# 巢狀聚合Metric
GET /hotel/_search
{
  "size": 0,
  "aggs": {
    "brandAgg": {
      "terms": {
        "field": "brand",
        "size": 10,
        "order": {
          "scoreAgg.avg": "desc" //對桶裡面的資料做排序
        }
      },
      "aggs": { //是brandAgg聚合的子聚合,也就是分組後對每組分別計算
        "scoreAgg": { //聚合名稱
          "stats": { //聚合型別,這裡stats可以計算min、max、avg等
            "field": "score" //聚合欄位,這裡是score
          }
        }
      }
    }
  }
}

執行結果如下所示:

{
  "took" : 0,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 201,
      "relation" : "eq"
    },
    "max_score" : null,
    "hits" : [ ]
  },
  "aggregations" : {
    "brandAgg" : {
      "doc_count_error_upper_bound" : 0,
      "sum_other_doc_count" : 111,
      "buckets" : [
        {
          "key" : "萬麗",
          "doc_count" : 2,
          "scoreAgg" : {
            "count" : 2,
            "min" : 46.0,
            "max" : 47.0,
            "avg" : 46.5,
            "sum" : 93.0
          }
        },
        {
          "key" : "凱悅",
          "doc_count" : 8,
          "scoreAgg" : {
            "count" : 8,
            "min" : 45.0,
            "max" : 47.0,
            "avg" : 46.25,
            "sum" : 370.0
          }
        },
        {
          "key" : "和頤",
          "doc_count" : 12,
          "scoreAgg" : {
            "count" : 12,
            "min" : 44.0,
            "max" : 47.0,
            "avg" : 46.083333333333336,
            "sum" : 553.0
          }
        },
        {
          "key" : "麗笙",
          "doc_count" : 2,
          "scoreAgg" : {
            "count" : 2,
            "min" : 46.0,
            "max" : 46.0,
            "avg" : 46.0,
            "sum" : 92.0
          }
        },
        {
          "key" : "喜來登",
          "doc_count" : 11,
          "scoreAgg" : {
            "count" : 11,
            "min" : 44.0,
            "max" : 48.0,
            "avg" : 46.0,
            "sum" : 506.0
          }
        },
        {
          "key" : "皇冠假日",
          "doc_count" : 17,
          "scoreAgg" : {
            "count" : 17,
            "min" : 44.0,
            "max" : 48.0,
            "avg" : 46.0,
            "sum" : 782.0
          }
        },
        {
          "key" : "萬豪",
          "doc_count" : 11,
          "scoreAgg" : {
            "count" : 11,
            "min" : 43.0,
            "max" : 47.0,
            "avg" : 45.81818181818182,
            "sum" : 504.0
          }
        },
        {
          "key" : "萬怡",
          "doc_count" : 13,
          "scoreAgg" : {
            "count" : 13,
            "min" : 44.0,
            "max" : 48.0,
            "avg" : 45.69230769230769,
            "sum" : 594.0
          }
        },
        {
          "key" : "君悅",
          "doc_count" : 4,
          "scoreAgg" : {
            "count" : 4,
            "min" : 44.0,
            "max" : 47.0,
            "avg" : 45.5,
            "sum" : 182.0
          }
        },
        {
          "key" : "希爾頓",
          "doc_count" : 10,
          "scoreAgg" : {
            "count" : 10,
            "min" : 37.0,
            "max" : 48.0,
            "avg" : 45.4,
            "sum" : 454.0
          }
        }
      ]
    }
  }
}

四、資料聚合-多條件聚合

需求:搜尋頁面中的城市、星級、品牌等資訊不應該是在頁面寫死,而是透過聚合索引庫中的酒店資料得來的。

示例:

# 多條件聚合
GET /hotel/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "match": {
            "all": "酒店"
          }
        }
      ],
      "should": [
        {
          "term": {
            "brand": "皇冠假日"
          }
        },
        {
          "term": {
            "brand": "華美達"
          }
        }
      ],
      "must_not": [
        {
          "range": {
            "price": {
              "lte": 500
            }
          }
        }
      ],
      "filter": [
        {
          "range": {
            "score": {
              "gte": 45
            }
          }
        }
      ],
      "minimum_should_match": 1,
      "boost": 1
    }
  },
  "size": 0,
  "aggs": {
    "cityAgg": {
      "terms": {
        "field": "city",
        "size": 10,
        "order": {
          "_count": "desc"
        }
      }
    },
    "starNameAgg": {
      "terms": {
        "field": "starName",
        "size": 10,
        "order": {
          "_count": "desc"
        }
      }
    },
    "brandAgg": {
      "terms": {
        "field": "brand",
        "size": 10,
        "order": {
          "scoreAgg.avg": "desc"
        }
      },
      "aggs": {
        "scoreAgg": {
          "stats": {
            "field": "score"
          }
        }
      }
    }
  }
}

執行結果如下:

{
  "took" : 1,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 17,
      "relation" : "eq"
    },
    "max_score" : null,
    "hits" : [ ]
  },
  "aggregations" : {
    "brandAgg" : {
      "doc_count_error_upper_bound" : 0,
      "sum_other_doc_count" : 0,
      "buckets" : [
        {
          "key" : "皇冠假日",
          "doc_count" : 12,
          "scoreAgg" : {
            "count" : 12,
            "min" : 45.0,
            "max" : 48.0,
            "avg" : 46.416666666666664,
            "sum" : 557.0
          }
        },
        {
          "key" : "華美達",
          "doc_count" : 5,
          "scoreAgg" : {
            "count" : 5,
            "min" : 45.0,
            "max" : 46.0,
            "avg" : 45.2,
            "sum" : 226.0
          }
        }
      ]
    },
    "starNameAgg" : {
      "doc_count_error_upper_bound" : 0,
      "sum_other_doc_count" : 0,
      "buckets" : [
        {
          "key" : "五鑽",
          "doc_count" : 7
        },
        {
          "key" : "五星級",
          "doc_count" : 5
        },
        {
          "key" : "四星級",
          "doc_count" : 3
        },
        {
          "key" : "四鑽",
          "doc_count" : 2
        }
      ]
    },
    "cityAgg" : {
      "doc_count_error_upper_bound" : 0,
      "sum_other_doc_count" : 0,
      "buckets" : [
        {
          "key" : "上海",
          "doc_count" : 10
        },
        {
          "key" : "北京",
          "doc_count" : 4
        },
        {
          "key" : "深圳",
          "doc_count" : 3
        }
      ]
    }
  }
}

至此本文就全部介紹完了,如果覺得對您有所啟發請記得點個贊哦!!!

此文由博主精心撰寫轉載請保留此原文連結:https://www.cnblogs.com/xyh9039/p/18093166

版權宣告:如有雷同純屬巧合,如有侵權請及時聯絡本人修改,謝謝!!!

相關文章