架构师系列-搜索引擎ElasticSearch(十)- 索引别名及重建

索引别名

别名,有点类似数据库的视图,别名一般都会和一些过滤条件相结合,可以做到即使是同一个索引上,让不同人看到不同的数据。

别名的作用

        在开发中,一般随着业务需求的迭代,较老的业务逻辑就要面临更新甚至是重构,对于es来说为了适应新的业务逻辑,就要对原有的索引做一些修改,比如对某些字段做调整。
        而做这些操作的时候,可能会对业务造成影响,甚至是停机调整等问题,因为es提供了索引的别名来解决这个问题,索引的别名就像一个快捷方式或者是软连接,可以指向一个或者多个索引,也可以给任意一个需要索引名的API来使用。

别名的操作

查询别名

直接调用 _a1ias API的GET方法可以看到索引的别名

GET 1ogstash-village-2023.0.25/_alias

使用别名进行查询

GET 1ogstash-vi1lage/_search {
    "query":{
        "match":{
            "name":“龙苑居住区”
        }
    }
}

创建别名

我们还可以在建立一个别名,别名和索引的关系是多对多的关系,一个索引可以有多个别名,同样一个别名也可以有多个索引 

POST /_aliases
{
    "actions": [{
        "add":{
            "index": "1ogstash-vi11age-2022.08.22",
            "alias": "logstash-vi1lage-1.0"
        }
    }]
}

别名修改

POST /_alias {
    "actions":[
        "remove":{
            "index":"1ogstash-vi11age-2022.08.22",
            "alias":"1ogstash-vi1lage-1.0"
        },
        {
            "add":{
                "index":"1ogstash-vi11age-2022.08.22",
                "alias": "1ogstash-vi1lage-2.0"
            }
        }
    ]
}

 别名的过滤

POST /_aliases {
    "actions":[
    {
        add":{
            "index":"logstash-village-2022.08.22",
            "alias":"logstash-village-hn",
            "filter":{
                "term":"province”:“河南省”
            }
        }
    }]
}

删除别名

查看索引的所有别名 
GET logstash-vi1lage-2023.04.25/_alias

删除其中一个别名
DELETE logstash-village-2023.04.25/ alias/logstash-village-route key

索引重建

es在字段的mapping建立后就不可再次修改mapping的值。在我们实际的情况下有些时候就是需要修改mapping的值,解决方案就是重新构建索引数据。

索引重建步骤

1、创建oldindex

 2、给索引创建别名

 3、想oldindex中插入9条数据

 4、创建新的索引newindex

 5、重建索引

 6、实现不重启服务索引的切换

创建旧索引

PUT oldindex 
{
    "mappings": {
        properties":{
            "name":{
                "type": "text
            },
            "price":{
                "type": "double"
            }
        }
    }
}

 添加数据

POST oldindex/_doc/_bulk
{"create":{"_id":1}}
{"name":"name 01","price":1}
{"create":{"_id":2}}
{"name":"name 02","price":2}
{"create":{" id":3}}
{"name":"name 03",price":3}
{"create":{"_id":4}}
{"name":"name 04","price":4}
{"create":{"_id":5}}
{"name":"name 05","price":5}
{"create":{"_id":6}}
{"name":"name 6","price":6}
{"create":{"_id":7}}
{"name":"name 07","price":7}
{"create":{"_id":8}}
{"name":"name 08","price":8}
{"create:{"_id":9}},
{"name" :"name 09","price":9}

查询数据

GET oldindex/_search {}

创建别名

POST /_aliases {
    "actions": [{
        "add": {
            "index": "oldindex",
            "alias": "search_index"
        }
    }]
}

使用别名查询数据

GET search_index/_search

 创建新的索引

根据需求我们创建一个新的索引,价格字段改为integer类型

PuT newindex 
{ 
    "mappings": {
        "properties":{
            "name":{
                "type":"text"
            },
            "price": {
                "type": "integer"
            }
        }
    }
}

重建新索引


数据量大的话可以异步执行,如果reindex时间过长,建议加上 wait_for_completion=false 的参数,这样 reindex 将直接返回 taskId。

PosT _reindex?wait_for_completion=false
{
    "conflicts":"proceed",//如果新的索引中数据冲突,程序继续往下执行,删除则会导致程序会终止
    "source":{
        "index":"oldindex"// 表示从oldindex中同步数据
    },
    "dest": { 
        "index":"newindex",// 表示数据插入新索引newindex中
        "op_type":"create"// 数据插入的类型为创建,如果存在就会版本冲突
    }
}

执行后会返回一个任务的ID

 可以根据任务id查询重建索引的情况,complete: true标识索引重建完成。

 取消重建索引任务
POST _task/索引重建任务ID/_cancel
更多参数示例
PosT _reindex?wait_for_completion=false
{
    "size”:5,// 表示只获取5条数据插入到新的索引中
    "conflicts":"proceed",//如果新索引中数据冲突,程序维续执行,删除会导致程序会止
    "source":{
        "size":2,//默认情况下,_reindex使用1000进行批量操作,调整批量插入2条
        "index":"oldindex",// 表示从o1dindex,类型product中查询出price字段的值"
        "_source":[ 
            "price”//只需要同步price字段
        ],
        "query":{
            "range":{
                "price":{
                    "gte": 2,
                    "1te":8
                }
            }
        }
    },
    "dest": {
        "index": "newindex", // 表示数据插入新索引newindex中
        "op_type": "create" // 数据插入的类型为创建,如果存在就会版本冲突
    }
}

别名切换

我们需要将别含切换到另刚刚重建的索引上,切换索引可以实现不重启服务索引的切换 

POST _aliases {
    "actions":[
    {
        "remove":{
            "index": "oldindex”,
            "alias":"search_index"
        },
    },
    {
        add":{
            "index": "newindex”,
            "alias": "search_index"
        }
    }
    ]
}

 删除旧索引

delete oldindex

 

 

  • 7
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值