且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

我应该使用哪种Elasticsearch聚合?

更新时间:2023-08-24 14:16:46

您可以使用日期直方图聚合.

You can filter the users with last activity for last 7 days using date math operation of elasticsearch. You can push the filter before the date histogram aggregation.

POST active_users/document_type1/_search
{
  "size": 0, 
  "aggs": {
    "filtered_active_users_7_days": {
      "filter": {
        "range": {
          "last_activity": {
            "gte": "now-7d/d"
          }
        }
      },
      "aggs": {
        "date_histogram_last_7_days": {
          "date_histogram": {
            "field": "last_activity",
            "interval": "day"
          }
        }
      }
    }
  }
}

希望这对您有用.