8. python-es-8.3.3-日期范围聚合date_range

本文介绍如何使用Elasticsearch进行日期范围聚合查询,并通过具体示例展示了配置索引、插入数据、设置查询条件的过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

世界上并没有完美的程序,但是我们并不因此而沮丧,因为写程序就是一个不断追求完美的过程。-侯氏工坊

文章目录

date_range

from elasticsearch import Elasticsearch
import urllib3

urllib3.disable_warnings()

# PUT es_date_range
# {
#   "mappings": {
#     "properties": {
#       "my_date": {"type": "date", "format": ["yyyy-MM-dd"]}
#     }
#   }
# }

# POST es_date_range/_bulk
# {"index": {"_id": 1}}
# {"my_date": "2022-01-01"}
# {"index": {"_id": 2}}
# {"my_date": "2022-02-03"}
# {"index": {"_id": 3}}
# {"my_date": "2022-02-13"}
# {"index": {"_id": 4}}
# {"my_date": "2022-02-05"}
# {"index": {"_id": 5}}
# {"my_date": "2022-03-05"}
# {"index": {"_id": 6}}
# {"my_date": "2022-05-06"}

# GET es_date_range/_search
# {
#   "size": 0,
#   "aggs": {
#     "my_date_histogram": {
#       "date_range": {
#         "field": "my_date",
#         "keyed": true,
#         "missing": "2022-01-01",
#         "ranges": [
#           {"to": "2022-02-02", "key": "first"},
#           {"from": "2022-02-02", "to": "2022-02-10", "key": "second"},
#           {"from": "2022-02-10", "key": "third"}
#         ]
#       }
#     }
#   }
# }

# 创建es实例
es = Elasticsearch("https://192.168.2.64:9200",
				   verify_certs=False,
				   basic_auth=("elastic", "MuZkDqdW--VsfDjTcoex"),
				   request_timeout=60,
				   max_retries=3,
				   retry_on_timeout=True,
				   node_selector_class="round_robin")

# 刷新
es.indices.refresh(index="es_date_range")

# date_range doc
# from >=
# to <
# date/format:
# G era年代
# u year
# y year-of-era
# D day-of-year
# M/L month-of-year
# d day-of-month
# Q/q quarter-of-year
# Y week-based-year
# w week-of-week-based-year
# W week-of-month
# E day-of-week
# e/c localized day-of-week
# F week-of-month
# a am-pm-of-day
# h clock-hour-of-am-pm (1-12)
# K hour-of-am-pm (0-11)
# k clock-hour-of-am-pm (1-24)
# H hour-of-day (0-23)
# m minute-of-hour
# s second-of-minute
# S fraction-of-second
# A milli-of-day
# n nano-of-second
# N nano-of-day
# V time-zone ID
# z time-zone name
# O localized zone-offset
# X zone-offset Z for zero
# x zone-offset
# Z zone-offset
# p pad next
# ' escape for text
# single quote 单引号
# []
# {}
date_range = {
    "my_date_histogram": {
      "date_range": {
        "field": "my_date",
        "keyed": True,
        "missing": "2022-01-01",
        "ranges": [
          {"to": "2022-02-02", "key": "first"},
          {"from": "2022-02-02", "to": "2022-02-10", "key": "second"},
          {"from": "2022-02-10", "key": "third"}
        ]
      }
    }
  }

resp = es.search(index="es_date_range", aggregations=date_range)

print(resp['aggregations']['my_date_histogram']['buckets'])
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

这是谁的博客?

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值