ElasticSearch 管道聚合

2021-01-13 10:55:56 浏览数 (1)

松哥原创的 Spring Boot 视频教程已经杀青,感兴趣的小伙伴戳这里-->Spring Boot Vue 微人事视频教程


假期最后一天,明天又要开始搬砖了,不过春节是越来越近啦~

元旦三天假期似乎天气都还不错,很适合出去走走~

今天松哥和大家分享一个 Es 中的简单话题--管道聚合。

以下是视频笔记:

注意,笔记只是视频内容的一个简要记录,因此笔记内容比较简单,完整的内容可以查看视频。

24.ElasticSearch 管道聚合

管道聚合相当于在之前聚合的基础上,再次聚合。

24.1 Avg Bucket Aggregation

计算聚合平均值。例如,统计每个出版社所出版图书的平均值,然后再统计所有出版社的平均值:

代码语言:javascript复制
GET books/_search
{
  "aggs": {
    "book_count": {
      "terms": {
        "field": "publish",
        "size": 3
      },
      "aggs": {
        "book_avg": {
          "avg": {
            "field": "price"
          }
        }
      }
    },
    "avg_book":{
      "avg_bucket": {
        "buckets_path": "book_count>book_avg"
      }
    }
  }
}

24.2 Max Bucket Aggregation

统计每个出版社所出版图书的平均值,然后再统计平均值中的最大值:

代码语言:javascript复制
GET books/_search
{
  "aggs": {
    "book_count": {
      "terms": {
        "field": "publish",
        "size": 3
      },
      "aggs": {
        "book_avg": {
          "avg": {
            "field": "price"
          }
        }
      }
    },
    "avg_book":{
      "max_bucket": {
        "buckets_path": "book_count>book_avg"
      }
    }
  }
}

24.3 Min Bucket Aggregation

统计每个出版社所出版图书的平均值,然后再统计平均值中的最小值:

代码语言:javascript复制
GET books/_search
{
  "aggs": {
    "book_count": {
      "terms": {
        "field": "publish",
        "size": 3
      },
      "aggs": {
        "book_avg": {
          "avg": {
            "field": "price"
          }
        }
      }
    },
    "avg_book":{
      "min_bucket": {
        "buckets_path": "book_count>book_avg"
      }
    }
  }
}

24.4 Sum Bucket Aggregation

统计每个出版社所出版图书的平均值,然后再统计平均值之和:

代码语言:javascript复制
GET books/_search
{
  "aggs": {
    "book_count": {
      "terms": {
        "field": "publish",
        "size": 3
      },
      "aggs": {
        "book_avg": {
          "avg": {
            "field": "price"
          }
        }
      }
    },
    "avg_book":{
      "sum_bucket": {
        "buckets_path": "book_count>book_avg"
      }
    }
  }
}

24.5 Stats Bucket Aggregation

统计每个出版社所出版图书的平均值,然后再统计平均值的各种数据:

代码语言:javascript复制
GET books/_search
{
  "aggs": {
    "book_count": {
      "terms": {
        "field": "publish",
        "size": 3
      },
      "aggs": {
        "book_avg": {
          "avg": {
            "field": "price"
          }
        }
      }
    },
    "avg_book":{
      "stats_bucket": {
        "buckets_path": "book_count>book_avg"
      }
    }
  }
}

24.6 Extended Stats Bucket Aggregation

代码语言:javascript复制
GET books/_search
{
  "aggs": {
    "book_count": {
      "terms": {
        "field": "publish",
        "size": 3
      },
      "aggs": {
        "book_avg": {
          "avg": {
            "field": "price"
          }
        }
      }
    },
    "avg_book":{
      "extended_stats_bucket": {
        "buckets_path": "book_count>book_avg"
      }
    }
  }
}

24.7 Percentiles Bucket Aggregation

代码语言:javascript复制
GET books/_search
{
  "aggs": {
    "book_count": {
      "terms": {
        "field": "publish",
        "size": 3
      },
      "aggs": {
        "book_avg": {
          "avg": {
            "field": "price"
          }
        }
      }
    },
    "avg_book":{
      "percentiles_bucket": {
        "buckets_path": "book_count>book_avg"
      }
    }
  }
}

0 人点赞