【问题标题】:ElasticSearch script to update if the value not exist如果值不存在,则 ElasticSearch 脚本进行更新
【发布时间】:2016-01-25 04:50:29
【问题描述】:

我正在尝试使用 Java 更新 es 文档。 我的文档如下

"_source": {

    "gender": "male" ,
    "names": ["name1"]

}

我需要在names 列表中添加更多名称。但我不想重复。 如何在不重复值的情况下更新 ES 文档中的数组?

我尝试过这样的事情。但它不起作用。

client.prepareUpdate(index,type,id)
         .addScriptParam("newobject", "newName")
        .setScript("ctx._source.names.contains(newobject) ? ctx.op = \"none\" :  ctx._source.names+=newobject ").execute().actionGet(); 

【问题讨论】:

    标签: java elasticsearch upsert elasticsearch-plugin


    【解决方案1】:

    这个想法是在结果列表上简单地调用unique()

    client.prepareUpdate(index,type,id)
             .addScriptParam("newobject", "newName")
            .setScript("ctx._source.names+=newobject; ctx._source.names = ctx._source.names.unique(); ").execute().actionGet(); 
    

    此外,要使其正常工作,您需要确保 scripting is enabled

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-09-17
      • 2012-12-17
      • 2019-05-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多