在Elasticsearch,有时要通过索引日期来筛选某段时间的数据,这时就要用到ES提供的日期数学表达式
特别在日志数据中,只是查询一段时间内的日志数据,这时就可以使用日期数学表达式,这样可以限制检索的索引数量,减少集群的负载,提高系统性能。
几乎所有的API都支持日期索引中的数学参数值。
<static_name{date_math_expr{date_format|time_zone}}>
其中各个字段的含义是:
static_name:索引名字的静态部分
date_math_expr:动态的日期表达式
date_format:格式化,默认是YYYY.MM.dd
time_zone:时区,默认是UTC
需要注意的是,在使用时要把索引以及日期表达式的部分放在<>
尖括号内。
比如现在的时间是2024年3月22日中午12点.utc
表达式 | 表达式的值 |
---|---|
<test-{now/d}> | test-2024.03.22 |
<test-{now/M}> | test-2024.03.01 |
<test-{now/M{YYYY.MM}}> | test-2024.03 |
<test-{now/M-1M{YYYY.MM}}> | test-2024.02 |
<test-{now/d{YYYY.MM.dd\|+12:00}}> | test-2024.03.23 |
在数学日期表达式中,now就是现在的时间2016.05.09 14:11:26
它还支持加减法,比如:
curl -XPOST '192.168.204.32:9200/<test-\{now%2FM\}>/type/1?pretty' -d '{"name":"xing1",age:20}'‘
{
"_index" : "test-2016.05.01",
"_type" : "type",
"_id" : "1",
"_version" : 1,
"_shards" : {
"total" : 2,
"successful" : 2,
"failed" : 0
},
"created" : true
}
注意:
/
必须经过编码也就是%2F
# curl -XPOST '192.168.204.42:9200/<test-\{now%2FM\}>/_search?pretty' -d '{"query":{"match_all":{}}}'
{
"took" : 3,
"timed_out" : false,
"_shards" : {
"total" : 5,
"successful" : 5,
"failed" : 0
},
"hits" : {
"total" : 1,
"max_score" : 1.0,
"hits" : [ {
"_index" : "test-2016.05.01",
"_type" : "type",
"_id" : "1",
"_score" : 1.0,
"_source" : {
"name" : "xing1",
"age" : 20
}
} ]
}
}
几本上所有api索引参数,支持日期索引中数学参数值。
扫码关注腾讯云开发者
领取腾讯云代金券
Copyright © 2013 - 2025 Tencent Cloud. All Rights Reserved. 腾讯云 版权所有
深圳市腾讯计算机系统有限公司 ICP备案/许可证号:粤B2-20090059 深公网安备号 44030502008569
腾讯云计算(北京)有限责任公司 京ICP证150476号 | 京ICP备11018762号 | 京公网安备号11010802020287
Copyright © 2013 - 2025 Tencent Cloud.
All Rights Reserved. 腾讯云 版权所有