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的同学,可以私信或留言,我帮您内推,流程快!!!

福利二

福利三

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值