白话Elasticsearch42-深入聚合数据分析之案例实战__bucket filter:统计牌品最近一个月的平均价格(Filter Aggregation)

2021-08-17 15:00:31 浏览数 (3)


概述

继续跟中华石杉老师学习ES,第41篇

课程地址: https://www.roncoo.com/view/55


官方说明

Filter Aggregation:戳这里


案例

需求: 统计牌品最近一个月的平均价格

今天是 2019-08-24 ,为了演示该案例,我们批量更新下数据

代码语言:javascript复制
POST /tvs/sales/_bulk
{"update":{"_id":"3n0msmwBYSg4rD-7WPab"}}
{"doc":{"sold_date":"2019-07-30"}}
{"update":{"_id":"4H0msmwBYSg4rD-7WPab"}}
{"doc":{"sold_date":"2019-01-05"}}
{"update":{"_id":"4n0msmwBYSg4rD-7WPab"}}
{"doc":{"sold_date":"2019-04-12"}}
{"update":{"_id":"230msmwBYSg4rD-7WPaa"}}
{"doc":{"sold_date":"2019-03-28"}}
{"update":{"_id":"3X0msmwBYSg4rD-7WPab"}}
{"doc":{"sold_date":"2019-05-20"}}
{"update":{"_id":"3H0msmwBYSg4rD-7WPab"}}
{"doc":{"sold_date":"2019-06-05"}}
{"update":{"_id":"330msmwBYSg4rD-7WPab"}}
{"doc":{"sold_date":"2019-08-02"}}
{"update":{"_id":"4X0msmwBYSg4rD-7WPab"}}
{"doc":{"sold_date":"2019-07-30"}}

更新后的数据:

DSL:

代码语言:javascript复制
#统计牌品最近一个月的平均价格
GET /tvs/sales/_search 
{
  "query": {
    "term": {
      "brand": "TCL"
    }
  },
  "aggs": {
    "recent_30d": {
      "filter": {
        "range": {
          "sold_date": {
            "gte": "now-30d"
          }
        }
      },
      "aggs": {
        "recent_30d_price": {
          "avg": {
            "field": "price"
          }
        }
      }
    }
  }, 
  "size": 0
}

返回

分析一下:

aggs.filter,针对的是聚合。

为什么不把filter放在query里呢? 如果放query里面的filter,是全局的,会对所有的数据都有影响 。

但是假设你要统计TCL电视,最近1个月的平均值; 最近3个月的平均值; 最近6个月的平均值

那就需要对bucket 进行 filter:对不同的bucket下的aggs,进行filter

代码语言:javascript复制
#统计牌品最近一个月的平均价格
GET /tvs/sales/_search 
{
  "query": {
    "term": {
      "brand": "TCL"
    }
  },
  "aggs": {
    "recent_30d": {
      "filter": {
        "range": {
          "sold_date": {
            "gte": "now-30d"
          }
        }
      },
      "aggs": {
        "recent_30d_price": {
          "avg": {
            "field": "price"
          }
        }
      }
    },
    "recent_90d":{
      "filter": {
        "range": {
          "sold_date": {
            "gte": "now-90d"
          }
        }
      },
      "aggs": {
        "recent_90d_price": {
          "avg": {
            "field": "price"
          }
        }
      }
    },
    "recent_180d":{
      "filter": {
        "range": {
          "sold_date": {
            "gte": "now-180d"
          }
        }
      },
      "aggs": {
        "recent_180d_price": {
          "avg": {
            "field": "price"
          }
        }
      }
    }
  }
}

返回:

代码语言:javascript复制
{
  "took": 12,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 2,
    "max_score": 0.9808292,
    "hits": [
      {
        "_index": "tvs",
        "_type": "sales",
        "_id": "330msmwBYSg4rD-7WPab",
        "_score": 0.9808292,
        "_source": {
          "price": 1200,
          "color": "绿色",
          "brand": "TCL",
          "sold_date": "2019-08-02"
        }
      },
      {
        "_index": "tvs",
        "_type": "sales",
        "_id": "3n0msmwBYSg4rD-7WPab",
        "_score": 0.6931472,
        "_source": {
          "price": 1500,
          "color": "蓝色",
          "brand": "TCL",
          "sold_date": "2019-07-30"
        }
      }
    ]
  },
  "aggregations": {
    "recent_30d": {
      "doc_count": 2,
      "recent_30d_price": {
        "value": 1350
      }
    },
    "recent_90d": {
      "doc_count": 2,
      "recent_90d_price": {
        "value": 1350
      }
    },
    "recent_180d": {
      "doc_count": 2,
      "recent_180d_price": {
        "value": 1350
      }
    }
  }
}

0 人点赞