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函数,即可解决
相关推荐
wheelerer1 天前
企业营销的隐形门槛:当规则成为第一道城墙
大数据·营销·流量营销
跨境猫小妹1 天前
亚马逊合规新纪元:隐形战场里,谁在悄悄出局?
大数据·人工智能·产品运营·跨境电商·防关联
老陈头聊SEO1 天前
生成引擎优化(GEO)赋能数字内容战略与网络传播效果提升
其他·搜索引擎·seo优化
玄微云1 天前
AI智能体开发公司推荐:玄微科技专注垂直场景的实践者
大数据·人工智能·软件需求
数据智研1 天前
【数据分享】腾格里沙漠空间矢量范围
大数据·信息可视化·数据分析
智能化咨询1 天前
(68页PPT)埃森哲XX集团用户主数据治理项目汇报方案(附下载方式)
大数据·人工智能
数据智研1 天前
【数据分享】毛乌素沙地(毛乌素沙漠)空间矢量范围
大数据·人工智能·信息可视化·数据分析
Linux_Nathan1 天前
【服务部署】ELFK架构篇之Elasticsearch
elk·elasticsearch·架构
啊吧怪不啊吧1 天前
C++之基于正倒排索引的Boost搜索引擎项目searcher部分代码及详解
c++·搜索引擎·项目
TinpeaV1 天前
Elasticsearch8(ES)保姆级菜鸟入门教程
大数据·spring boot·elasticsearch·搜索引擎·全文检索·postman