ElasticSearch备考 -- Alias

一、题目

1) Create the alias `hamlet` that maps both `hamlet-1` and `hamlet-2`

Verify that the documents grouped by `hamlet` are 8

2) Configure `hamlet-3` to be the write index of the `hamlet` alias

二、思考

可以通过指定别名,来指向一个或多个索引,可以理解成一个linux系统重软链接。

别名创建三种方式

  • 在创建索引时指定别名
  • 索引模版中创建别名
  • 对已有的索引创建别名

通过分析题目1)可以看出是使用第三种方式。题目2)是使用第一种方式。

三、解题

初始化数据

hamlet-1/_doc/_bulk
{"index":{"_index":"hamlet-1","_id":0}}
{"line_number":"1.1.1","speaker":"BERNARDO","text_entry":"Whos there?"}
{"index":{"_index":"hamlet-1","_id":1}}
{"line_number":"1.1.2","speaker":"FRANCISCO","text_entry":"Nay, answer me: stand, and unfold yourself."}
{"index":{"_index":"hamlet-1","_id":2}}
{"line_number":"1.1.3","speaker":"BERNARDO","text_entry":"Long live the king!"}
{"index":{"_index":"hamlet-1","_id":3}}
{"line_number":"1.2.1","speaker":"KING CLAUDIUS","text_entry":"Though yet of Hamlet our dear brothers death"}

hamlet-2/_doc/_bulk
{"index":{"_index":"hamlet-2","_id":4}}
{"line_number":"2.1.1","speaker":"LORD POLONIUS","text_entry":"Give him this money and these notes, Reynaldo."}
{"index":{"_index":"hamlet-2","_id":5}}
{"line_number":"2.1.2","speaker":"REYNALDO","text_entry":"I will, my lord."}
{"index":{"_index":"hamlet-2","_id":6}}
{"line_number":"2.1.3","speaker":"LORD POLONIUS","text_entry":"You shall do marvellous wisely, good Reynaldo,"}
{"index":{"_index":"hamlet-2","_id":7}}
{"line_number":"2.1.4","speaker":"LORD POLONIUS","text_entry":"Before you visit him, to make inquire"}

题目一

Step 1、对已有索引创建别名

POST _aliases
{
  "actions": [
    {
      "add": {
        "index": "hamlet-1",
        "alias": "hamlet"
      }
    },
    {
      "add": {
        "index": "hamlet-2",
        "alias": "hamlet"
      }
    }
  ]
}

Step 2、通过查询验证别名

POST hamlet/_count

题目二

方案一:创建索引指定别名

aliases 和 mappings 平级

aliases 中别名中花括号不用写任何内容

PUT /hamlet-3
{
  "aliases": {
    "hamlet": {}
  }, 
  "mappings": {
    "properties": {
      "line_number": {
        "type": "text"
      },
      "speaker": {
        "type": "text"
      },
      "text_entry": {
        "type": "text"
      }
    }
  }
}

方案二:索引指定别名可以写

通过参数"is_write_index": true,可以别名执行写操作

POST /_aliases
{
  "actions": [
    {
      "add": {
        "index": "hamlet-1",
        "alias": "hamlet"
      }
    },
    {
      "add": {
        "index": "hamlet-2",
        "alias": "hamlet"
      }
    },
    {
      "add": {
        "index": "hamlet-3",
        "alias": "hamlet",
        "is_write_index": true
      }
    }
  ]
}

四、总结

别名创建三种方式,注意每种方式的适用场景

  • 在创建索引时指定别名
  • 索引模版中创建别名
  • 对已有的索引创建别名

参考资料

送一波福利:

福利一

有需要内推JD的同学,可以私信或留言,我帮您内推,流程快!!!

有需要内推JD的同学,可以私信或留言,我帮您内推,流程快!!!

有需要内推JD的同学,可以私信或留言,我帮您内推,流程快!!!

福利二

福利三

Elasticsearch X-Pack 是一个强大的插件,它为 Elasticsearch 提供了一系列的扩展功能,包括安全、监控、报告、机器学习等。下面简单介绍一下如何安装 Elasticsearch X-Pack。 1. 下载 Elasticsearch X-Pack 插件 你可以从 Elastic 官网下载对应版本的 X-Pack 插件,链接为:https://www.elastic.co/cn/downloads/x-pack。 2. 安装 Elasticsearch X-Pack 插件 安装 Elasticsearch X-Pack 插件需要使用 Elasticsearch 的插件安装命令。将下载好的 X-Pack 插件解压到 Elasticsearch 的 plugins 目录下,然后使用以下命令安装插件: ``` bin/elasticsearch-plugin install file:///path/to/x-pack-xxx.zip ``` 其中,`/path/to/x-pack-xxx.zip` 是 X-Pack 插件的压缩包路径。 3. 配置 Elasticsearch X-Pack 插件 安装 Elasticsearch X-Pack 插件后,你需要在 Elasticsearch 的配置文件中进行相应的配置。具体配置方法可以参考官方文档。 4. 启动 Elasticsearch X-Pack 插件 启动 Elasticsearch X-Pack 插件需要先启动 Elasticsearch,然后在 Elasticsearch 的命令行界面输入以下命令: ``` bin/elasticsearch ``` 如果一切顺利,你应该可以看到 Elasticsearch 启动成功的日志信息。 5. 验证 Elasticsearch X-Pack 插件是否安装成功 你可以通过访问 Elasticsearch 的 API 或者使用 Kibana 来验证 Elasticsearch X-Pack 插件是否安装成功。如果安装成功,你应该可以看到 X-Pack 相关的 API 和功能。 总之,在安装 Elasticsearch X-Pack 插件之前,你需要先了解自己的 Elasticsearch 版本,然后下载对应版本的 X-Pack 插件。安装插件需要使用 Elasticsearch 的插件安装命令,并且需要在 Elasticsearch 的配置文件中进行相应的配置。启动插件后,你可以通过访问 Elasticsearch 的 API 或者使用 Kibana 来验证插件是否安装成功。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值