简体   繁体   English

在Elasticsearch中更新多层嵌套文档

[英]Update multi level nested document in elasticsearch

Using Elasticsearch 1.7.1, I have the following document structure 使用Elasticsearch 1.7.1,我具有以下文档结构

"_source" : {
"questions" : {
    "defaultQuestion" : {           
        "tag" : 0,
        "gid" : 0,
        "rid" : 0,
        "caption" : "SRID",         
    },
    "tableQuestion" : {
        "rows" : [{
                "ids" : {                       
                    "answerList" : ["3547", "3548"],                        
                    "tag" : "0",
                    "caption" : "Accounts",                     
                },
                "name" : {                      
                    "answerList" : ["Some Name"],                       
                    "tag" : "0",
                    "caption" : "Name",                     
                }
            }
        ],
        "caption" : "BPI 1500541753537",
        "id" : 644251570,
        "tag" : ""
    }
},
"id" : "447722821"  
}

I want to add a new object in in questions.tableQuestion.rows. 我想在questions.tableQuestion.rows中添加一个新对象。 My current script is replacing the existing object with the new one. 我当前的脚本是用新对象替换现有对象。 Kindly suggest how to append it instead. 请建议如何附加它。 Following is my update script. 以下是我的更新脚本。

{ "update": {"_id": "935663867", "_retry_on_conflict" : 3} }
{ "script" : "ctx._source.questions += param1", "params" : {"param1" : {"tableQuestion": {"rows" : [ NEWROWOBJECT ]}  } }}

You can build the path with next nested fields, right to the rows property and then use += operator. 您可以在rows属性的右侧,使用下一个嵌套字段构建路径,然后使用+ =运算符。 It's also good to have a check if rows array is null and initialize it in this case. 检查rows数组是否为空并在这种情况下将其初始化也很好。

Checked with ES 2.4, but should be similar for earlier versions: 已通过ES 2.4进行检查,但对于早期版本应与之类似:

POST http://127.0.0.1:9200/sample/demo/{document_id}/_update

{ 
    "script": {
        "inline": "if (ctx._source.questions.tableQuestion.rows == null) ctx._source.questions.tableQuestion.rows = new ArrayList(); ctx._source.questions.tableQuestion.rows += param1;",
        "params" : {
            "param1" : {
                            "ids": {
                                "answerList": [
                                    "478",
                                    "255"
                                ],
                                "tag": "2",
                                "caption": "My Test"
                            },
                            "name": {
                                "answerList": [
                                    "My Name"
                                ],
                                "tag": "1",
                                "caption": "My Demo"
                            }
                        }
        }
    }
}

For ES 5.x and Painless language the script is a bit different: 对于ES 5.x和无痛语言,脚本略有不同:

POST http://127.0.0.1:9200/sample/demo/{document_id}/_update

{ 
    "script": {
        "inline": "if (ctx._source.questions.tableQuestion.rows == null) { ctx._source.questions.tableQuestion.rows = new ArrayList();} ctx._source.questions.tableQuestion.rows.add(params.param1);",
        "params" : {
            "param1" : {
                            ...
            }
        }
    }
}

Update to the additional comment 更新至其他评论

If some part of the path is dynamic, you can also use parameters to build the path - with get(param_name) method - try this syntax (I removed the null check for simplicity): 如果路径的某些部分是动态的,则还可以使用参数来构建路径-使用get(param_name)方法-尝试以下语法(为简单起见,我删除了null检查):

{ 
    "script": {
        "inline": "ctx._source.questions.get(param2).rows += param1;",
        "params" : {
            "param2" : "6105243",
            "param1" : {
                            "ids": {
                                "answerList": [
                                    "478",
                                    "255"
                                ],
                                "tag": "2",
                                "caption": "My Test"
                            },
                            "name": {
                                "answerList": [
                                    "My Name"
                                ],
                                "tag": "1",
                                "caption": "My Demo"
                            }
                        }
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM