es通过脚本来进行更新某个List字段

 Map<String, Object> params = new HashMap<String, Object>();
   params ("data", "abc");


 String script =  "if(ctx._source.containsKey('variable')){ \n" +
                        "ctx._source.variable.add(params.data);\n" +
                        " }else{\n" +
                        "ctx._source.variable= [];ctx._source.variable.add(params.data);\n" +
                        " }";

Script script = new Script(ScriptType.INLINE, "painless", script, params);

 UpdateRequest updateRequest = new UpdateRequest(index, id;
            updateRequest.script(script);
            updateRequest.setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE);
            UpdateResponse updateResponse = client.update(updateRequest, RequestOptions.DEFAULT);
if(ctx._source.containsKey('variable')){ 
ctx._source.variable.add(params.data);
 }else{
ctx._source.variable = [];ctx._source.variable.add(params.data);
 }

或者这样写:

    @Autowired
    RestHighLevelClient restHighLevelClient ;
 Map<String, Object> params = new HashMap<>();
                params.put("id", "id");
 Script updateScript = new Script(Script.DEFAULT_SCRIPT_TYPE, Script.DEFAULT_SCRIPT_LANG,
                                "ctx._source.id_column=ctx._source.id_column==null?[]:ctx._source.id_column;ctx._source.id_column.add(params.id)", params);
                        updateRequest.script(updateScript );
                        restHighLevelClient .update(updateRequest, RequestOptions.DEFAULT);

你可能感兴趣的:(elasticsearch,服务器,java)