elasticsearch-更新 mapping

文章详细介绍了如何在Elasticsearch中更新mapping,包括新增索引字段、修改字段类型和名称。当修改字段类型如将integer改为long时,由于Elasticsearch不允许直接修改,需通过reindex进行数据迁移来实现。同样,更改字段名称也需要使用reindex配合脚本操作或alias类型。此外,还展示了其他reindex操作,如创建缺失文档、设置批次大小和选择性同步字段。
摘要由CSDN通过智能技术生成

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": "上海闵行"
  }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值