弹性搜索部分更新到嵌套文档

时间:2017-06-22 11:43:25

标签: elasticsearch

我有以下文件:

{
            {
                "_index": "demo_processinglog",
                "_type": "status",
                "_id": "1498130124024a",
                "_score": 1,
                "_source": {
                    "startTime": 1411230604024,
                    "clientName": "fastlog",
                    "projectName": "demo",
                    "sourcePath": "/nfs/clients/client1/projects/project1/",
                    "sourceFiles": [
                        {
                            "sourceFile": "/nfs/clients/client1/projects/project1/Readme.txt",
                            "fileSize": 3563
                            "status": "123"
                        },
                        {
                            "sourceFile": "/nfs/clients/client1/projects/project1/XML/data.xml",
                            "fileSize": 51893940,
                            "status": 234
                        },
                        {
                            "sourceFile": "/nfs/clients/client1/projects/project1/XML/data2.xml",
                            "fileSize": 1665,
                            "status": 345
                        },
                        {
                            "sourceFile": "/nfs/clients/client1/projects/project1/XML/data3.xml",
                            "fileSize": 5799680,
                            "status": 456
                        }
                    ]
                }
            }

我想要做的是更新特定源文件的状态字段。我尝试使用“_update”端点进行更新,但是如果我尝试更新源文件的“status”字段:

“sourceFile”:“/ nfs / client / client1 / project / project1 / Readme.txt”

删除“sourceFiles”数组中的所有其他内容。如何仅更新特定“sourceFile”的状态字段并保留其他数据?我还尝试使用“doc_as_upsert”选项进行更新,但仍会删除其他所有内容。

更新

我刚刚绑定了以下脚本,但是没有更新任何内容:

{
    "script": "for ( int i = 0; i < ctx._source.sourceFiles.size(); i ++ ) {if ( ctx._source.sourceFiles[ i ].fileName == params.sourceFile) {ctx._source.sourceFiles[ i ].status = params.status;} }",
    "lang": "painless",
    "params": {
            "status": "completed",
            "sourceFile": "/nfs/clients/client1/projects/project1/Readme.txt"
    }
}

谢谢!

1 个答案:

答案 0 :(得分:1)

所以,脚本完全符合我的要求,我只需要使用&#34; inline&#34;来调用它,所以最终的工作脚本是:

{
    "script":{
"inline":  "for ( int i = 0; i < ctx._source.sourceFiles.size(); i ++ ) {if ( ctx._source.sourceFiles[ i ].fileName == params.sourceFile) {ctx._source.sourceFiles[ i ].status = params.status;} }",
    "lang": "painless",
    "params": {
            "status": "completed",
            "sourceFile": "/nfs/clients/client1/projects/project1/Readme.txt"
    }
  }
}