前言
- 本文对 Elasticsearch 7.13 无效
- 创立工夫(create_time)没找到好的实现形式
- 如果入库的数据不会更新,文中的 create_time 可等同于 update_time
update_time 示例
- 创立 Ingest pipelines(script、date)
PUT _ingest/pipeline/add_update_time
{
"processors": [
{
"script": {
"lang": "painless",
"source": """DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssX"); df.setTimeZone(TimeZone.getTimeZone("Asia/Shanghai")); Date date = new Date(); ctx.update_time = df.format(date);"""
}
}
]
}
- 创立索引,并设置默认 pipeline
PUT my_index
{
"settings": {"index.default_pipeline": "add_update_time"}
}
- 插入数据
POST my_index/_doc/1
{"title": "nothing"}
- 查看 mapping
GET my_index/_mapping
{
"my_index" : {
"mappings" : {
"properties" : {
"title" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
},
"update_time" : {"type" : "date"}
}
}
}
}
- 查问数据
GET my_index/_search
{
"query": {
"range": {
"update_time": {
"time_zone": "+08",
"gte": "2021-07-28T16:00:00",
"lte": "now"
}
}
}
}
{
"took" : 0,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 1,
"relation" : "eq"
},
"max_score" : 1.0,
"hits" : [
{
"_index" : "my_index",
"_type" : "_doc",
"_id" : "1",
"_score" : 1.0,
"_source" : {
"update_time" : "2021-07-28T18:03:35+08",
"title" : "nothing"
}
}
]
}
}
本文出自 qbit snap