世界上并没有完美的程序,但是我们并不因此而沮丧,因为写程序就是一个不断追求完美的过程。-侯氏工坊
文章目录
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'])