Elasticsearch 增加字段,并设置默认值

1.添加字段

PUT test_idx/test/_mapping

##设置一级属性
{
  "properties": {
    "bookNum": {
      "type":"long"
    }
  }
}

## 设置子对象的属性
{
  "properties": {
    "obj.bookNum": {
      "type":"long"
    }
  }
}

备注:
test_idx: 索引名称
test: 索引中 mappings 的下一级对象
bookNum: 需要添加的属性
obj1: 索引中的子对象

响应如下才能说明新增成功

{
    "acknowledged": true
}

2.给新增的字段增加默认值

POST book/test/_update_by_query

## 一级属性设置默认值
{
  "script":{
    "lang":"painless",
    "source":"if (ctx._source.bookNum == null) {ctx._source.bookNum = 0}"
  }
}

## 子对象中设置默认值
{
  "script":{
    "lang":"painless",
    "source":"if (ctx._source.obj1.is_empty == null) {ctx._source.obj1.is_empty = 0}"
  }
}

© 版权声明
THE END
如果内容对您有所帮助,就支持一下吧!
点赞0 分享
评论 抢沙发

请登录后发表评论

    暂无评论内容