带参数的Elasticsearch Java HighLevel客户端更新脚本

时间:2019-01-13 19:39:39

标签: java elasticsearch

我想通过在用户列表中添加一个元素来进行部分更新:

"users" : [
            {
              "password" : "pwd",
              "level" : "admin",
              "user_name" : "XX",
              "last_name" : "YY",
              "first_name" : "ZZ"
            }
]

我已使用Kibana尝试了此请求:

POST my_index/user/1/_update
{
    "script" : {
        "source": "ctx._source.users.add(params.user)",
        "lang": "painless",
        "params" : {
          "user": {
            "user_name" : "user",
            "first_name" : "XX",
            "last_name" : "XX",
            "level" : "simple",
            "password" : "pwd!#"
          }
        }
    }
}

工作正常:

 "users" : [
                {
                  "password" : "pwd",
                  "level" : "admin",
                  "user_name" : "XX",
                  "last_name" : "YY",
                  "first_name" : "ZZ"
                },
               {
              "password" : "pwd!#",
              "level" : "simple",
              "user_name" : "user",
              "last_name" : "XX",
              "first_name" : "XX"
            }
    ]

我想使用Java客户端来做,我已经尝试了以下代码:

XContentBuilder builder = XContentFactory.jsonBuilder();
            builder.startObject();
            {
                 builder.field("user_name", user.getUserName());
                 builder.field("first_name", user.getFirstName());
                 builder.field("last_name", user.getLastName());
                 builder.field("level", user.getLevel());
                 builder.field("password", user.getPassword());
            }
            builder.endObject();
            params.put("user", Strings.toString(builder));

UpdateByQueryRequest setScript = request.setScript(
                new Script(
                    ScriptType.INLINE, "painless",
                    "ctx._source.users.add(params)",                    
                    params)); 
        request.setScroll(TimeValue.timeValueMinutes(10));

 BulkByScrollResponse bulkResponse = restHighLevelClient.updateByQuery(request, RequestOptions.DEFAULT);

结果,我有:

"users" : [
            {
              "password" : "pwd",
              "level" : "admin",
              "user_name" : "XX",
              "last_name" : "YY",
              "first_name" : "ZZ"
            },
            {
              "user" : """{"user_name":"XX","first_name":"XX","last_name":"XX","level":"XX","password":"XX"}""",
              "ctx" : {
                "_routing" : null,
                "_parent" : null,
                "_index" : "administration",
                "_type" : "environnement",
                "_id" : "1",
                "_version" : 24
              }
            }
          ]

所以这是不正确的,我只希望将用户信息添加到用户中,并且不想索引ctx信息。

谢谢!

1 个答案:

答案 0 :(得分:1)

有点晚了,但是...您的脚本中有一个错字,您添加了params而不是params.user。

UpdateByQueryRequest setScript = request.setScript(
            new Script(
                ScriptType.INLINE, "painless",
                "ctx._source.users.add(params.user)",    <-- here                
                params)); 
    request.setScroll(TimeValue.timeValueMinutes(10));

无论如何,这可能无法解决问题,我遇到了类似的问题,发现最简单的解决方案是将每个值作为参数进行传递,并在脚本中创建对象一次,这将得出:

parameters.put("user_name", user.getUserName());
parameters.put("first_name", user.getFirstName());
parameters.put("last_name", user.getLastName());
parameters.put("level", user.getLevel());
parameters.put("password", user.getPassword());
request.setScript(new Script(
        ScriptType.INLINE,
        "painless",
        "ctx._source.users.add(params)", 
        parameters));
request.setScroll(TimeValue.timeValueMinutes(10));
BulkByScrollResponse bulkResponse = restHighLevelClient.updateByQuery(request,RequestOptions.DEFAULT);