elasticsearch-更新 mapping
- 新增索引字段
- 修改索引字段
- 修改类型
- 修改名称
创建索引mapping
PUT /my-index-000001
{
"mappings": {
"properties": {
"age": { "type": "integer" },
"email": { "type": "keyword" },
"name": { "type": "text" }
}
}
}
//插入数据
POST /my-index-000001/_doc/1
{
"age":1,
"email":"hello@qq.com",
"name":"tom"
}
//查询
GET /my-index-000001/_search
{
"query": {
"match": {
"name": "tom"
}
}
}
//成功返回
新增索引字段
//已存在index中增加索引字段
PUT /my-index-000001/_mapping
{
"properties": {
"address": {
"type": "keyword"
}
}
}
//修改数据
POST /my-index-000001/_doc/1
{
"age":1,
"email":"hello@qq.com",
"name":"tom",
"address":"上海闵行"
}
//查询
GET /my-index-000001/_search
{
"query": {
"match": {
"address": "上海闵行"
}
}
}
//成功返回
修改索引字段
修改字段类型
//将字段 age integer类型改成long类型
PUT /my-index-000001/_mapping
{
"properties": {
"age": {
"type": "long"
}
}
}
//报错
{
"error": {
"root_cause": [
{
"type": "illegal_argument_exception",
"reason": "mapper [age] cannot be changed from type [integer] to [long]"
}
],
"type": "illegal_argument_exception",
"reason": "mapper [age] cannot be changed from type [integer] to [long]"
},
"status": 400
}
解决方法, 使用 reindex
- 重建新索引
- 使用reindex时行数据迁移
//重建新索引
PUT /my-index-000003
{
"mappings": {
"properties": {
"age": { "type": "long" }, //修改为long类型
"email": { "type": "keyword" },
"name": { "type": "text" },
"address": {"type": "text"}
}
}
}
//索引迁移
POST _reindex
{
"source": {
"index": "my-index-000001"
},
"dest": {
"index": "my-index-000003"
}
}
//查看数据,查看mapping
GET /my-index-000003/_doc/1 //成功
GET /my-index-000003/_mapping //成功
修改字段名称
1.使用 reindex
POST _reindex
{
"source": {
"index": "my-index-000001"
},
"dest": {
"index": "my-index-000002"
},
"script": {
"source": "ctx._source.adddressNew = ctx._source.remove(\"address\")"
}
}
//删除原来索引
DELETE /my-index-000001
//查询新索引--> 成功
GET /my-index-000002/_doc/1
{
"_index": "my-index-000002",
"_id": "1",
"_version": 1,
"_seq_no": 0,
"_primary_term": 1,
"found": true,
"_source": {
"name": "tom",
"adddressNew": "上海闵行",
"age": 1,
"email": "hello@qq.com"
}
}
2.使用 alias 字段类型
PUT /my-index-000001/_mapping
{
"properties": {
"addressNew": {
"type": "alias", //使用 alias类型
"path": "address"
}
}
}
GET /my-index-000001/_search
{
"query": {
"match": {
"addressNew": "上海闵行" //也可以查询成功
}
}
}
其他 reindex 操作
1.只创建目标索引中缺少的文档
POST _reindex
{
"source": {
"index": "my-index-000001"
},
"dest": {
"index": "my-index-000004",
"op_type": "create"
}
}
2.设置批次大小
POST _reindex
{
"source": {
"index": "my-index-000001",
"size": 10
},
"dest": {
"index": "my-index-000005"
}
}
3.只同步源index里部分字段
//
POST _reindex
{
"source": {
"index": "my-index-000001",
"_source": ["name", "age"]
},
"dest": {
"index": "my-index-000006"
}
}
GET /my-index-000006/_doc/1
{
"_index": "my-index-000006",
"_id": "1",
"_version": 1,
"_seq_no": 0,
"_primary_term": 1,
"found": true,
"_source": {
"age": 1,
"name": "tom"
}
}
4.屏蔽不需要字段
POST _reindex
{
"source": {
"index": "my-index-000001",
"_source": {
"excludes": ["name"]
}
},
"dest": {
"index": "my-index-000007"
}
}
GET /my-index-000007/_doc/1
{
"_index": "my-index-000007",
"_id": "1",
"_version": 1,
"_seq_no": 0,
"_primary_term": 1,
"found": true,
"_source": {
"age": 1,
"email": "hello@qq.com",
"address": "上海闵行"
}
}