当前位置: 代码网 > it编程>编程语言>Asp.net > Elasticsearch 系列(五)- 数据聚合

Elasticsearch 系列(五)- 数据聚合

2024年05月16日 Asp.net 我要评论
本章将和大家分享 Elasticsearch 中的数据聚合功能,通过聚合(aggregations)可以实现对文档数据的统计、分析、运算。 ...

本章将和大家分享 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

版权声明:如有雷同纯属巧合,如有侵权请及时联系本人修改,谢谢!!!

(0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2025  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com