繁体   English   中英

在Elasticsearch中更新多层嵌套文档

[英]Update multi level nested document in elasticsearch

使用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"  
}

我想在questions.tableQuestion.rows中添加一个新对象。 我当前的脚本是用新对象替换现有对象。 请建议如何附加它。 以下是我的更新脚本。

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

您可以在rows属性的右侧,使用下一个嵌套字段构建路径,然后使用+ =运算符。 检查rows数组是否为空并在这种情况下将其初始化也很好。

已通过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"
                            }
                        }
        }
    }
}

对于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" : {
                            ...
            }
        }
    }
}

更新至其他评论

如果路径的某些部分是动态的,则还可以使用参数来构建路径-使用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