经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 程序设计 » Elasticsearch » 查看文章
Elasticsearch 常用的聚合操作
来源:cnblogs  作者:不颓废青年  时间:2021/5/31 9:03:03  对本文有异议

Aggregation 简介

ps : 本篇文章 Elasticsearch 和 Kibana 版本为 7.10.1。如果版本不一致请查看官方文档,避免误导!

聚合框架有助于基于搜索查询提供聚合数据。它基于称为聚合的简单构建块,可以组合以构建复杂的数据摘要。

Elasticsearch 将聚合分为三类:

  • Metric (指标聚合)

    从字段值计算度量的聚合,例如最大、最小、总和和平均值。

  • Bucket (桶聚合)

    根据字段值、范围或其他条件将文档分组为桶(也称为箱),类似于关系型数据库中的group by。

  • Pipeline (管道聚合)

    从其他聚合而不是文档或字段中获取输入的聚合。

聚合可以将我们的数据汇总为指标,统计或其他分析信息。使用聚合可以为我们带来的好处:

  • 我的网站的平均加载时间是多少?
  • 根据交易量,谁是我最有价值的客户?
  • 什么会被视为我网络上的大文件?
  • 每个产品类别中有多少个产品?

数据准备

创建索引

  1. DELETE twitter
  2. PUT twitter
  3. {
  4. "settings": {
  5. "number_of_shards": 2,
  6. "number_of_replicas": 1
  7. },
  8. "mappings": {
  9. "properties": {
  10. "birthday": {
  11. "type": "date"
  12. },
  13. "address": {
  14. "type": "text",
  15. "fields": {
  16. "keyword": {
  17. "type": "keyword",
  18. "ignore_above": 256
  19. }
  20. }
  21. },
  22. "age": {
  23. "type": "long"
  24. },
  25. "city": {
  26. "type": "keyword"
  27. },
  28. "country": {
  29. "type": "keyword"
  30. },
  31. "location": {
  32. "type": "geo_point"
  33. },
  34. "message": {
  35. "type": "text",
  36. "fields": {
  37. "keyword": {
  38. "type": "keyword",
  39. "ignore_above": 256
  40. }
  41. }
  42. },
  43. "province": {
  44. "type": "keyword"
  45. },
  46. "uid": {
  47. "type": "long"
  48. },
  49. "user": {
  50. "type": "text",
  51. "fields": {
  52. "keyword": {
  53. "type": "keyword",
  54. "ignore_above": 256
  55. }
  56. }
  57. }
  58. }
  59. }
  60. }

导入数据

使用 Bulk API 将数据导入到 Elasticsearch 中:

  1. POST _bulk
  2. {"index":{"_index":"twitter","_id":1}}
  3. {"user":"张三","message":"今儿天气不错啊,出去转转去","uid":2,"age":20,"city":"北京","province":"北京","country":"中国","address":"中国北京市海淀区","location":{"lat":"39.970718","lon":"116.325747"}, "birthday": "1999-04-01"}
  4. {"index":{"_index":"twitter","_id":2}}
  5. {"user":"老刘","message":"出发,下一站云南!","uid":3,"age":22,"city":"北京","province":"北京","country":"中国","address":"中国北京市东城区台基厂三条3号","location":{"lat":"39.904313","lon":"116.412754"}, "birthday": "1997-04-01"}
  6. {"index":{"_index":"twitter","_id":3}}
  7. {"user":"李四","message":"happy birthday!","uid":4,"age":25,"city":"北京","province":"北京","country":"中国","address":"中国北京市东城区","location":{"lat":"39.893801","lon":"116.408986"}, "birthday": "1994-04-01"}
  8. {"index":{"_index":"twitter","_id":4}}
  9. {"user":"老贾","message":"123,gogogo","uid":5,"age":30,"city":"北京","province":"北京","country":"中国","address":"中国北京市朝阳区建国门","location":{"lat":"39.718256","lon":"116.367910"}, "birthday": "1989-04-01"}
  10. {"index":{"_index":"twitter","_id":5}}
  11. {"user":"老王","message":"Happy BirthDay My Friend!","uid":6,"age":26,"city":"北京","province":"北京","country":"中国","address":"中国北京市朝阳区国贸","location":{"lat":"39.918256","lon":"116.467910"}, "birthday": "1993-04-01"}
  12. {"index":{"_index":"twitter","_id":6}}
  13. {"user":"老吴","message":"好友来了都今天我生日,好友来了,什么 birthday happy 就成!","uid":7,"age":28,"city":"上海","province":"上海","country":"中国","address":"中国上海市闵行区","location":{"lat":"31.175927","lon":"121.383328"}, "birthday": "1991-04-01"}

注意:并不是所有字段都可以用来做聚合,一般来说,只有具有 keyword或者数值类型的字段是可以用来做聚合。

我们可以通过 _field_cat 命令还查询文档中的字段是否可以作为聚合:

  1. GET twitter/_field_caps?fields=message,age,province,city.keyword

从结果我们可以看到四个字段都可以用来做搜索的,但是只有 agecity.keyword才可以用来做聚合

  1. {
  2. "indices" : [
  3. "twitter"
  4. ],
  5. "fields" : {
  6. "province" : {
  7. "text" : {
  8. "type" : "text",
  9. "searchable" : true,
  10. "aggregatable" : false
  11. }
  12. },
  13. "message" : {
  14. "text" : {
  15. "type" : "text",
  16. "searchable" : true,
  17. "aggregatable" : false
  18. }
  19. },
  20. "city.keyword" : {
  21. "keyword" : {
  22. "type" : "keyword",
  23. "searchable" : true,
  24. "aggregatable" : true
  25. }
  26. },
  27. "age" : {
  28. "long" : {
  29. "type" : "long",
  30. "searchable" : true,
  31. "aggregatable" : true
  32. }
  33. }
  34. }
  35. }

searchable
是否为所有索引上的搜索都索引了该字段。

aggregatable
是否可以在所有索引上汇总此字段。

non_searchable_indices
该字段不可搜索的索引列表;如果所有索引对该字段的定义相同,则为null。

non_aggregatable_indices
该字段不可聚合的索引列表;如果所有索引对该字段的定义相同,则为null。

聚合操作 语法

  1. "aggregations" : {
  2. "<aggregation_name>" : { <!--聚合的名字 -->
  3. "<aggregation_type>" : { <!--聚合的类型 -->
  4. <aggregation_body> <!--聚合体:对哪些字段进行聚合 -->
  5. }
  6. [,"meta" : { [<meta_data_body>] } ]? <!--元 -->
  7. [,"aggregations" : { [<sub_aggregation>]+ } ]? <!--在聚合里面在定义子聚合 -->
  8. }
  9. [,"<aggregation_name_2>" : { ... } ]*<!--聚合的名字 -->
  10. }

上面的 aggregation 可以使用 aggs 来代替

Metric 聚合操作

Avg Sum Max Min 聚合

Avg Aggregation : 一个单值度量聚合,计算从聚合文档中提取的数值的平均值。

Sum Aggregation :sum聚合对从聚合文档中提取的数值进行汇总的单值度量。

Max Aggregation :一个单值度量聚合,用于跟踪并返回从聚合文档中提取的数值中的最大值。

Min Aggregation :一个单值度量聚合,用于跟踪并返回从聚合文档中提取的数值中的最小值。

这些值可以从文档中的特定数字字段中提取,也可以由提供的脚本生成。

查询 twitter 索引下文档 age 的 平均值、总和、最大值及最小值:

  1. GET twitter/_search?size=0
  2. {
  3. "aggs": {
  4. "age_avg": {
  5. "avg": {
  6. "field": "age"
  7. }
  8. },
  9. "age_sum":{
  10. "sum": {
  11. "field": "age"
  12. }
  13. },
  14. "age_max":{
  15. "max": {
  16. "field": "age"
  17. }
  18. },
  19. "age_min":{
  20. "min": {
  21. "field": "age"
  22. }
  23. }
  24. }
  25. }

返回结果:

  1. {
  2. "took" : 1,
  3. "timed_out" : false,
  4. "_shards" : {
  5. "total" : 1,
  6. "successful" : 1,
  7. "skipped" : 0,
  8. "failed" : 0
  9. },
  10. "hits" : {
  11. "total" : {
  12. "value" : 6,
  13. "relation" : "eq"
  14. },
  15. "max_score" : null,
  16. "hits" : [ ]
  17. },
  18. "aggregations" : {
  19. "age_sum" : {
  20. "value" : 151.0
  21. },
  22. "age_min" : {
  23. "value" : 20.0
  24. },
  25. "age_avg" : {
  26. "value" : 25.166666666666668
  27. },
  28. "age_max" : {
  29. "value" : 30.0
  30. }
  31. }
  32. }

Stats 聚合

数据聚合一个多值指标聚合,它根据从聚合文档中提取的数值计算统计信息。

返回的统计数据包括:最小值,最大值,和;

汇总所有文档的年龄统计

  1. GET twitter/_search?size=0
  2. {
  3. "query": {
  4. "match": {
  5. "city": "北京"
  6. }
  7. },
  8. "aggs": {
  9. "age_stats": {
  10. "stats": {
  11. "field": "age"
  12. }
  13. }
  14. }
  15. }

返回结果

  1. {
  2. "took" : 0,
  3. "timed_out" : false,
  4. "_shards" : {
  5. "total" : 1,
  6. "successful" : 1,
  7. "skipped" : 0,
  8. "failed" : 0
  9. },
  10. "hits" : {
  11. "total" : {
  12. "value" : 5,
  13. "relation" : "eq"
  14. },
  15. "max_score" : null,
  16. "hits" : [ ]
  17. },
  18. "aggregations" : {
  19. "age_stats" : {
  20. "count" : 5,
  21. "min" : 20.0,
  22. "max" : 30.0,
  23. "avg" : 24.6,
  24. "sum" : 123.0
  25. }
  26. }
  27. }

Bucket 聚合操作

Range 聚合(multi-bucket)

基于多桶值源的聚合,可以定义一组范围(每个范围代表一个桶)。在聚合过程中,将从每个存储区范围中检查并从文档中提取值

注意:此聚合包含每个范围的 from 值,但不包括 to 值。

将年龄进行分段,查询不同年龄段的用户:

  1. GET twitter/_search
  2. {
  3. "size": 0,
  4. "aggs": {
  5. "age_range": {
  6. "range": {
  7. "field": "age",
  8. "ranges": [
  9. {
  10. "from": 20,
  11. "to": 22
  12. },
  13. {
  14. "from": 22,
  15. "to": 25
  16. },
  17. {
  18. "from": 25,
  19. "to": 30
  20. }
  21. ]
  22. }
  23. }
  24. }
  25. }

上面我们使用 range 类型的聚合,定义了不同的年龄段。通过上面的查询,得到了不同年龄段的 bucket。并且因为是针对聚合,我们并不关心返回的结果,通过 size=0 忽略了返回结果。得到了以下输出:

  1. {
  2. "took" : 2,
  3. "timed_out" : false,
  4. "_shards" : {
  5. "total" : 1,
  6. "successful" : 1,
  7. "skipped" : 0,
  8. "failed" : 0
  9. },
  10. "hits" : {
  11. "total" : {
  12. "value" : 6,
  13. "relation" : "eq"
  14. },
  15. "max_score" : null,
  16. "hits" : [ ]
  17. },
  18. "aggregations" : {
  19. "age_range" : {
  20. "buckets" : [
  21. {
  22. "key" : "20.0-22.0",
  23. "from" : 20.0,
  24. "to" : 22.0,
  25. "doc_count" : 1
  26. },
  27. {
  28. "key" : "22.0-25.0",
  29. "from" : 22.0,
  30. "to" : 25.0,
  31. "doc_count" : 1
  32. },
  33. {
  34. "key" : "25.0-30.0",
  35. "from" : 25.0,
  36. "to" : 30.0,
  37. "doc_count" : 3
  38. }
  39. ]
  40. }
  41. }
  42. }

Sub-aggregation

在聚合的内部嵌套一个聚合。

在 range 操作之中,我们可以做 sub-aggregation。分别来计算它们的平均年龄、最大以及最小的年龄!

  1. GET twitter/_search
  2. {
  3. "size": 0,
  4. "aggs": {
  5. "age_range": {
  6. "range": {
  7. "field": "age",
  8. "ranges": [
  9. {
  10. "from": 20,
  11. "to": 22
  12. },
  13. {
  14. "from": 22,
  15. "to": 25
  16. },
  17. {
  18. "from": 25,
  19. "to": 30
  20. }
  21. ]
  22. },
  23. "aggs": {
  24. "age_avg": {
  25. "avg": {
  26. "field": "age"
  27. }
  28. },
  29. "age_min":{
  30. "min": {
  31. "field": "age"
  32. }
  33. },
  34. "age_max":{
  35. "max": {
  36. "field": "age"
  37. }
  38. }
  39. }
  40. }
  41. }
  42. }

上面的查询结果为:

  1. {
  2. "took" : 1,
  3. "timed_out" : false,
  4. "_shards" : {
  5. "total" : 1,
  6. "successful" : 1,
  7. "skipped" : 0,
  8. "failed" : 0
  9. },
  10. "hits" : {
  11. "total" : {
  12. "value" : 6,
  13. "relation" : "eq"
  14. },
  15. "max_score" : null,
  16. "hits" : [ ]
  17. },
  18. "aggregations" : {
  19. "age_range" : {
  20. "buckets" : [
  21. {
  22. "key" : "20.0-22.0",
  23. "from" : 20.0,
  24. "to" : 22.0,
  25. "doc_count" : 1,
  26. "age_min" : {
  27. "value" : 20.0
  28. },
  29. "age_avg" : {
  30. "value" : 20.0
  31. },
  32. "age_max" : {
  33. "value" : 20.0
  34. }
  35. },
  36. {
  37. "key" : "22.0-25.0",
  38. "from" : 22.0,
  39. "to" : 25.0,
  40. "doc_count" : 1,
  41. "age_min" : {
  42. "value" : 22.0
  43. },
  44. "age_avg" : {
  45. "value" : 22.0
  46. },
  47. "age_max" : {
  48. "value" : 22.0
  49. }
  50. },
  51. {
  52. "key" : "25.0-30.0",
  53. "from" : 25.0,
  54. "to" : 30.0,
  55. "doc_count" : 3,
  56. "age_min" : {
  57. "value" : 25.0
  58. },
  59. "age_avg" : {
  60. "value" : 26.333333333333332
  61. },
  62. "age_max" : {
  63. "value" : 28.0
  64. }
  65. }
  66. ]
  67. }
  68. }
  69. }

Filters 聚合 (multi-bucket)

使用 Filter 聚合定义一个多存储桶聚合,每个存储桶都与一个过滤器相关。每个存储桶将收集与其关联的过滤器相匹配的所有文档。

在上面我们使用 Range 将数据拆分成了不同的 Bucket,但是这种方式只适合字段为数字的字段。我们可以使用 Filter 聚合来对非数字字段来建立不同的 Bucket。

  1. GET twitter/_search
  2. {
  3. "size": 0,
  4. "aggs": {
  5. "city_filters": {
  6. "filters": {
  7. "filters": {
  8. "beijing": {
  9. "match":{
  10. "city":"北京"
  11. }
  12. },
  13. "shanghai":{
  14. "match":{
  15. "city":"上海"
  16. }
  17. }
  18. }
  19. }
  20. }
  21. }
  22. }

