# 创建索引 index1v1
curl -X PUT http://localhost:9200/index1v1?pretty
# 给索引 index1v1 创建别名 index1,做增删改查使用别名
# 好处是 如果想要更改_mapping,可以通过重建索引的方式,不停机即可实现更改
curl -X PUT "http://localhost:9200/_alias?pretty" -H 'Content-Type: application/json' -d'
{
"actions": [
{
"add": {
"index": "index1v1",
"alias": "index1"
}
}
]
}
'
# 给索引index1的文档 book1 设置 mapping,这里给publishDate建text类型
# 之后演示重建索引重新设置mapping,将publishDate更改为date类型
curl -X POST "http://localhost:9200/index1/book1/_mapping?pretty" -H 'Content-Type: application/json' -d'
{
"book1": {
"properties": {
"bookId": {
"type": "long"
},
"bookName": {
"type": "text"
},
"publishDate": {
"type": "text"
}
}
}
}
'
# 插入数据
curl -X PUT "http://localhost:9200/index1/book1/1?pretty" -H 'Content-Type: application/json' -d'
{
"bookId":1,
"bookName":"Java程序设计",
"publishDate":"2018-01-12"
}
'
# 重建索引 步骤1
# 重建索引前 先建立 index1v2,设置最终的mapping后,再执行 重建索引
curl -X PUT http://localhost:9200/index1v2?pretty
curl -H 'Content-Type: application/json' -X POST "http://localhost:9200/index1v2/book1/_mapping?pretty" -d'
{
"book1": {
"properties": {
"bookId": {
"type": "long"
},
"bookName": {
"type": "text"
},
"publishDate": {
"type": "date"
}
}
}
}
'
# 重建索引 步骤2
# 重建索引 数据从index1v1索引重建到index1v2索引 文档 book
# wait_for_completion=false 将不等待重建完成,接口返回 taskid
# 使用 GET _tasks/{taskID} 查看重建进度
curl -X POST "http://localhost:9200/_reindex?wait_for_completion=false" -H 'Content-Type: application/json' -d'
{
"source": { "index": "index1v1", "type": "book1" },
"dest": { "index": "index1v2", "type": "book1" }
}
'
# 重建索引 步骤3
# 切索引别名
curl -X POST "http://localhost:9200/_aliases?pretty" -H 'Content-Type: application/json' -d'
{
"actions": [
{ "remove": { "index": "index1v1", "alias": "index1" }},
{ "add": { "index": "index1v2", "alias": "index1" }}
]
}
'
# 重建索引 步骤4
# 删除原index1v1索引
curl -X DELETE "http://localhost:9200/index1v1?pretty"