【ElasticSearch】索引(更新)
增加字段
REST API
文档:https://www.elastic.co/guide/en/elasticsearch/reference/7.13/docs-reindex.html
准备一个空索引myindex
PUT /myindex
{
"settings": {
"number_of_shards": 1,
"number_of_replicas": 1
}
}
新追加一个字段
PUT /myindex/_mapping
{
"properties": {
"name": {
"type": "keyword"
}
}
}
终端
curl -XPOST http://localhost:9200/myindex/_mapping -H 'Content-Type:application/json' -d '
{
"properties": {
"name": {
"type": "keyword"
}
}
}
'
更新 ignore_above
PUT /myindex/_mapping
{
"properties": {
"name": {
"type": "keyword",
"ignore_above": 500
}
}
}
Java REST Client
文档:https://www.elastic.co/guide/en/elasticsearch/client/java-rest/7.13/java-rest-high-put-mapping.html
PutMappingRequest request = new PutMappingRequest("myindex");
Map<String, Object> mapping = new HashMap<>();
Map<String, Object> properties = new HashMap<>();
Map<String, Object> name = new HashMap<>();
name.put("type", "keyword");
properties.put("name", name);
mapping.put("properties", properties);
request.source(mapping);
AcknowledgedResponse putMappingResponse = restHighLevelClient.indices().putMapping(request, RequestOptions.DEFAULT);
return putMappingResponse.isAcknowledged();
重命名字段
1、通过 alias
REST API
PUT /myindex/_mapping
{
"properties": {
"title": {
"type": "alias",
"path": "name"
}
}
}
Java REST Client
PutMappingRequest request = new PutMappingRequest("myindex");
Map<String, Object> mapping = new HashMap<>();
Map<String, Object> properties = new HashMap<>();
Map<String, Object> title = new HashMap<>();
title.put("path", "name");
title.put("type", "alias");
properties.put("title", title);
mapping.put("properties", properties);
request.source(mapping);
AcknowledgedResponse putMappingResponse = restHighLevelClient.indices().putMapping(request, RequestOptions.DEFAULT);
result putMappingResponse.isAcknowledged();
2、通过 _reindex
文档:https://www.elastic.co/guide/en/elasticsearch/reference/7.13/docs-reindex.html
准备数据
PUT /mysrc
{
"settings": {
"number_of_shards": 1,
"number_of_replicas": 1
},
"mappings": {
"properties": {
"name": {
"type": "keyword",
"ignore_above": 100
},
"age": {
"type": "integer"
}
}
}
}
PUT mydest
{
"settings": {
"number_of_shards": 1,
"number_of_replicas": 1
},
"mappings": {
"properties": {
"name": {
"type": "keyword",
"ignore_above": 100
},
"score": {
"type": "integer"
}
}
}
}
PUT mysrc/_create/1
{
"name": "张三",
"age": 20
}
PUT mysrc/_doc/2
{
"name": "李四",
"age": 25
}
REST API
将 age 重新命名为 score,即 mydest 中 score 的值来自 age
POST _reindex
{
"source": {
"index": "mysrc"
},
"dest": {
"index": "mydest"
},
"script": {
"source": "ctx._source.score = ctx._source.remove(\"age\")"
}
}
Java REST Client
删除字段
通过 _reindex
准备数据
PUT /mysrc
{
"settings": {
"number_of_shards": 1,
"number_of_replicas": 1
},
"mappings": {
"properties": {
"name": {
"type": "keyword",
"ignore_above": 100
},
"sex": {
"type": "keyword"
},
"age": {
"type": "integer"
}
}
}
}
PUT mydest
{
"settings": {
"number_of_shards": 1,
"number_of_replicas": 1
},
"mappings": {
"properties": {
"name": {
"type": "keyword",
"ignore_above": 100
}
}
}
}
PUT mysrc/_create/1
{
"name": "Bob",
"sex": "male",
"age": 20
}
PUT mysrc/_doc/2
{
"name": "Alice",
"sex": "female",
"age": 25
}
REST API
将 age 字段删除,即 mydest 中没有 age
POST _reindex
{
"source": {
"index": "mysrc"
},
"dest": {
"index": "mydest"
},
"script": {
"source": "ctx._source.remove(\"age\")"
}
}
删除多个字段
POST _reindex
{
"source": {
"index": "mysrc"
},
"dest": {
"index": "mydest"
},
"script": {
"source": "ctx._source.remove(\"age\"); ctx._source.remove(\"sex\");"
}
}
Java REST Client
注意:已有索引映射是无法直接修改
文档:https://www.elastic.co/guide/en/elasticsearch/reference/7.13/indices-put-mapping.html
Except for supported mapping parameters, you can’t change the mapping or field type of an existing field. Changing an existing field could invalidate data that’s already indexed
除了支持的映射参数外,您不能更改现有字段的映射或字段类型。更改现有字段可能会使已编制索引的数据无效。
If you need to change the mapping of a field in other indices, create a new index with the correct mapping and reindex your data into that index
如果需要更改其他索引中字段的映射,请使用正确的映射创建一个新索引,并将数据重新索引到该索引中
文档:https://www.elastic.co/guide/en/elasticsearch/reference/7.13/docs-reindex.html
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南