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函数,即可解决
相关推荐
汇智信科3 小时前
智慧矿山和工业大数据解决方案“智能设备管理系统”
大数据·人工智能·工业大数据·智能矿山·汇智信科·智能设备管理系统
SEO_juper4 小时前
AI+SEO全景决策指南:10大高价值方法、核心挑战与成本效益分析
人工智能·搜索引擎·seo·数字营销
阿里云大数据AI技术4 小时前
Hologres Dynamic Table 在淘天价格力的业务实践
大数据·人工智能·阿里云·hologres·增量刷新
OpenCSG7 小时前
新能源汽车行业经典案例 — 某新能源汽车 × OpenCSG
大数据·人工智能·汽车·客户案例·opencsg
外参财观7 小时前
流量变现的边界:携程金融按下暂停键后的冷思考
大数据·人工智能·金融
CCPC不拿奖不改名8 小时前
两种完整的 Git 分支协作流程
大数据·人工智能·git·python·elasticsearch·搜索引擎·自然语言处理
a努力。8 小时前
字节Java面试被问:TCP的BBR拥塞控制算法原理
java·开发语言·python·tcp/ip·elasticsearch·面试·职场和发展
智在碧得8 小时前
碧服打造DataOps全链路闭环,定义大数据工程化发布新标杆
大数据·网络·数据库
亿信华辰软件8 小时前
构建智慧数据中台,赋能饮料集团全链路数字化转型新引擎
大数据·人工智能·云计算
Elastic 中国社区官方博客9 小时前
使用瑞士风格哈希表实现更快的 ES|QL 统计
大数据·数据结构·sql·elasticsearch·搜索引擎·全文检索·散列表