フィールドの追加
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
サポートされているマッピングパラメータを除き、既存のフィールドのマッピングやフィールドタイプを変更することはできません。既存のフィールドを変更すると、既にインデックスされたデータが無効になる可能性があります。
他のインデックスのフィールドのマッピングを変更する必要がある場合は、正しいマッピングで新しいインデックスを作成し、そのインデックスにデータを再インデックスしてください。
ドキュメント:https://www.elastic.co/guide/en/elasticsearch/reference/7.13/docs-reindex.html