更改多个文档字段在ES 2.3.3中不起作用

时间:2016-08-18 11:33:36

标签: elasticsearch

我有一份文件:

"_index": "boe_bpm",
"_type": "document",
"_id": "3215951",
"_version": 1,
"_source": {
    "title": "aaaa",
    "process": {
        "tasks": [{
                "class": "value1",
                "id": 1
            }, {
                "class": "value1",
                "id": 2
            },

            ... {
                "class": "value1",
                "id": 1000
            }

        ]
    }
}

我想更改process/tasks的值,但失败了,我的代码:

    List ll = new ArrayList();
        for (int i = 0; i < 1000; i++) {
            ll.add(i);
        }

    params.put("ids", ll);
    params.put("classParam", "xxxxzzzaaa");
    client.prepareUpdate("boe_bpm", "document", "3215951").setScript(new Script(
                        "def items = ctx._source.items.findAll{ it.id in ids}; if (items) { for(int i=0; i<items.size(); i++) { items[i]['class']=classParam; } }",
                        ScriptType.INLINE, null, params))
                .get();

没有错误或异常信息。

为什么它不起作用?如何更改我的代码?

1 个答案:

答案 0 :(得分:1)

根据您上面的示例文档,ctx._source.items应为ctx._source.process.tasks,对吧?

如果你像这样充分利用Groovy,你的脚本可以简单得多:

ctx._source.process.tasks.findAll { it.id < 1000 }.each { it['class'] = classParam }

因此,您的更新代码将为:

Map<String, Object> params = new HashMap<>();
params.put("classParam", "xxxxzzzaaa");
String script = "ctx._source.process.tasks.findAll { it.id < 1000 }.each { it['class'] = classParam }";
client.prepareUpdate("boe_bpm", "document", "3215951")
  .setScript(new Script(script, ScriptType.INLINE, null, params))
            .get();