Elasticsearch:自动使用服务器时间设置日期字段并更新时区

在大多数情况下,你的数据包含一个以 create_date 命名的字段。 即使没有日期字段,处理各种格式和时区的日期对数据仓库来说也是一个重大挑战。 与此类似,如果要检测变化的数据,则必须准确设置日期字段。

在 Elasticsearch 中还有一个选项可以自动将服务器的日期设置为字段。

我们将使用摄取管道属性的 setdate 处理器。

创建摄入管道

首先我们需要设置一个时间戳字段。 之后我们将使用日期处理器来更新字段。

日期处理器有一些功能。 target_field 属性就是其中之一。 如果未定义 target_field 属性,它将计算 field 并写入一个名为 @timestamp 的新字段。 但我们想要改变一个已经存在的字段。

bash 复制代码
1.  PUT _ingest/pipeline/sales-timestamp
2.  {
3.    "description": "Set two different timestamp fields.",
4.    "processors": [
5.      {
6.        "set": {
7.          "field": "timestamp",
8.          "value": "{{{_ingest.timestamp}}}"
9.        }
10.      },
11.      {
12.        "date": {
13.            "field": "timestamp",
14.            "target_field": "tr_timestamp",
15.            "timezone": "+0300",
16.            "formats": [ "ISO8601"]
17.        }
18.      }
19.    ]
20.  } 

运行上面的脚本后,系统将显示("acknowledged":true)消息:

markdown 复制代码
1.  {
2.    "acknowledged": true
3.  }

此外,还可以使用 DELETE 命令进行删除或使用 GET 命令验证属性。 重新运行 PUT 命令应该足以更新管道。

sql 复制代码
1.  GET _ingest/pipeline

3.  DELETE _ingest/pipeline/sales-timestamp

下一步是使用管道创建索引。 对于新建立的摄取管道,必须设置 index.default_pipeline。

即使它会自动填充 date 字段,你仍然需要在索引的映射中定义它们

sql 复制代码
1.  # Create "sales" Index
2.  PUT sales
3.  {
4.    "settings": {
5.      "index.default_pipeline": "sales-timestamp"
6.    },
7.    "mappings": {
8.       "properties": {
9.         "timestamp": { "type": "date" },
10.         "tr_timestamp": { "type": "date" },
11.         "name": { "type": "text" },
12.         "authour": { "type": "keyword" }
13.       }
14.     }
15.  }

写入数据

当你想同时向一个索引添加多个数据时,可以使用 Bulk API。 它尝试从每一行解析你的脚本。 这意味着你不能在批量插入期间使用格式化的 JSON。

通过这种技术,Elasticsearch 会自动为数据分配一个 ID。

bash 复制代码
1.  POST sales/_bulk
2.  {"index":{}}
3.  {"name":"The Lord of the Rings: The Fellowship of the Ring","authour":"J. R. R. Tolkien"}
4.  {"index":{}}
5.  {"name":"The Lord of the Rings 2: The Two Towers","authour":"J. R. R. Tolkien"}

下一步将允许我们列出或搜索我们的数据。

bash 复制代码
1.  GET sales/_search?filter_path=**.hits
2.  {
3.    "size": 5, 
4.    "query": {
5.      "match_all": {}
6.    }
7.  }

上面运行的结果为:

json 复制代码
1.  {
2.    "hits": {
3.      "hits": [
4.        {
5.          "_index": "sales",
6.          "_id": "rVjrTooBxPLM4Lwr4CwQ",
7.          "_score": 1,
8.          "_source": {
9.            "name": "The Lord of the Rings: The Fellowship of the Ring",
10.            "authour": "J. R. R. Tolkien",
11.            "tr_timestamp": "2023-09-01T07:06:35.783+03:00",
12.            "timestamp": "2023-09-01T04:06:35.783682Z"
13.          }
14.        },
15.        {
16.          "_index": "sales",
17.          "_id": "rljrTooBxPLM4Lwr4CwQ",
18.          "_score": 1,
19.          "_source": {
20.            "name": "The Lord of the Rings 2: The Two Towers",
21.            "authour": "J. R. R. Tolkien",
22.            "tr_timestamp": "2023-09-01T07:06:35.792+03:00",
23.            "timestamp": "2023-09-01T04:06:35.792130Z"
24.          }
25.        }
26.      ]
27.    }
28.  }

在这种情况下,你应该密切关注 tr_timestamp 和 timestamp 数据。 tr_timestamp 列中的数据末尾有 "+03:00"。

你可以向索引添加更多数据。

bash 复制代码
1.  POST sales/_bulk
2.  {"index":{}}
3.  {"name":"The Lord of the Rings 3: The Return of the King", "authour":"J. R. R. Tolkien"}
json 复制代码
 1.        {
2.          "_index": "sales",
3.          "_id": "r1j0TooBxPLM4LwreSyl",
4.          "_score": 1,
5.          "_source": {
6.            "name": "The Lord of the Rings 3: The Return of the King",
7.            "authour": "J. R. R. Tolkien",
8.            "tr_timestamp": "2023-09-01T07:15:59.397+03:00",
9.            "timestamp": "2023-09-01T04:15:59.397509Z"
10.          }
11.        }

更多关于 pipeline 的使用方法,请阅读文章 "Elasticsearch:ingest pipelines - 使用技巧和窍门"。

相关推荐
wdxylb7 小时前
GIt使用笔记大全
笔记·git·elasticsearch
Elastic 中国社区官方博客20 小时前
使用真实 Elasticsearch 进行高级集成测试
大数据·数据库·elasticsearch·搜索引擎·全文检索·jenkins·集成测试
画船听雨眠aa1 天前
gitlab云服务器配置
服务器·git·elasticsearch·gitlab
risc1234561 天前
【Elasticsearch 】悬挂索引(Dangling Indices)
大数据·elasticsearch·搜索引擎
Elastic 中国社区官方博客1 天前
使用 Ollama 和 Kibana 在本地为 RAG 测试 DeepSeek R1
大数据·数据库·人工智能·elasticsearch·ai·云原生·全文检索
zfj3211 天前
学技术学英语:elasticsearch硬件相关的配置&优化技巧
网络·elasticsearch·全文检索·内存·cpu·磁盘
字节全栈_vBr2 天前
面试之Solr&Elasticsearch
elasticsearch·面试·solr
risc1234562 天前
【Elasticsearch】中数据流需要配置索引模板吗?
大数据·elasticsearch·jenkins
JackieZhengChina2 天前
新一代搜索引擎,是 ES 的15倍?
大数据·elasticsearch·搜索引擎
是小崔啊2 天前
Spring Boot - 数据库集成06 - 集成ElasticSearch
数据库·spring boot·elasticsearch