上面的查询结果显示有5个北京的文档,一个上海的文档。并且每个filter都有自己的名字:

  1. {
  2. "took" : 1,
  3. "timed_out" : false,
  4. "_shards" : {
  5. "total" : 1,
  6. "successful" : 1,
  7. "skipped" : 0,
  8. "failed" : 0
  9. },
  10. "hits" : {
  11. "total" : {
  12. "value" : 6,
  13. "relation" : "eq"
  14. },
  15. "max_score" : null,
  16. "hits" : [ ]
  17. },
  18. "aggregations" : {
  19. "city_filter" : {
  20. "buckets" : {
  21. "beijing" : {
  22. "doc_count" : 5
  23. },
  24. "shanghai" : {
  25. "doc_count" : 1
  26. }
  27. }
  28. }
  29. }
  30. }

Filter 聚合 (single-bucket)

在当前文档上下文中定义与指定过滤器匹配的所有文档的单个存储桶。通常将用于将当前聚合上下文缩小到一组特定的文档。

查询城市为 北京 的文档,并求平均年龄、最大以及最小年龄:

  1. GET twitter/_search
  2. {
  3. "size":0,
  4. "aggs": {
  5. "agg_filter": {
  6. "filter": {
  7. "match":{
  8. "city":"北京"
  9. }
  10. },
  11. "aggs": {
  12. "age_avg": {
  13. "avg": {
  14. "field": "age"
  15. }
  16. },
  17. "avg_max":{
  18. "max": {
  19. "field": "age"
  20. }
  21. },
  22. "avg_min":{
  23. "min": {
  24. "field": "age"
  25. }
  26. }
  27. }
  28. }
  29. }
  30. }

查询结果为:

  1. {
  2. "took" : 1,
  3. "timed_out" : false,
  4. "_shards" : {
  5. "total" : 1,
  6. "successful" : 1,
  7. "skipped" : 0,
  8. "failed" : 0
  9. },
  10. "hits" : {
  11. "total" : {
  12. "value" : 6,
  13. "relation" : "eq"
  14. },
  15. "max_score" : null,
  16. "hits" : [ ]
  17. },
  18. "aggregations" : {
  19. "agg_filter" : {
  20. "doc_count" : 5,
  21. "avg_min" : {
  22. "value" : 20.0
  23. },
  24. "avg_max" : {
  25. "value" : 30.0
  26. },
  27. "age_avg" : {
  28. "value" : 24.6
  29. }
  30. }
  31. }
  32. }

Date Range 聚合 (multi-bucket)

专用于日期值的范围聚合。此聚合与正常范围聚合之间的主要区别是,from和to值可以用Date Math表达式表示,而且还可以指定返回from和to响应字段的日期格式。

注意:对于每个范围,此聚合包括from值,排除to值。

根据生日范围查询文档:

  1. GET twitter/_search
  2. {
  3. "size": 0,
  4. "aggs": {
  5. "birthday_range": {
  6. "date_range": {
  7. "field": "birthday",
  8. "format": "yyyy-MM-dd",
  9. "ranges": [
  10. {
  11. "from": "1989-04-01",
  12. "to": "1997-04-01"
  13. },
  14. {
  15. "from": "1994-04-01",
  16. "to": "1999-04-01"
  17. }
  18. ]
  19. }
  20. }
  21. }
  22. }

