16、ES实战:管道聚合

本学习笔记基于ElasticSearch 7.10版本,旧版本已经废弃的聚合功能暂时不做笔记,以后有涉及到再做补充。
参考官方文档:https://www.elastic.co/guide/en/elasticsearch/reference/7.10/search-aggregations-pipeline.html

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

1、Avg Bucket

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

GET books/_search
{
   
     
  "aggs": {
   
     
    "book_count": {
   
     
      "terms": {
   
     
        "field": "publish.size",
        "size": 3
      },
      "aggs": {
   
     
        "book_avg": {
   
     
          "avg": {
   
     
            "field": "price"
          }
        }
      }
    },
    "avg_book":{
   
     
      "avg_bucket": {
   
     
        "buckets_path": "book_count>book_avg"
      }
    }
  }
}

2、Max Bucket

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

GET books/_search
{
   
     
  "aggs": {
   
     
    "book_count": {
   
     
      "terms": {
   
     
        "field": "publish.size",
        "size": 3
      },
      "aggs": {
   
     
        "book_avg": {
   
     
          "avg": {
   
     
            "field": "price"
          }
        }
      }
    },
    "max_book":{
   
     
      "max_bucket": {
   
     
        "buckets_path": "book_count>book_avg"
      }
    }
  }
}

3、Min Bucket

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

GET books/_search
{
   
     
  "aggs": {
   
     
    "book_count": {
   
     
      "terms": {
   
     
        "field": "publish.size",
        "size": 3
      },
      "aggs": {
   
     
        "book_avg": {
   
     
          "avg": {
   
     
            "field": "price"
          }
        }
      }
    },
    "min_book":{
   
     
      "min_bucket": {
   
     
        "buckets_path": "book_count>book_avg"
      }
    }
  }
}

4、Sum Bucket

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

GET books/_search
{
   
     
  "aggs": {
   
     
    "book_count": {
   
     
      "terms": {
   
     
        "field": "publish.size",
        "size": 3
      },
      "aggs": {
   
     
        "book_avg": {
   
     
          "avg": {
   
     
            "field": "price"
          }
        }
      }
    },
    "sum_book":{
   
     
      "sum_bucket": {
   
     
        "buckets_path": "book_count>book_avg"
      }
    }
  }
}

5、Stats Bucket

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

GET books/_search
{
   
     
  "aggs": {
   
     
    "book_count": {
   
     
      "terms": {
   
     
        "field": "publish.size",
        "size": 3
      },
      "aggs": {
   
     
        "book_avg": {
   
     
          "avg": {
   
     
            "field": "price"
          }
        }
      }
    },
    "stats_book":{
   
     
      "stats_bucket": {
   
     
        "buckets_path": "book_count>book_avg"
      }
    }
  }
}

6、Extended Stats Bucket

统计每个出版社所出版图书的平均值,然后再统计平均值的各种数据,比 Stats Bucket 统计多了一些方差之类的数据:

GET books/_search
{
   
     
  "aggs": {
   
     
    "book_count": {
   
     
      "terms": {
   
     
        "field": "publish.size",
        "size": 3
      },
      "aggs": {
   
     
        "book_avg": {
   
     
          "avg": {
   
     
            "field": "price"
          }
        }
      }
    },
    "extended_book":{
   
     
      "extended_stats_bucket": {
   
     
        "buckets_path": "book_count>book_avg"
      }
    }
  }
}

7、Percentiles Bucket

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

GET books/_search
{
   
     
  "aggs": {
   
     
    "book_count": {
   
     
      "terms": {
   
     
        "field": "publish.size",
        "size": 3
      },
      "aggs": {
   
     
        "book_avg": {
   
     
          "avg": {
   
     
            "field": "price"
          }
        }
      }
    },
    "percentiles_book":{
   
     
      "percentiles_bucket": {
   
     
        "buckets_path": "book_count>book_avg"
      }
    }
  }
}

版权声明:

本文仅记录ElasticSearch学习心得,如有侵权请联系删除。
更多内容请访问原创作者:江南一点雨
*

版权声明:本文不是「本站」原创文章,版权归原作者所有 | 原文地址: