首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何更新Elasticsearch/ AWS Opensearch中嵌套的数字字段值?

如何更新Elasticsearch/ AWS Opensearch中嵌套的数字字段值?
EN

Stack Overflow用户
提问于 2021-11-12 13:38:02
回答 1查看 494关注 0票数 0

我对ES不太熟悉,所以我可能犯了一些错误。

我有一个具有以下映射的索引users

代码语言:javascript
运行
复制
{
   "mappings":{
      "dynamic":false,
      "properties":{
         "id":{
            "type":"keyword"
         },
         "userId":{
            "type":"text"
         },
         "total":{
            "type":"float"
         },
         "count":{
            "type":"float"
         },
         "earnings":{
            "properties":{
               "potential":{
                  "properties":{
                     "pending":{
                        "type":"float"
                     },
                     "finished":{
                        "type":"float"
                     }
                  }
               },
               "completed":{
                  "properties":{
                     "pending":{
                        "type":"float"
                     },
                     "paid":{
                        "type":"float"
                     }
                  }
               }
            }
         }
      }
   }
}

我想使用update更新嵌套字段,比如earnings.potential.pending。我有一个类似于下面使用Update的脚本。

代码语言:javascript
运行
复制
{
  "script" : {
    "source": "ctx._source.earnings.potential.pending += params.total",
    "lang": "painless",
    "params" : {
      "total" : 4
    }
  }
}

然而,我在illegal_argument_exception外,点符号是假定的问题如下所示。

代码语言:javascript
运行
复制
"ctx._source.earnings.potential.pending += params.total",
            ^---- HERE'
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-11-12 14:02:26

如果要更新的文档中的earnings为null,则会得到一个错误:

代码语言:javascript
运行
复制
...
"caused_by": {
  "type": "null_pointer_exception",
  "reason": "cannot access method/field [potential] from a null def reference"
}

因此,本质上,如果字段为null,则需要在字段上设置默认值。最简单的方法是在索引时执行该操作,或者运行一次查询来在任何地方设置默认值。但是,如果它不是选项,则可以扩展脚本以检查和分配默认设置:

代码语言:javascript
运行
复制
{
    "script": {
        "lang": "painless",
        "source": "if(ctx._source.earnings == null) { ctx._source.earnings = [:] }\n if(ctx._source.earnings.potential == null) { ctx._source.earnings.potential = [:] }\n if(ctx._source.earnings.potential.pending == null) { ctx._source.earnings.potential.pending = 0 } \n ctx._source.earnings.potential.pending += params.total",
        "params": {
            "total": 5
        }
    }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/69943778

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档