查询结果:

  1. {
  2. "took" : 0,
  3. "timed_out" : false,
  4. "_shards" : {
  5. "total" : 1,
  6. "successful" : 1,
  7. "skipped" : 0,
  8. "failed" : 0
  9. },
  10. "hits" : {
  11. "total" : {
  12. "value" : 6,
  13. "relation" : "eq"
  14. },
  15. "max_score" : null,
  16. "hits" : [ ]
  17. },
  18. "aggregations" : {
  19. "birthday_range" : {
  20. "buckets" : [
  21. {
  22. "key" : "1989-04-01-1997-04-01",
  23. "from" : 6.07392E11,
  24. "from_as_string" : "1989-04-01",
  25. "to" : 8.598528E11,
  26. "to_as_string" : "1997-04-01",
  27. "doc_count" : 4
  28. },
  29. {
  30. "key" : "1994-04-01-1999-04-01",
  31. "from" : 7.651584E11,
  32. "from_as_string" : "1994-04-01",
  33. "to" : 9.229248E11,
  34. "to_as_string" : "1999-04-01",
  35. "doc_count" : 2
  36. }
  37. ]
  38. }
  39. }
  40. }

Terms 聚合 (multi-bucket)

基于多桶值源的聚合,其中动态构建桶-每个唯一值一个。

可以根据 terms 聚合查询关键字出现的频率。下面我们查询在所有文档中出现 happy birthday 关键字并按照城市进行分类:

  1. GET twitter/_search
  2. {
  3. "query": {
  4. "match": {
  5. "message": "happy birthday"
  6. }
  7. },
  8. "size": 0,
  9. "aggs": {
  10. "city_terms": {
  11. "terms": {
  12. "field": "city.keyword",
  13. "size": 10,
  14. "order": {
  15. "_count": "asc"
  16. }
  17. }
  18. }
  19. }
  20. }

size=10 指的是排名前十的城市。并通过 doc_count 进行排序。聚合的结果为:

  1. {
  2. "took" : 1,
  3. "timed_out" : false,
  4. "_shards" : {
  5. "total" : 1,
  6. "successful" : 1,
  7. "skipped" : 0,
  8. "failed" : 0
  9. },
  10. "hits" : {
  11. "total" : {
  12. "value" : 3,
  13. "relation" : "eq"
  14. },
  15. "max_score" : null,
  16. "hits" : [ ]
  17. },
  18. "aggregations" : {
  19. "city_terms" : {
  20. "doc_count_error_upper_bound" : 0,
  21. "sum_other_doc_count" : 0,
  22. "buckets" : [
  23. {
  24. "key" : "上海",
  25. "doc_count" : 1
  26. },
  27. {
  28. "key" : "北京",
  29. "doc_count" : 2
  30. }
  31. ]
  32. }
  33. }
  34. }

histogram 聚合

基于多桶值源的汇总,可以应用于从文档中提取数值或数值范围值。根据值动态构建固定大小(也称为间隔)的存储桶。

  1. GET twitter/_search
  2. {
  3. "size": 0,
  4. "aggs": {
  5. "age_histogram": {
  6. "histogram": {
  7. "field": "age",
  8. "interval": 2
  9. }
  10. }
  11. }
  12. }
  • interval : 间隔为2

返回结果:

  1. {
  2. "took" : 3,
  3. "timed_out" : false,
  4. "_shards" : {
  5. "total" : 1,
  6. "successful" : 1,
  7. "skipped" : 0,
  8. "failed" : 0
  9. },
  10. "hits" : {
  11. "total" : {
  12. "value" : 6,
  13. "relation" : "eq"
  14. },
  15. "max_score" : null,
  16. "hits" : [ ]
  17. },
  18. "aggregations" : {
  19. "age_histogram" : {
  20. "buckets" : [
  21. {
  22. "key" : 20.0,
  23. "doc_count" : 1
  24. },
  25. {
  26. "key" : 22.0,
  27. "doc_count" : 1
  28. },
  29. {
  30. "key" : 24.0,
  31. "doc_count" : 1
  32. },
  33. {
  34. "key" : 26.0,
  35. "doc_count" : 1
  36. },
  37. {
  38. "key" : 28.0,
  39. "doc_count" : 1
  40. },
  41. {
  42. "key" : 30.0,
  43. "doc_count" : 1
  44. }
  45. ]
  46. }
  47. }
  48. }

原文链接:http://www.cnblogs.com/leizzige/p/14822038.html

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号