索引 别名 就像一个快捷方式或软连接,可以指向一个或多个索引,也可以给任何一个需要索引名的API来使用。
索引别名用处:
1、在运行的集群中可以无缝的从一个索引切换到另一个索引(原子操作,无需担心别名未指向索引的短时间段)
2、给多个索引分组
3、给索引的一个子集创建视图
//创建索引别名方式1
POST /_aliases
{
"actions" : [
{ "add" : { "index" : "index_test_1", "alias" : "test_alias" } }
]
}
//创建索引别名方式2
POST /index_test_1/_alias/test_alias
//创建索引别名方式3
PUT index_test_2
{
"aliases": {
"test_alias": {}
},
"settings": {
"number_of_replicas": 1,
"number_of_shards": 2
},
"mappings": {
"test": {
"dynamic": "strict",
"properties": {
"name": {
"type": "text",
"analyzer": "ik_max_word",
"fields": {
"keyword": {
"type": "keyword"
}
}
},
"age": {
"type": "integer"
},
"gender":{
"type": "integer"
}
}
}
}
}
//移除索引别名
DELETE index_test_1/_alias/test_alias
或
POST /_aliases//移除并切换索引
{
"actions" : [
{ "remove" : { "index" : "index_test_1", "alias" : "test_alias" } },
{ "add" : { "index" : "index_test_2", "alias" : "test_alias" } }
]
}
//查看索引别名
GET /*/_alias/test_alias //这些索引别名指向哪些索引
GET /index_test_2/_alias/* //索引指向哪些别名
当我们拥有索引别名后,就可以通过索引别名,对索引别名中包含的所有所有进行搜索,和原本的查询完成一致,只需将索引名改成,索引别名即可:GET test_alias/_search?q=*。
GET test_alias/_search?q=*
GET index_test_1,index_test_2/_search?q=*
有索引别名指定两个索引后,以上两个查询时等同的。
就可以解决,按周期等情况,多个索引查询时,需要书写多个索引名的情景。
在7.0版本以下时,当索引别名对应多个索引时,不支持通过索引别名写入,当且仅当索引别名只对应一个索引时,才可以直接通过索引别名写入,否则还需通过索引名写入。
另外,索引别名中还可以指定,路由和过滤:
POST index_test_1/test?routing=1
{
"name":"张三",
"age":15
}
POST index_test_1/test?routing=5
{
"name":"李四",
"age":16
}
//索引别名,指定过滤
POST /_aliases
{
"actions": [
{
"add": {
"index": "index_test_1",
"alias": "test_alias_age_gt15",
"filter": {
"range": {
"age": {
"gt": 15
}
}
}
}
}
]
}
//索引别名,指定路由
POST /_aliases
{
"actions": [
{
"add": {
"index": "index_test_1",
"alias": "test_alias_routting_1",
"routing": "1"
}
}
]
}
GET test_alias_age_gt15/_search
GET test_alias_routting_1/_search