Search code examples
elasticsearchelasticsearch-jest

Elasticsearch partial update not happening while using updateBuilder


I have the following document in elastic search:

   {
      "postDate": "2016-03-09T11:57:37+0530",
      "message": "trying out Elasticsearch",
      "user": "ankita",
      "tags": [
        "testing"
      ]
    }

And i am trying to update it using jestHttpClient with following code:

 private static void updateDocument(JestClient client, String id) {


    String script = "{\n" +
            "    \"script\" : \"ctx._source.tags += tag\",\n" +
            "    \"params\" : {\n" +
            "        \"tag\" : \"blue\"\n" +
            "    }\n" +
            "}";
    //String script ="{ \"script\" : \"ctx._source.newfield = \"something\"\"}";
    try {
        Update update=new Update.Builder(script).index("article").type("type").id(id).build();
        client.execute(update);
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

}

But it is not updating the document , not sure what is wrong here,

Is there a way where we can partially update a document in elasticsearch?


Solution

  • You need to make sure that you have enabled dynamic scripting in your elasticsearch.yml configuration file.

    Since you have installed ES using brew, you can normally find that configuration file at /usr/local/Cellar/elasticsearch/2.2.0/config/elasticsearch.yml

    Simply append the following line to your file and restart ES:

    script.inline: true
    

    Your update script should work after that.