elasticsearch 别名的操作详细指导

导读:本篇文章讲解 elasticsearch 别名的操作详细指导,希望对大家有帮助,欢迎收藏,转发!站点地址:www.bmabk.com

强大的es提供了这样的操作:别名操作。让你多个的index可以只用一个index查询。常用操作

添加别名

curl -XPOST 'http://localhost:9200/_aliases' -d 
{
    "actions": [
        {"add": {"index": "test1", "alias": "alias1"}}
    ]
}

删除别名

curl -XPOST 'http://localhost:9200/_aliases' -d 
{
    "actions": [
        {"remove": {"index": "test1", "alias": "alias1"}}
    ]
}

重新命名别名

curl -XPOST 'http://localhost:9200/_aliases' -d '
{
    "actions": [
        {"remove": {"index": "test1", "alias": "alias1"}},
        {"add": {"index":"test1", "alias": "alias2"}}
    ]
}'

将多个index赋予一个别名

curl -XPOST 'http://localhost:9200/_aliases' -d '
{
    "actions": [
        {"add": {"index": "test1", "alias":"alias1"}},
        {"add": {"index": "test2", "alias":"alias1"}}
    ]
}'

更多操作:

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.bmabk.com/index.php/post/13980.html

(0)
小半的头像小半

相关推荐

极客之音——专业性很强的中文编程技术网站,欢迎收藏到浏览器,订阅我们!