本文已参与「新人创作礼」活动,一起开启掘金创作之路。
包括内容:Reindex数据迁移,复合聚合按计数排序,TSVB使用脚本字段,滚动升级,Elastic中国社区官方博客
1. Reindex数据迁移
重建索引(_reindex),即:一旦索引被创建,则无法直接修改索引字段的mapping属性,必需要重建索引然后将旧的索引数据迁移到新的索引中才行(迁移过程底层使用了scroll API )。
示例:
POST _reindex
{
"conflicts": "proceed", # 发生冲突继续执行
"source": {
"index": "old_index",
"type": "_doc",
"size": 5000, # 设置每批迁移的文档记录数
"_source": ["user", "_doc"], # 可设置要迁移的索引字段,不设置则默认所有字段
"query": { # 可设置要迁移的文档记录过滤条件
"match_all": { }
}
},
"dest": {
"index": "new_index",
"type": "_doc",
"version_type": "internal" # "internal"或者不设置,则Elasticsearch强制性的将文档转储到目标中,覆盖具有相同类型和ID的任何内容
}
}
2. 复合聚合按计数排序
GET /service-exception/_search
{
"size": 0,
"query": {
"range": {
"@timestamp": {
"gte": "2021-05-06 00:00:00",
"lte": "2021-05-06 23:59:59",
"format": "yyyy-MM-dd HH:mm:ss",
"time_zone":"+08:00"
}
}
},
"aggs": {
"group_by_fields": {
"composite":{
"sources": [
{
"service_name": {
"terms": {
"field": "service_name"
}
}
},{
"error_message": {
"terms": {
"field": "error_message"
}
}
}
],
"size": 50
},
"aggs": {
"top_bucket_sort":{
"bucket_sort": {
"sort": [
{ "_count": { "order": "desc" } }
]
}
}
}
}
}
}
3. TSVB使用脚本字段([TSVB] Visualize Scripted Fields)
- [TSVB] FR: Scripted Fields / Scripted Aggregations #13928 github.com/elastic/kib…
The PR was closed and this comment explains why:
[#68332 (comment)](https://github.com/elastic/kibana/pull/68332#issuecomment-678154430)
-
[TSVB] Add scripted aggregation support to TSVB #68332 github.com/elastic/kib…
-
Add support for runtime fields #59332 github.com/elastic/ela…
https://www.elastic.co/guide/en/elasticsearch/reference/7.13/runtime.html
4. 滚动升级(Rolling Upgrade Elasticsearch)
注意事项:
- 不支持在升级期间以外在同一集群中运行多个版本的Elasticsearch,因为不能将分片从升级的节点复制到运行旧版本的节点。
- 升级时将集群的节点划分为两组并按顺序升级组(第一组:Nodes that are not Master-eligible,第二组:Master-eligible nodes),可以确保所有Master-ineligible节点都能够加入集群,无论符合Master-eligible节点是否已经升级。
- 一旦您开始将集群升级到指定版本,您就必须完成升级,它可能对其内部状态进行无法恢复的更改
升级步骤:
- 禁用分片分配
- 停止非必要的索引和执行同步刷新(可选)
- 暂时停止与活动机器学习作业和数据源相关的任务(可选)
- 关闭单个节点
- 升级关闭的节点(如果没有使用外部数据目录,请将旧数据目录复制到新安装)
- 升级任何插件
- 启动升级后的节点
- 重新启用分片分配
- 等待节点恢复
- 对于需要更新的每个节点重复这些步骤
- 重新启动机器学习作业
检查
# 升级时将集群的节点划分为两组并按顺序升级组
GET /_nodes/_all,master:false
GET /_nodes/master:true
# 禁用分片分配
PUT _cluster/settings
{
"persistent": {
"cluster.routing.allocation.enable": "primaries"
}
}
# 停止非必要的索引和执行同步刷新
POST _flush/synced
# 重新启用分片分配
PUT _cluster/settings
{
"persistent": {
"cluster.routing.allocation.enable": null
}
}
# 等待节点恢复
GET _cat/health?v=true
# 对于需要更新的每个节点重复这些步骤
GET /_cat/health?v=true
GET /_cat/nodes?h=ip,name,version&v=true
# 重新启动机器学习作业
POST _ml/set_upgrade_mode?enabled=false
5. 数据增强
数据增强有很多种方式
- 数据源处理
- 自定义脚本
- Logstash Filter
- ES pipeline