ElasticSearch7.x版本更新映射字段

目录

1、新建索引

请求方式:PUT

请求URL:

2、数据迁移

请求方式:POST

请求URL:

请求体:

3、验证新索引

请求方式:GET

请求URL:

响应体:

4、删除旧索引

请求方式:DELETE

请求URL:

5、新建旧索引

请求方式:PUT

请求URL:

5、再次数据迁移

请求方式:POST

请求URL:

 请求体:

6、再次验证

请求方式:GET

请求URL:

数据验证:

现有索引:testold

{
    "testold": {
        "aliases": {},
        "mappings": {
            "properties": {
                "_class": {
                    "type": "text",
                    "fields": {
                        "keyword": {
                            "type": "keyword",
                            "ignore_above": 256
                        }
                    }
                },
                "id": {
                    "type": "keyword"
                },
                "cotent": {
                    "type": "keyword"
                },
                "tid": {
                    "type": "keyword"
                }
            }
        },
        "settings": {
            "index": {
                "creation_date": "1713842844859",
                "number_of_shards": "1",
                "number_of_replicas": "1",
                "uuid": "YmaRFkrmR-SuD-V95VpmwQ",
                "version": {
                    "created": "7090099"
                },
                "provided_name": "testold"
            }
        }
    }
}

工具人索引:testnew

{
    "mappings": {
        "properties": {
            "cotent": {
                "type": "keyword"
            }, 
            "id": {
                "type": "keyword"
            }, 
            "tid": {
                "type": "long"
            }
        }
    }, 
    "settings": {
        "index": {
            "number_of_shards": 1, 
            "number_of_replicas": 1
        }
    }
}

需要把testold的tid字段修改为long类型。

1、新建索引

请求方式:PUT

请求URL:

http://localhost:9200/testnew

{
    "mappings": {
        "properties": {
            "cotent": {
                "type": "keyword"
            }, 
            "id": {
                "type": "keyword"
            }, 
            "tid": {
                "type": "long"
            }
        }
    }, 
    "settings": {
        "index": {
            "number_of_shards": 1, 
            "number_of_replicas": 1
        }
    }
}

2、数据迁移

请求方式:POST

请求URL:

http://localhost:9200/_reindex

请求体:

{
    "source": {
        "index": "testold"
    }, 
    "dest": {
        "index": "testnew"
    }
}

3、验证新索引

请求方式:GET

请求URL:

http://localhost:9200/testnew/_mapping

响应体:

{
    "testnew": {
        "aliases": { }, 
        "mappings": {
            "properties": {
                "_class": {
                    "type": "text", 
                    "fields": {
                        "keyword": {
                            "type": "keyword", 
                            "ignore_above": 256
                        }
                    }
                }, 
                "id": {
                    "type": "keyword"
                }, 
                "cotent": {
                    "type": "keyword"
                }, 
                "tid": {
                    "type": "long"
                }
            }
        }, 
        "settings": {
            "index": {
                "creation_date": "1713842844859", 
                "number_of_shards": "1", 
                "number_of_replicas": "1", 
                "uuid": "YmaRFkrmR-SuD-V95VpmwQ", 
                "version": {
                    "created": "7090145"
                }, 
                "provided_name": "testnew"
            }
        }
    }
}

4、删除旧索引

请求方式:DELETE

请求URL:

http://localhost:9200/testold

注意:删除旧索引后,需要按照新要求的字段类型进行新建

5、新建旧索引

使用旧的索引名进行新建,以上动作目的是保证数据不丢失。

请求方式:PUT

请求URL:

http://localhost:9200/testold

{
    "mappings": {
        "properties": {
            "cotent": {
                "type": "keyword"
            }, 
            "id": {
                "type": "keyword"
            }, 
            "tid": {
                "type": "long"
            }
        }
    }, 
    "settings": {
        "index": {
            "number_of_shards": 1, 
            "number_of_replicas": 1
        }
    }
}

5、再次数据迁移

请求方式:POST

请求URL:

http://localhost:9200/_reindex

 请求体:

{
    "source": {
        "index": "testnew"
    }, 
    "dest": {
        "index": "testold"
    }
}

6、再次验证

请求方式:GET

请求URL:

http://localhost:9200/testold/_mapping

数据验证:

http://localhost:9200/testold/_search

总结:在整个过程中,testnew只是祈祷一个中间过度的作用,确保在修改字段类型的过程中造成的数据丢失问题。

  • 5
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值