002es生命周期之索引别名的使⽤

简介:es之索引别名的使⽤

别名有什么用

在开发中,随着业务需求的迭代,较⽼的业务逻辑就要⾯临更新甚⾄是重构,⽽对于es来说,为了适应新的业务逻辑,可能就要对原有的索引做⼀些修改,⽐如对某些字段做调整,甚⾄是重建索引。⽽做这些操作的时候,可能会对业务造成影响,甚⾄是停机调整等问题。由此,es提供了索引别名来解决这些问题。 索引别名就像⼀个快捷⽅式或是软连接,可以指向⼀个或多个索引,也可 以给任意⼀个需要索引名的API来使⽤。别名的应⽤为程序提供了极⼤地灵活性

查询别名

GET /nba/_alias
GET /_alias

新增别名

POST /_aliases
{
	"actions": [{
		"add": {
			"index": "nba",
			"alias": "nba_v1.0"
		}
	}]
}
PUT /nba/_alias/nba_v1.1

删除别名

POST /_aliases
{
	"actions": [{
		"remove": {
			"index": "nba",
			"alias": "nba_v1.0"
		}
	}]
}
DELETE /nba/_alias/nba_v1.1

重命名

POST /_aliases
{
	"actions": [{
			"remove": {
				"index": "nba",
				"alias": "nba_v1.0"
			}
		},
		{
			"add": {
				"index": "nba",
				"alias": "nba_v2.0"
			}
		}
	]
}

为多个索引指定⼀个别名

POST /_aliases
{
	"actions": [{
			"add": {
				"index": "nba",
				"alias": "national_player"
			}
		},
		{
			"add": {
				"index": "wnba",
				"alias": "national_player"
			}
		}
	]
}

为同个索引指定多个别名

POST /_aliases
{
	"actions": [{
			"add": {
				"index": "nba",
				"alias": "nba_v2.1"
			}
		},
		{
			"add": {
				"index": "nba",
				"alias": "nba_v2.2"
			}
		}
	]
}

通过别名读索引

  • 当别名指定了⼀个索引,则查出⼀个索引
GET /nba_v2.1
  • 当别名指定了多个索引,则查出多个索引
GET /national_player

通过别名写索引

  • 当别名指定了⼀个索引,则可以做写的操作
POST /nba_v2.1/_doc/566
{
	"countryEn": "Croatia",
	"teamName": "快船",
	"birthDay": 858661200000,
	"country": "克罗地亚",
	"teamCityEn": "LA",
	"code": "ivica_zubac",
	"displayAffiliation": "Croatia",
	"displayName": "伊维察 祖巴茨哥哥",
	"schoolType": "",
	"teamConference": "⻄部",
	"teamConferenceEn": "Western",
	"weight": "108.9 公⽄",
	"teamCity": "洛杉矶",
	"playYear": 3,
	"jerseyNo": "40",
	"teamNameEn": "Clippers",
	"draft": 2016,
	"displayNameEn": "Ivica Zubac",
	"heightValue": 2.16,
	"birthDayStr": "1997-03-18",
	"position": "中锋",
	"age": 22,
	"playerId": "1627826"
}
  • 当别名指定了多个索引,可以指定写某个索引
POST /_aliases
{
	"actions": [{
			"add": {
				"index": "nba",
				"alias": "national_player",
				"is_write_index": true
			}
		},
		{
			"add": {
				"index": "wnba",
				"alias": "national_player"
			}
		}
	]
}
POST /national_player/_doc/566
{
	"countryEn": "Croatia",
	"teamName": "快船",
	"birthDay": 858661200000,
	"country": "克罗地亚",
	"teamCityEn": "LA",
	"code": "ivica_zubac",
	"displayAffiliation": "Croatia",
	"displayName": "伊维察 祖巴茨妹妹",
	"schoolType": "",
	"teamConference": "⻄部",
	"teamConferenceEn": "Western",
	"weight": "108.9 公⽄",
	"teamCity": "洛杉矶",
	"playYear": 3,
	"jerseyNo": "40",
	"teamNameEn": "Clippers",
	"draft": 2016,
	"displayNameEn": "Ivica Zubac",
	"heightValue": 2.16,
	"birthDayStr": "1997-03-18",
	"position": "中锋",
	"age": 22,
	"playerId": "1627826"
}
posted @ 2023-01-05 23:33  arun_yh  阅读(106)  评论(0编辑  收藏  举报