一、题目
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
}
}
]
}
四、总结
别名创建三种方式,注意每种方式的适用场景
- 在创建索引时指定别名
- 索引模版中创建别名
- 对已有的索引创建别名
参考资料
- Aliases | Elasticsearch Guide [8.1] | Elastic
- https://www.elastic.co/guide/en/elasticsearch/reference/8.1/index-templates.html
送一波福利:
福利一
有需要内推JD的同学,可以私信或留言,我帮您内推,流程快!!!
有需要内推JD的同学,可以私信或留言,我帮您内推,流程快!!!
有需要内推JD的同学,可以私信或留言,我帮您内推,流程快!!!
福利二
福利三