前言
- 本文对 Elasticsearch 7.17 有效
- 创建时间(create_time)没找到好的实现方式
- 如果入库的数据不再更新,文中的 update_time 可等同于 create_time
update_time 示例
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
GET _ingest/pipeline
// 查看具体的 pipeline
GET _ingest/pipeline/add_update_time
PUT my_index
{
"settings": {
"index.default_pipeline": "add_update_time"
}
}
POST my_index/_doc/1
{
"title": "nothing"
}
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
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。