ElasticSearch 通过script脚本自动生成Asia/Shanghai(东八区)时间
程序员文章站
2022-05-19 10:04:51
...
公司规定每次往es重中写入数据的时候都需要自动生成三个时间:create_time(创建时间),update_time(更新时间 默认等于创建时间),delete_time(删除时间默认为0)
一.es默认为utc时间
es默认是按UTC时间计算存储,国内的时区是+8(东八区),所以比UTC时间快8小时
二.script脚本自动生成时间以及默认值
1.定义管道(Pipeline)
# 通过管道修改默认utc时间为东八区时间,并将默认值赋值给 create_time,update_time,delete_time
PUT _ingest/pipeline/pipeline_cocktail_py_date
{
"description" : "inner pipeline cocktail_py",
"processors" : [
{
"script": {
"lang": "painless",
"source": """
def imp = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
imp.setTimeZone(TimeZone.getTimeZone("Asia/Shanghai"));
def ts = imp.format(new Date((new Date().getTime())));
if (ctx.create_time==null ){
ctx.create_time = ts;
}
if (ctx.delete_time==null ){
ctx.delete_time = 0;
}
ctx.update_time = ts;
"""
}
}
]
}
2.es自定义动态模板
# 通过模板先规范create_time,delete_time,update_time的类型以及相应的格式
PUT /my-index-100002?pretty
{
"settings": {
"default_pipeline": "pipeline_cocktail_py_date"
},
"mappings": {
"properties": {
"delete_time": {
"type": "date",
"format": "epoch_millis"
},
"create_time": {
"type": "date",
"format": "yyyy-MM-dd HH:mm:ss.SSS"
},
"update_time": {
"type": "date",
"format": "yyyy-MM-dd HH:mm:ss.SSS"
}
}
}
}
3.插入测试数据
PUT my-index-100002/_doc/01
{
"name": "这是一条测试数据"
}
4.查看相应自动生成的数据情况以及相应mapping
GET /my-index-100002/_search
{
"query": {
"bool": {
"must": [
]
}
}
}
GET /my-index-100002/_mapping
参考:https://www.elastic.co/guide/en/elasticsearch/reference/current/script-processor.html