【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