Isu*_*ana 4 json elasticsearch elasticsearch-5
我需要更新 Elasticsearch 索引中的几个文档,我使用 _update_by_query 插件尝试了以下操作。
我需要做的是在匹配特定条件的几个现有文档中添加一个新字段。新字段是一个嵌套的 JSON。所以在添加它之后文档源应该看起来像
_source: {
...existing fields,
"new_field" : {
"attrName1" : "value",
"attrName2" : "value",
}
}
Run Code Online (Sandbox Code Playgroud)
我尝试使用 _update_by_query API 来完成这项工作。但到目前为止,我只能用它添加字符串字段和数组。当尝试使用以下查询添加 JSON 时,它给了我一个错误。
询问
curl -XPOST "http://xxx.xxx.xxx.xxx:pppp/my_index_name/_update_by_query" -d'
{
"query": {
"bool": {
"must": [
{
"term": {
"team.keyword": "search_phrase"
}
}
]
}
},
"script" : {
"inline":"ctx._source.field_name = {\"a\":\"b\"}"
}
}'
Run Code Online (Sandbox Code Playgroud)
错误
{
"error": {
"root_cause": [
{
"type": "script_exception",
"reason": "compile error",
"script_stack": [
"ctx._source.field_name = {\"a\":\"b\"}",
" ^---- HERE"
],
"script": "ctx._source.field_name = {\"a\":\"b\"}",
"lang": "painless"
}
],
"type": "script_exception",
"reason": "compile error",
"caused_by": {
"type": "illegal_argument_exception",
"reason": "invalid sequence of tokens near ['{'].",
"caused_by": {
"type": "no_viable_alt_exception",
"reason": null
}
},
"script_stack": [
"ctx._source.field_name = {\"a\":\"b\"}",
" ^---- HERE"
],
"script": "ctx._source.field_name = {\"a\":\"b\"}",
"lang": "painless"
},
"status": 500
}
Run Code Online (Sandbox Code Playgroud)
到目前为止,我只能将字符串添加为新字段。实现这一目标的正确方法是什么?
小智 6
使用 params 来实现相同的效果,而不是直接赋值。
{
"query": {
"bool": {
"must": [
{
"term": {
"team.keyword": "search_phrase"
}
}
]
}
},
"script": {
"inline": "ctx._source.field_name = params.new_field",
"params": {
"new_field": {
"a": "b"
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1824 次 |
| 最近记录: |