Elasticsearch 索引别名的使用

几种常用方式:

1.定时更新别名指向,更新后原索引可删除或保留

POST /_aliases
{
  "actions": [
    {
      "add": {
        "alias": "logs_current",
        "index": "logs_2018-10"
      }
    },
    {
      "remove": {
        "alias": "logs_current",
        "index": "logs_2018-09"
      }
    },
    {
      "add": {
        "alias": "last_3_months",
        "index": "logs_2018-10"
      }
    },
    {
      "remove": {
        "alias": "last_3_months",
        "index": "logs_2018-07"
      }
    }
  ]
}

2.更新索引指向并删除原索引

PUT test     
PUT test_2   
POST /_aliases
{
    "actions" : [
        { "add":  { "index": "test_2", "alias": "test" } },
        { "remove_index": { "index": "test" } }  
    ]
}

3.创建过滤视图

创建索引
PUT /test1 { "mappings": { "_doc": { "properties": { "user" : { "type": "keyword" } } } } }
创建视图
POST /_aliases
{
    "actions" : [
        {
            "add" : {
                 "index" : "test1",
                 "alias" : "alias2",
                 "filter" : { "term" : { "user" : "kimchy" } }
            }
        }
    ]
}

 

下面是索引相关操作基础知识:

_aliases:批量操作

创建别名

POST /_aliases
{
    "actions" : [
        { "add" : { "index" : "test1", "alias" : "alias1" } }
    ]
}

删除别名

POST /_aliases
{
    "actions" : [
        { "remove" : { "index" : "test1", "alias" : "alias1" } }
    ]
}

组合操作

POST /_aliases
{
    "actions" : [
        { "remove" : { "index" : "test1", "alias" : "alias1" } },
        { "add" : { "index" : "test2", "alias" : "alias1" } }
    ]
}
POST /_aliases
{
    "actions" : [
        { "add" : { "index" : "test1", "alias" : "alias1" } },
        { "add" : { "index" : "test2", "alias" : "alias1" } }
    ]
}

数组形式同时指定多个索引别名

POST /_aliases
{
    "actions" : [
        { "add" : { "indices" : ["test1", "test2"], "alias" : "alias1" } }
    ]
}

通配符形式

POST /_aliases
{
    "actions" : [
        { "add" : { "index" : "test*", "alias" : "all_test_indices" } }
    ]
}

 

通过别名指定routing

POST /_aliases
{
    "actions" : [
        {
            "add" : {
                 "index" : "test",
                 "alias" : "alias1",
                 "routing" : "1"
            }
        }
    ]
}

可以具体指定查询和索引的routing

POST /_aliases
{
    "actions" : [
        {
            "add" : {
                 "index" : "test",
                 "alias" : "alias2",
                 "search_routing" : "1,2",
                 "index_routing" : "2"
            }
        }
    ]
}

如果查询使用别名,并且参数中也指定了routing,则routing使用二者的交集,使用2.

GET /alias2/_search?q=user:kimchy&routing=2,3

当索引别名指向多个索引时,进行写操作,其中的一个索引必须被指定为写索引,并且只能指定一个,否则则无法写入。

POST /_aliases
{
    "actions" : [
        {
            "add" : {
                 "index" : "test",
                 "alias" : "alias1",
                 "is_write_index" : true
            }
        },
        {
            "add" : {
                 "index" : "test2",
                 "alias" : "alias1"
            }
        }
    ]
}

切换写索引属性,原子操作

POST /_aliases
{
    "actions" : [
        {
            "add" : {
                 "index" : "test",
                 "alias" : "alias1",
                 "is_write_index" : false
            }
        }, {
            "add" : {
                 "index" : "test2",
                 "alias" : "alias1",
                 "is_write_index" : true
            }
        }
    ]
}

 

_alias:单个别名操作

创建别名

PUT /{index}/_alias/{name}
参数:

index:The index the alias refers to. Can be any of * | _all | glob pattern | name1, name2, …

name:The name of the alias. This is a required option.

routing:An optional routing that can be associated with an alias.
filter:An optional filter that can be associated with an alias.

PUT /logs_201305/_alias/2013

创建一个过滤视图

PUT /users
{
    "mappings" : {
        "_doc" : {
            "properties" : {
                "user_id" : {"type" : "integer"}
            }
        }
    }
}
PUT /users/_alias/user_12
{
    "routing" : "12",
    "filter" : {
        "term" : {
            "user_id" : 12
        }
    }
}

创建索引时同时指定别名

PUT /logs_20162801
{
    "mappings" : {
        "_doc" : {
            "properties" : {
                "year" : {"type" : "integer"}
            }
        }
    },
    "aliases" : {
        "current_day" : {},
        "2016" : {
            "filter" : {
                "term" : {"year" : 2016 }
            }
        }
    }
}

删除别名

DELETE /{index}/_alias/{name}
index:* | _all | glob pattern | name1, name2, …
name:* | _all | glob pattern | name1, name2, …

DELETE /logs_20162801/_alias/current_day

查询别名

GET /logs_20162801/_alias/*
GET /_alias/2016
GET /_alias/20*

HEAD /_alias/2016
HEAD /_alias/20*
HEAD /logs_20162801/_alias/*

 

参考资料:

https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-aliases.html#alias-retrieving

 

转载于:https://www.cnblogs.com/libin2015/p/10649189.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值