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函数,即可解决
相关推荐
武子康1 天前
大数据-184 Elasticsearch Doc Values 机制详解:列式存储如何支撑排序/聚合/脚本
大数据·后端·elasticsearch
expect7g1 天前
Paimon源码解读 -- Compaction-8.专用压缩任务
大数据·后端·flink
良策金宝AI1 天前
从CAD插件到原生平台:工程AI的演进路径与智能协同新范式
大数据·人工智能
康实训1 天前
智慧老年实训室建设核心方案
大数据·实训室·养老实训室·实训室建设
min1811234561 天前
分公司组织架构图在线设计 总部分支管理模板
大数据·人工智能·信息可视化·架构·流程图
周杰伦_Jay1 天前
【Elasticsearch】核心概念,倒排索引,数据操纵
大数据·elasticsearch·搜索引擎
cai_cai01 天前
springAlibaba + ollama + es 完成RAG知识库功能
大数据·elasticsearch·搜索引擎
老陈头聊SEO1 天前
长尾关键词对SEO的重要性及其优化策略总结
其他·搜索引擎·seo优化
Cx330❀1 天前
Git 分支管理完全指南:从基础到团队协作
大数据·git·搜索引擎·全文检索
库库茯苓1 天前
Kibana报错:Unable to retrieve version information from Elasticsearch nodes (解决方法)Window11环境
elasticsearch·kibana