【Elasticsearch】索引别名 aliases

在博文《【Elasticsearch】动手创建一个索引》中,我们创建了一个名为 test-index 的索引,并且指定索引别名为 log-index,同时设置其为写入索引。

如果此时我再次建立一个索引,名称为 test-index-01,别名为 log-index,也指定为写入索引,会发生什么呢?

您说得完全正确!这会报错,因为一个别名在同一时间只能指向一个作为写入索引的索引。

🚫 1.错误演示

1.1 当前状态:

  • test-index ← 别名 log-indexis_write_index: true

1.2 执行以下命令会报错

PUT /test-index-01
{
  "settings": {
    "number_of_shards": 3,
    "number_of_replicas": 1,
    "refresh_interval": "15s"
  },
  "aliases": {
    "log-index": {
      "is_write_index": true  // ← 冲突!
    }
  },
  "mappings": {
    "properties": {
      // ... 字段定义
    }
  }
}

错误信息类似:
在这里插入图片描述

✅ 2.解决方案

如果想要切换写入索引,有以下几种正确的做法。

2.1 方案 1:先移除旧索引的写入权限,再设置新索引

// 步骤1:移除旧索引的写入权限
POST /_aliases
{
  "actions": [
    {
      "add": {
        "index": "test-index",
        "alias": "log-index",
        "is_write_index": false
      }
    }
  ]
}

// 步骤2:创建新索引并设置为写入索引
PUT /test-index-01
{
  "settings": {
    "number_of_shards": 3,
    "number_of_replicas": 1,
    "refresh_interval": "15s"
  },
  "aliases": {
    "log-index": {
      "is_write_index": true
    }
  },
  "mappings": {
    "properties": {
      "timestamp": {
        "type": "date",
        "format": "yyyy-MM-dd HH:mm:ss||epoch_millis"
      },
      "message": {
        "type": "text",
        "analyzer": "ik_max_word"
      }
      // ... 其他字段
    }
  }
}

2.2 方案 2:使用原子操作一次性切换

POST /_aliases
{
  "actions": [
    {
      "add": {
        "index": "test-index-01",
        "alias": "log-index",
        "is_write_index": true
      }
    },
    {
      "add": {
        "index": "test-index", 
        "alias": "log-index",
        "is_write_index": false
      }
    }
  ]
}

2.3 方案 3:创建时不设置写入索引,后续再切换

// 步骤1:创建新索引(不设置写入索引)
PUT /test-index-01
{
  "settings": {
    "number_of_shards": 3,
    "number_of_replicas": 1,
    "refresh_interval": "15s"
  },
  "aliases": {
    "log-index": {}  // ← 只添加别名,不设置写入索引
  },
  "mappings": {
    // ... 字段定义
  }
}

// 步骤2:原子切换写入索引
POST /_aliases
{
  "actions": [
    {
      "add": {
        "index": "test-index-01",
        "alias": "log-index", 
        "is_write_index": true
      }
    },
    {
      "add": {
        "index": "test-index",
        "alias": "log-index",
        "is_write_index": false
      }
    }
  ]
}

🔍 3.验证当前状态

在执行任何操作前,您可以先检查当前别名状态:

3.1 查看别名指向的所有索引

GET /_alias/log-index

在这里插入图片描述

3.2 查看哪个索引是当前写入索引

GET /_cat/aliases/log-index?v

在这里插入图片描述

3.3 查看所有包含写入索引的别名

GET /_cat/aliases?v&filter_path=is_write_index:true

在这里插入图片描述

💡 4.实际应用场景

这种设计在以下场景中很有用:

4.1 场景:基于时间的索引滚动

# 第1个月:logs-2024-01 是写入索引
# 第2个月:切换到 logs-2024-02 为写入索引
# 第3个月:切换到 logs-2024-03 为写入索引
# 但应用程序始终使用别名 "logs" 写入
  • 1️⃣ 设置 logs-2024-01 为写入索引。

在这里插入图片描述

  • 2️⃣ 通过别名往索引中写入数据。

在这里插入图片描述

  • 3️⃣ 可以看到此时数据写入了 logs-2024-01

在这里插入图片描述

  • 4️⃣ 切换写入索引为 logs-2024-02

在这里插入图片描述

  • 5️⃣ 可以看到 logs-2024-02 此时已成为别名 logs 的写入索引了。

在这里插入图片描述

  • 6️⃣ 再次通过索引别名往索引中写入数据。

在这里插入图片描述

  • 7️⃣ 可以观察到,此时写入的数据已经存储到了 logs-2024-02 中。

在这里插入图片描述

4.2 场景:索引重建

# 阶段1:products-v1 是写入索引
# 阶段2:创建 products-v2,数据迁移
# 阶段3:切换写入索引到 products-v2
# 阶段4:下线 products-v1

🎯 5.总结

  • 一个别名可以指向多个索引
  • 但只能有一个索引设置为 is_write_index: true
  • 切换写入索引需要使用原子操作
  • 应用程序可以始终使用别名,无需关心底层索引变化

这种机制正是 Elasticsearch 实现索引滚动、零停机重建等高级功能的基础。

### 在 Kibana 创建 Elasticsearch 索引aliases 参数的作用 在使用 Kibana 创建 Elasticsearch 索引时,`aliases` 参数允许为索引创建别名别名是一个指向实际索引的引用,可以简化查询操作并提供更灵活的索引管理方式[^1]。以下是关于 `aliases` 参数作用的详细说明: #### 1. 提高查询灵活性 通过为索引设置别名,可以在查询时直接使用别名代替实际索引名称。例如,如果有一个索引名为 `user_info1`,并且为其设置了别名 `user_info_rep`,那么无论是查询索引本身还是其别名,都可以得到相同的结果。 ```json PUT user_info1 { "aliases": { "user_info_rep": {} } } ``` #### 2. 支持索引轮换 在某些场景下,可能需要定期轮换索引(如按日期分片存储日志数据)。通过别名,可以将查询指向最新的索引,而无需修改应用程序中的索引名称。例如,假设每天生成一个新的日志索引,可以通过别名 `current_logs` 指向当天的索引[^3]。 ```json POST _aliases { "actions": [ { "add": { "index": "logs_2023-10-01", "alias": "current_logs" } } ] } ``` #### 3. 实现无缝迁移 当需要对现有索引进行重构或重新索引时,别名可以帮助实现无缝迁移。例如,在创建新版本索引后,只需更新别名指向新索引,而无需中断应用程序的正常运行[^1]。 ```json POST _reindex { "source": { "index": "old_index" }, "dest": { "index": "new_index" } } POST _aliases { "actions": [ { "remove": { "index": "old_index", "alias": "my_alias" } }, { "add": { "index": "new_index", "alias": "my_alias" } } ] } ``` #### 4. 组合多个索引 别名可以指向多个索引,从而方便地对一组索引执行联合查询。例如,假设存在多个日志索引(`logs_2023-09`、`logs_2023-10`),可以通过创建一个别名 `all_logs` 来同时查询这些索引。 ```json POST _aliases { "actions": [ { "add": { "index": "logs_2023-09", "alias": "all_logs" } }, { "add": { "index": "logs_2023-10", "alias": "all_logs" } } ] } ``` --- ### 注意事项 - 别名不能包含空格或特殊字符,只能由小写字母、数字、连字符 (`-`) 和下划线 (`_`) 组成。 - 别名不能与现有的索引名称冲突。 - 别名本身不存储任何数据,仅作为指向实际索引的引用。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

大数据与AI实验室

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值