如何在Elastic Search中增加具有动态属性的嵌套对象?

沙巴

我在elasticsearch中有一个看起来像这样的文档:

{
    "_index": "stats",
    "_type": "_doc",
    "_id": "1",
    "_score": 1.0,
    "_source": {
        "publishTime": {
            "lastUpdate": 1580991095131,
            "h0_4": 0,
            "h4_8": 0,
            "h8_12": 3,
            "h12_16": 5,
            "h16_20": 2,
            "h20_24": 1
        },
        "postCategories": {
             "lastUpdate": 1580991095131,
             "tech": 56,
             "lifestyle": 63,
             "healthcare": 49,
             "finances": 25,
         }
     }
 }

通过发送POST请求来更新/增加现有属性值,/stats/_update/1效果很好!但是,如果尝试在下添加一个不存在的属性名称,则会postCategories收到Bad Request (400)类型error的错误remote_transport_exception/illegal_argument_exception

"ctx._source.postCategories.relationships += params.postCategories.relationships",
                                                                  ^---- HERE"

哎呀

{
    "script": {
        "source": "ctx._source.postCategories.relationships += params.postCategories.relationships",
        "lang": "painless",
        "params": {
            "postCategories": {
                "relationships": 2
            }
        }
    },
    "upsert": {
       "postCategories": {
            "relationships": 2
        }
    }
}

我还按照此处的文档尝试了Scripted Upsert方法,但是,发生了相同的错误:

脚本更新

{
    "scripted_upsert":true,
    "script": {
        "source": "ctx._source.postCategories.relationships += params.postCategories.relationships",
        "params": {
            "postCategories": {
                "relationships": 2
            }
        }
    },
    "upsert": {}
}

谁能告诉我如何在postCategories对象下正确添加/添加新的属性名称

谢谢!

多项式质子

基本上是说您正在尝试为不存在的字段分配值。我认为下面应该工作(未经测试)。

尝试检查该字段是否存在-如果存在则继续操作。否则添加新字段并分配值。

"if (ctx._source.postCategories.containsKey(\"relationships\")) { ctx._source.postCategories.relationships += params.postCategories.relationships} else { ctx._source.postCategories[\"relationships\"] = params.postCategories.relationships}",

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何在AWS Elastic Beanstalk中基于延迟配置扩展?

如何在.net中读取Elastic Beanstalk环境属性?

如何在Elastic Beanstalk中运行'rake db:drop'

如何在Elastic Search中进行双嵌套聚合?

如何获取具有嵌套属性的Javascript对象的子集?

如何在Python中模拟具有嵌套属性的对象?

如何在TypeScript中为具有动态属性的对象定义类型

如何在ebextensions脚本中重新引导Elastic Beanstalk实例?

如何在React中映射具有动态键的对象?

如何在Elastic Beanstalk中更新WordPress +插件

如何在Redux中存储具有单独属性的对象

如何在多个对象中插入具有动态值的属性或在多个数组中动态添加元素?

如何在php中动态创建具有计数的对象?

如何在Elastic Search上更改搜索结果的顺序?

如何在Laravel中实现Elastic Search以自动完成

如何在一个Docker撰写文件中包含Elastic Search(具有多个节点)和Kibana?

Elastic Search NEST-如何在搜索中具有多个级别的过滤器

如何在Elastic搜索中处理html编码的文本?

如何在RavenDB中存储具有动态属性的C#对象?

如何在 Elastic Search 中编写条件

如何在嵌套对象中查找属性

如何在VBA中的excel中创建具有文件属性的动态对象列表

如何在 Elastic Search 中获得这个 sql 查询结果

如何在 TEntity 中动态搜索具有值的属性?

如何将字段添加到具有对象值数组的 Elastic Search 文档中?

如何在 JavaScript 中动态获取嵌套对象属性的值?

如何在 Elastic Beanstalk 中更改 http nginx 超时

如何深度复制具有动态添加属性的对象?

如何在打字稿中创建动态嵌套的对象和对象属性