提问要多花一点心思哦

es局部更新文档字段

Elasticsearch | 作者 wuzx | 发布于2023年02月07日 | 阅读数:4917

目前索引mapping如下, 其中deptList字段使用嵌套文档方式{
  "cac_event_v1" : {
    "mappings" : {
      "properties" : {
          "id" : {
                "type" : "keyword"
           },
         "name" : {
                "type" : "keyword"
           },
         "upmanObject" : {  
             "properties" : {...}
          }
          "deptList" : {
               "type" : "nested",
              "properties" : {...}
           }
         }
       }
   }
}
 
需求:实现局部更新name字段或者upmanObject 的值,
 
使用下面的方式单独更新某个字段,nested类型的字段就会更新成空
POST index_cac_event/_update/id值
{
  "doc": {
    "name": "111"
  }
}
 
求教有没能局部更新文档字段的且不影响nested类型的子文档数据,如果每次需要加一个字段都要整个文档同步代价太大了
 
已邀请:

duanxiaobiao - 90 后IT男

赞同来自:

使用script进行局部更新,例如(字段自己调整):
 
{
"query": {
"term": {
"user": "张三"
}
},
"script": {
"source": "ctx._source.DOB = params.DOB",
"params": {
"DOB": 1996-12-01
}
}
}

要回复问题请先登录注册