居然是你

elasticsearch不能删除已有index中的字段吧?

Elasticsearch | 作者 tyzuo | 发布于2019年02月22日 | 阅读数:6489

昨天有个同事问我能否删除已有index中的某个字段,好久没用es了,我记得好像不能删除吧?但是另外一个同事说可以删除,不过他也记不得怎么删了,请问是否可以删除已有index中的字段。
已邀请:

rochy - rochy_he

赞同来自: fanmo3yuan tyzuo

可以借助 script 来删除字段
## 修改字段值
POST test/_doc/1/_update
{
"script" : {
"source": "ctx._source.counter += params.count",
"lang": "painless",
"params" : {
"count" : 4
}
}
}

## 删除值的某个元素
POST test/_doc/1/_update
{
"script" : {
"source": "if (ctx._source.tags.contains(params.tag)) { ctx._source.tags.remove(ctx._source.tags.indexOf(params.tag)) }",
"lang": "painless",
"params" : {
"tag" : "blue"
}
}
}

## 添加字段
POST test/_doc/1/_update
{
"script" : "ctx._source.new_field = 'value_of_new_field'"
}

## 删除字段
POST test/_doc/1/_update
{
"script" : "ctx._source.remove('new_field')"
}

laoyang360 - 《一本书讲透Elasticsearch》作者,Elastic认证工程师 [死磕Elasitcsearch]知识星球地址:http://t.cn/RmwM3N9;微信公众号:铭毅天下; 博客:https://elastic.blog.csdn.net

赞同来自:

 
1、增加字段  可以  已验证
2、删除字段 不可以
4、修改字段 不可以
3、修改字段类型 不可以

不过ES有其他方案可以替换,借助reindex,数据量大会有性能问题。 
 

要回复问题请先登录注册