ElasticSearch:使用dsl语句同时查询出最近2小时、最近1天、最近7天、最近30天的数量

场景

需要使用dsl语句同时查询出最近2小时、最近1天、最近7天、最近30天的数量,如果按照常规逻辑,需要写四个dsl语句,才能查询出来,那么能不能使用一句dsl就可以查询出结果呢?

show time

xml 复制代码
GET alarm_forward_history_*/_search
{
  "size": 0,
  "query": {
    "bool": {
      "must": [
        {
          "term": {
            "sourceName": {
              "value": "运维123"
            }
          }
        },
        {
          "term": {
            "sourceAlertKey": {
              "value": "生产问题描述223"
            }
          }
        },
        {
          "term": {
            "cmdb.app_name": {
              "value": "dba数据库"
            }
          }
        }
      ]
    }
  },
  "aggs": {
    "date": {
      "date_range": {
        "field": "@timestamp",
        "ranges": [
          {
            "from": "now-2h",
            "to": "now",
            "key": "lastTwoHour"
          },
          {
            "from": "now-1d",
            "to": "now",
            "key": "lastDay"
          },
          {
            "from": "now-7d",
            "to": "now",
            "key": "lastSevenDay"
          },
          {
            "from": "now-30d",
            "to": "now",
            "key": "lastMonth"
          }
        ]
      }
    }
  }
}

输出结果大致如下:

xml 复制代码
"aggregations" : {
    "date" : {
      "buckets" : [
        {
          "key" : "lastMonth",
          "from" : 1.728359639296E12,
          "from_as_string" : "2024-10-08T03:53:59.296Z",
          "to" : 1.730951639296E12,
          "to_as_string" : "2024-11-07T03:53:59.296Z",
          "doc_count" : 7
        },
        {
          "key" : "lastSevenDay",
          "from" : 1.730346839296E12,
          "from_as_string" : "2024-10-31T03:53:59.296Z",
          "to" : 1.730951639296E12,
          "to_as_string" : "2024-11-07T03:53:59.296Z",
          "doc_count" : 0
        },
        {
          "key" : "lastDay",
          "from" : 1.730865239296E12,
          "from_as_string" : "2024-11-06T03:53:59.296Z",
          "to" : 1.730951639296E12,
          "to_as_string" : "2024-11-07T03:53:59.296Z",
          "doc_count" : 0
        },
        {
          "key" : "lastTwoHour",
          "from" : 1.730944439296E12,
          "from_as_string" : "2024-11-07T01:53:59.296Z",
          "to" : 1.730951639296E12,
          "to_as_string" : "2024-11-07T03:53:59.296Z",
          "doc_count" : 0
        }
      ]
    }
  }
}

总结

  • 直接采用date_range函数,即可解决
相关推荐
zhojiew1 天前
AWS云上使用Redshift Test Drive进行负载重放测试的实践
大数据·redshift
移动云开发者联盟1 天前
存智赋能 共筑AI存储新生态,移动云聚力技术创新夯实AI数据基石
大数据·人工智能
TDengine (老段)1 天前
TDengine 数据库创建与参数详解
大数据·数据库·物联网·时序数据库·iot·tdengine·涛思数据
面向Google编程1 天前
从零学习Kafka:调优
大数据·kafka
不是株1 天前
ElasticSearch
大数据·elasticsearch·搜索引擎
随身数智备忘录1 天前
拆解合理化建议系统的三大流程,合理化建议如何解决建议征集难与落地慢
大数据·人工智能
逸Y 仙X1 天前
文章三十四:ElasticSearch Script脚本实战
大数据·elasticsearch·搜索引擎·全文检索
倔强的石头1061 天前
【Linux 指南】文件系统系列(三):Ext系统核心实现 —— 从块组到 inode 与数据块映射全解析
大数据·linux·运维
前端若水1 天前
处理智能体的不确定性:重试、回退与人工介入
大数据·人工智能·windows·开源协议
小船跨境1 天前
2026 NLP数据采集指南:代理IP如何帮助提升大规模采集效率
大数据·网络·人工智能