一、使用背景
索引创建后不能对mapping进行修改,但具有修改mapping的需求。
二、实现过程
1、新建相似index
2、将原来index中数据复制到新索引,通过reindex接口
3、为新建的索引创建一个原来索引的别名——以保证之前的查询条件不改变,然后删除原索引
三、分步骤实现
前言
原索引为nba,其中name中的index为false:index为false的字段是不能作为查询条件的,而此时想通过name字段进行搜索,于是需要修改mapping
{ "nba": { "aliases": { }, "mappings": { "_doc": { "properties": { "jerse_no": { "type": "keyword" }, "name": { "type": "text", "index":"false" }, "play_year": { "type": "keyword" }, "position": { "type": "keyword" }, "team_name": { "type": "text" } } } }, "settings": { "index": { "creation_date": "1621844156710", "number_of_shards": "5", "number_of_replicas": "1", "uuid": "0tNlkuGSSsepjidLaNXFGg", "version": { "created": "6040399" }, "provided_name": "nba2", } } }
1、新建相似index——nba2,nba2中name字段的index为默认值true
{ "nba2": { "aliases": { "nba": {} }, "mappings": { "_doc": { "properties": { "jerse_no": { "type": "keyword" }, "name": { "type": "text" }, "play_year": { "type": "keyword" }, "position": { "type": "keyword" }, "team_name": { "type": "text" } } } }, "settings": { "index": { "creation_date": "1621844156710", "number_of_shards": "5", "number_of_replicas": "1", "uuid": "0tNlkuGSSsepjidLaNXFGg", "version": { "created": "6040399" }, "provided_name": "nba2" } } } }
2、通过_reindex接口将nba中的数据复制到nba2中
POST _reindex { "source": { "index": "nba" }, "dest": { "index": "nba2" } }
3、通过_aliases为新建的索引创建一个原来索引的别名,然后删除原索引
POST /_aliases { "actions" : [ { "add": { "index": "nba2", "alias": "nba" } }, { "remove_index": { "index": "nba" } } ] }