我在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] 删除。
我来说两句