前言
es不能修改mapping
是不是感觉要凉了 哈哈 别着急 还有救
不能改 但是我们可以新建自己想要修改的mapping 通过一系列的骚操作 可以大致实现你想修改的效果,下面开始吧
一、案例展示前期准备
1.先创建一个索引 作为原来的mapping
代码如下(示例):
PUT blog
2.新建文档写入数据
代码如下(示例):
PUT blog/article/1
{
"id":"1",
"brief":"博客2简介",
"pubTime": 1624006061026,
"searchContent":"博客1简介 博客1标题",
"title":"博客1标题"
}
PUT blog/article/2
{
"id":"2",
"brief":"博客2简介",
"pubTime": 1624006061026,
"searchContent":"博客2简介 博客2标题",
"title":"博客2标题"
}
3.查看mapping
代码如下(示例):
GET /blog/_mapping
查询结果
{
"blog" : {
"mappings" : {
"article" : {
"properties" : {
"brief" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
},
"id" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
},
"pubTime" : {
"type" : "long"
},
"searchContent" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
},
"title" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
}
}
}
}
}
}
2.查看已写入的数据
GET /blog/article/_search
{
"query": {
"match_all": {}
}
}
查询结果
{
"took" : 0,
"timed_out" : false,
"_shards" : {
"total" : 5,
"successful" : 5,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : 2,
"max_score" : 1.0,
"hits" : [
{
"_index" : "blog",
"_type" : "article",
"_id" : "2",
"_score" : 1.0,
"_source" : {
"id" : "2",
"brief" : "博客2简介",
"pubTime" : 1624006061026,
"searchContent" : "博客2简介 博客2标题",
"title" : "博客2标题"
}
},
{
"_index" : "blog",
"_type" : "article",
"_id" : "1",
"_score" : 1.0,
"_source" : {
"id" : "1",
"brief" : "博客1简介",
"pubTime" : 1624006061026,
"searchContent" : "博客1简介 博客1标题",
"title" : "博客1标题"
}
}
]
}
}
二、开始修改
1.创建一个新的索引备份一下 待修改索引的数据
PUT /blog_bak
执行结果
{
"acknowledged" : true,
"shards_acknowledged" : true,
"index" : "blog_bak"
}
2.备份数据
POST _reindex
{
"source": {
"index": "blog"
},
"dest": {
"index": "blog_bak"
}
}
执行结果
{
"took" : 37,
"timed_out" : false,
"total" : 2,
"updated" : 0,
"created" : 2,
"deleted" : 0,
"batches" : 1,
"version_conflicts" : 0,
"noops" : 0,
"retries" : {
"bulk" : 0,
"search" : 0
},
"throttled_millis" : 0,
"requests_per_second" : -1.0,
"throttled_until_millis" : 0,
"failures" : [ ]
}
3.查看备份数据
GET /blog_bak/article/_search
{
"query": {
"match_all": {}
}
}
执行结果
{
"took" : 0,
"timed_out" : false,
"_shards" : {
"total" : 5,
"successful" : 5,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : 2,
"max_score" : 1.0,
"hits" : [
{
"_index" : "blog_bak",
"_type" : "article",
"_id" : "2",
"_score" : 1.0,
"_source" : {
"id" : "2",
"brief" : "博客2简介",
"pubTime" : 1624006061026,
"searchContent" : "博客2简介 博客2标题",
"title" : "博客2标题"
}
},
{
"_index" : "blog_bak",
"_type" : "article",
"_id" : "1",
"_score" : 1.0,
"_source" : {
"id" : "1",
"brief" : "博客1简介",
"pubTime" : 1624006061026,
"searchContent" : "博客1简介 博客1标题",
"title" : "博客1标题"
}
}
]
}
}
3.数据已经成功备份,这时候我们可以放心的把原来的待修改索引删除
DELETE blog
执行结果
{
"acknowledged" : true
}
4.再重新创建blog索引
比如我想给搜索字段seacrchContent增加ik分词,于是我将blog设置成如下
PUT blog
{
"mappings" : {
"article" : {
"properties" : {
"brief" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
},
"id" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
},
"pubTime" : {
"type" : "long"
},
"searchContent" : {
"type" : "text",
"analyzer" : "ik_max_word"
},
"title" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
},
"author" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
}
}
}
}
}
执行结果
{
"acknowledged" : true,
"shards_acknowledged" : true,
"index" : "blog"
}
5.在将原来备份的数据重新导回来
POST _reindex
{
"source": {
"index": "blog_bak"
},
"dest": {
"index": "blog"
}
}
执行结果
{
"took" : 7,
"timed_out" : false,
"total" : 2,
"updated" : 0,
"created" : 2,
"deleted" : 0,
"batches" : 1,
"version_conflicts" : 0,
"noops" : 0,
"retries" : {
"bulk" : 0,
"search" : 0
},
"throttled_millis" : 0,
"requests_per_second" : -1.0,
"throttled_until_millis" : 0,
"failures" : [ ]
}
6.查看当前修改后的mapping
GET /blog/_mapping
执行结果
{
"blog" : {
"mappings" : {
"article" : {
"properties" : {
"author" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
},
"brief" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
},
"id" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
},
"pubTime" : {
"type" : "long"
},
"pv" : {
"type" : "long"
},
"searchContent" : {
"type" : "text",
"analyzer" : "ik_max_word"
},
"title" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
}
}
}
}
}
}
7.查询该索引的数据
GET /blog/article/_search
{
"query": {
"match_all": {}
}
}
执行结果
{
"took" : 0,
"timed_out" : false,
"_shards" : {
"total" : 5,
"successful" : 5,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : 2,
"max_score" : 1.0,
"hits" : [
{
"_index" : "blog",
"_type" : "article",
"_id" : "2",
"_score" : 1.0,
"_source" : {
"id" : "2",
"brief" : "博客2简介",
"pubTime" : 1624006061026,
"searchContent" : "博客2简介 博客2标题",
"title" : "博客2标题"
}
},
{
"_index" : "blog",
"_type" : "article",
"_id" : "1",
"_score" : 1.0,
"_source" : {
"id" : "1",
"brief" : "博客1简介",
"pubTime" : 1624006061026,
"searchContent" : "博客1简介 博客1标题",
"title" : "博客1标题"
}
}
]
}
}
如此成功修改了searchContent的分词,也未丢失数据 。看完你也懂了,虽然不能修改
但是我们可以备份数据,删了原来的索引,再重新创建一个满足自己开发需求的索引,最后将数据重新导回来。