美文网首页
Elasticsearch 增加字段,并设置默认值

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

作者: thinking2019 | 来源:发表于2021-08-20 10:32 被阅读0次

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}"
  }
}

相关文章

网友评论

      本文标题:Elasticsearch 增加字段,并设置默认值

      本文链接:https://www.haomeiwen.com/subject/akttiltx.html