Elasticsearch 5.4.0 - 如何向现有文档添加新字段
Elasticsearch 5.4.0 - How to add new field to existing document
在生产环境中,我们已经有 2000 多个文档。我们需要将新字段添加到现有文档中。是否可以添加新字段?如何将新字段添加到现有字段
您可以使用 update by query API 向所有现有文档添加新字段:
POST your_index/_update_by_query
{
"query": {
"match_all": {}
},
"script": {
"inline": "ctx._source.new_field = 0",
"lang": "painless"
}
}
注意:如果您的新字段是字符串,请将 0
改为 ''
我们也可以使用 curl 添加新字段,并直接在终端中 运行 以下命令。
curl -X PUT "localhost:9200/you_index/_mapping/defined_mapping" -H 'Content-Type: application/json' -d '{ "properties":{"field_name" : {"type" : type_of_data}} }'
在生产环境中,我们已经有 2000 多个文档。我们需要将新字段添加到现有文档中。是否可以添加新字段?如何将新字段添加到现有字段
您可以使用 update by query API 向所有现有文档添加新字段:
POST your_index/_update_by_query
{
"query": {
"match_all": {}
},
"script": {
"inline": "ctx._source.new_field = 0",
"lang": "painless"
}
}
注意:如果您的新字段是字符串,请将 0
改为 ''
我们也可以使用 curl 添加新字段,并直接在终端中 运行 以下命令。
curl -X PUT "localhost:9200/you_index/_mapping/defined_mapping" -H 'Content-Type: application/json' -d '{ "properties":{"field_name" : {"type" : type_of_data}} }'