• 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

  • 相关阅读:
    对ArcGis Engine的增、删、改实现
    修改 ArcGis Engine 图层字段值
    获取DataTable 删除行的数据
    使用Kdiff3 来解决Git的文件冲突
    WordPress 用Windows Live Write写日志
    在GIT 中增加忽略文件夹与文件
    解决WinDbg下不能用 !ClrStack a
    DevExpress 实现下拉复选控件
    解决远程桌面连接后没有声音的问题
    读书
  • 原文地址:https://www.cnblogs.com/libin2015/p/10649189.html
Copyright © 2020-2023  润新知