public void testUpdate() {
    createIndexWithAlias();
    ensureYellow("test");

    UpdateRequestBuilder updateRequestBuilder =
        client()
            .prepareUpdate(indexOrAlias(), "type1", "1")
            .setUpsert("field1", "value1")
            .setDoc("field2", "value2");

    UpdateResponse updateResponse = updateRequestBuilder.get();
    assertThat(updateResponse.getIndex(), equalTo("test"));
    assertThat(updateResponse.getType(), equalTo("type1"));
    assertThat(updateResponse.getId(), equalTo("1"));
    assertThat(updateResponse.isCreated(), equalTo(true));

    GetResponse getResponse = client().prepareGet("test", "type1", "1").get();
    assertThat(getResponse.isExists(), equalTo(true));
    assertThat(getResponse.getSourceAsMap().containsKey("field1"), equalTo(true));
    assertThat(getResponse.getSourceAsMap().containsKey("field2"), equalTo(false));

    updateResponse = updateRequestBuilder.get();
    assertThat(updateResponse.getIndex(), equalTo("test"));
    assertThat(updateResponse.getType(), equalTo("type1"));
    assertThat(updateResponse.getId(), equalTo("1"));
    assertThat(updateResponse.isCreated(), equalTo(false));

    getResponse = client().prepareGet("test", "type1", "1").get();
    assertThat(getResponse.isExists(), equalTo(true));
    assertThat(getResponse.getSourceAsMap().containsKey("field1"), equalTo(true));
    assertThat(getResponse.getSourceAsMap().containsKey("field2"), equalTo(true));
  }
  @Test
  public void testUpsert() throws Exception {
    createIndex();
    ensureGreen();

    UpdateResponse updateResponse =
        client()
            .prepareUpdate("test", "type1", "1")
            .setUpsert(XContentFactory.jsonBuilder().startObject().field("field", 1).endObject())
            .setScript("ctx._source.field += 1", ScriptService.ScriptType.INLINE)
            .execute()
            .actionGet();
    assertTrue(updateResponse.isCreated());

    for (int i = 0; i < 5; i++) {
      GetResponse getResponse = client().prepareGet("test", "type1", "1").execute().actionGet();
      assertThat(getResponse.getSourceAsMap().get("field").toString(), equalTo("1"));
    }

    updateResponse =
        client()
            .prepareUpdate("test", "type1", "1")
            .setUpsert(XContentFactory.jsonBuilder().startObject().field("field", 1).endObject())
            .setScript("ctx._source.field += 1", ScriptService.ScriptType.INLINE)
            .execute()
            .actionGet();
    assertFalse(updateResponse.isCreated());

    for (int i = 0; i < 5; i++) {
      GetResponse getResponse = client().prepareGet("test", "type1", "1").execute().actionGet();
      assertThat(getResponse.getSourceAsMap().get("field").toString(), equalTo("2"));
    }
  }
  public void testUpdateDelete() {
    // update action goes to the primary, delete op gets executed locally, then replicated
    String[] updateShardActions =
        new String[] {UpdateAction.NAME + "[s]", DeleteAction.NAME + "[r]"};
    interceptTransportActions(updateShardActions);

    String indexOrAlias = randomIndexOrAlias();
    client().prepareIndex(indexOrAlias, "type", "id").setSource("field", "value").get();
    UpdateRequest updateRequest =
        new UpdateRequest(indexOrAlias, "type", "id").script(new Script("ctx.op='delete'"));
    UpdateResponse updateResponse =
        internalCluster().clientNodeClient().update(updateRequest).actionGet();
    assertThat(updateResponse.isCreated(), equalTo(false));

    clearInterceptedActions();
    assertSameIndices(updateRequest, updateShardActions);
  }
  public void testUpdateUpsert() {
    // update action goes to the primary, index op gets executed locally, then replicated
    String[] updateShardActions =
        new String[] {UpdateAction.NAME + "[s]", IndexAction.NAME + "[r]"};
    interceptTransportActions(updateShardActions);

    String indexOrAlias = randomIndexOrAlias();
    UpdateRequest updateRequest =
        new UpdateRequest(indexOrAlias, "type", "id")
            .upsert("field", "value")
            .doc("field1", "value1");
    UpdateResponse updateResponse =
        internalCluster().clientNodeClient().update(updateRequest).actionGet();
    assertThat(updateResponse.isCreated(), equalTo(true));

    clearInterceptedActions();
    assertSameIndices(updateRequest, updateShardActions);
  }
  @Test
  public void testUpdate() throws Exception {
    createIndex();
    ensureGreen();

    try {
      client()
          .prepareUpdate("test", "type1", "1")
          .setScript("ctx._source.field++", ScriptService.ScriptType.INLINE)
          .execute()
          .actionGet();
      fail();
    } catch (DocumentMissingException e) {
      // all is well
    }

    client().prepareIndex("test", "type1", "1").setSource("field", 1).execute().actionGet();

    UpdateResponse updateResponse =
        client()
            .prepareUpdate("test", "type1", "1")
            .setScript("ctx._source.field += 1", ScriptService.ScriptType.INLINE)
            .execute()
            .actionGet();
    assertThat(updateResponse.getVersion(), equalTo(2L));
    assertFalse(updateResponse.isCreated());

    for (int i = 0; i < 5; i++) {
      GetResponse getResponse = client().prepareGet("test", "type1", "1").execute().actionGet();
      assertThat(getResponse.getSourceAsMap().get("field").toString(), equalTo("2"));
    }

    updateResponse =
        client()
            .prepareUpdate("test", "type1", "1")
            .setScript("ctx._source.field += count", ScriptService.ScriptType.INLINE)
            .addScriptParam("count", 3)
            .execute()
            .actionGet();
    assertThat(updateResponse.getVersion(), equalTo(3L));
    assertFalse(updateResponse.isCreated());

    for (int i = 0; i < 5; i++) {
      GetResponse getResponse = client().prepareGet("test", "type1", "1").execute().actionGet();
      assertThat(getResponse.getSourceAsMap().get("field").toString(), equalTo("5"));
    }

    // check noop
    updateResponse =
        client()
            .prepareUpdate("test", "type1", "1")
            .setScript("ctx.op = 'none'", ScriptService.ScriptType.INLINE)
            .execute()
            .actionGet();
    assertThat(updateResponse.getVersion(), equalTo(3L));
    assertFalse(updateResponse.isCreated());

    for (int i = 0; i < 5; i++) {
      GetResponse getResponse = client().prepareGet("test", "type1", "1").execute().actionGet();
      assertThat(getResponse.getSourceAsMap().get("field").toString(), equalTo("5"));
    }

    // check delete
    updateResponse =
        client()
            .prepareUpdate("test", "type1", "1")
            .setScript("ctx.op = 'delete'", ScriptService.ScriptType.INLINE)
            .execute()
            .actionGet();
    assertThat(updateResponse.getVersion(), equalTo(4L));
    assertFalse(updateResponse.isCreated());

    for (int i = 0; i < 5; i++) {
      GetResponse getResponse = client().prepareGet("test", "type1", "1").execute().actionGet();
      assertThat(getResponse.isExists(), equalTo(false));
    }

    // check TTL is kept after an update without TTL
    client()
        .prepareIndex("test", "type1", "2")
        .setSource("field", 1)
        .setTTL(86400000L)
        .setRefresh(true)
        .execute()
        .actionGet();
    GetResponse getResponse =
        client().prepareGet("test", "type1", "2").setFields("_ttl").execute().actionGet();
    long ttl = ((Number) getResponse.getField("_ttl").getValue()).longValue();
    assertThat(ttl, greaterThan(0L));
    client()
        .prepareUpdate("test", "type1", "2")
        .setScript("ctx._source.field += 1", ScriptService.ScriptType.INLINE)
        .execute()
        .actionGet();
    getResponse = client().prepareGet("test", "type1", "2").setFields("_ttl").execute().actionGet();
    ttl = ((Number) getResponse.getField("_ttl").getValue()).longValue();
    assertThat(ttl, greaterThan(0L));

    // check TTL update
    client()
        .prepareUpdate("test", "type1", "2")
        .setScript("ctx._ttl = 3600000", ScriptService.ScriptType.INLINE)
        .execute()
        .actionGet();
    getResponse = client().prepareGet("test", "type1", "2").setFields("_ttl").execute().actionGet();
    ttl = ((Number) getResponse.getField("_ttl").getValue()).longValue();
    assertThat(ttl, greaterThan(0L));
    assertThat(ttl, lessThanOrEqualTo(3600000L));

    // check timestamp update
    client()
        .prepareIndex("test", "type1", "3")
        .setSource("field", 1)
        .setRefresh(true)
        .execute()
        .actionGet();
    client()
        .prepareUpdate("test", "type1", "3")
        .setScript("ctx._timestamp = \"2009-11-15T14:12:12\"", ScriptService.ScriptType.INLINE)
        .execute()
        .actionGet();
    getResponse =
        client().prepareGet("test", "type1", "3").setFields("_timestamp").execute().actionGet();
    long timestamp = ((Number) getResponse.getField("_timestamp").getValue()).longValue();
    assertThat(timestamp, equalTo(1258294332000L));

    // check fields parameter
    client().prepareIndex("test", "type1", "1").setSource("field", 1).execute().actionGet();
    updateResponse =
        client()
            .prepareUpdate("test", "type1", "1")
            .setScript("ctx._source.field += 1", ScriptService.ScriptType.INLINE)
            .setFields("_source", "field")
            .execute()
            .actionGet();
    assertThat(updateResponse.getGetResult(), notNullValue());
    assertThat(updateResponse.getGetResult().sourceRef(), notNullValue());
    assertThat(updateResponse.getGetResult().field("field").getValue(), notNullValue());

    // check updates without script
    // add new field
    client().prepareIndex("test", "type1", "1").setSource("field", 1).execute().actionGet();
    updateResponse =
        client()
            .prepareUpdate("test", "type1", "1")
            .setDoc(XContentFactory.jsonBuilder().startObject().field("field2", 2).endObject())
            .execute()
            .actionGet();
    for (int i = 0; i < 5; i++) {
      getResponse = client().prepareGet("test", "type1", "1").execute().actionGet();
      assertThat(getResponse.getSourceAsMap().get("field").toString(), equalTo("1"));
      assertThat(getResponse.getSourceAsMap().get("field2").toString(), equalTo("2"));
    }

    // change existing field
    updateResponse =
        client()
            .prepareUpdate("test", "type1", "1")
            .setDoc(XContentFactory.jsonBuilder().startObject().field("field", 3).endObject())
            .execute()
            .actionGet();
    for (int i = 0; i < 5; i++) {
      getResponse = client().prepareGet("test", "type1", "1").execute().actionGet();
      assertThat(getResponse.getSourceAsMap().get("field").toString(), equalTo("3"));
      assertThat(getResponse.getSourceAsMap().get("field2").toString(), equalTo("2"));
    }

    // recursive map
    Map<String, Object> testMap = new HashMap<>();
    Map<String, Object> testMap2 = new HashMap<>();
    Map<String, Object> testMap3 = new HashMap<>();
    testMap3.put("commonkey", testMap);
    testMap3.put("map3", 5);
    testMap2.put("map2", 6);
    testMap.put("commonkey", testMap2);
    testMap.put("map1", 8);

    client().prepareIndex("test", "type1", "1").setSource("map", testMap).execute().actionGet();
    updateResponse =
        client()
            .prepareUpdate("test", "type1", "1")
            .setDoc(XContentFactory.jsonBuilder().startObject().field("map", testMap3).endObject())
            .execute()
            .actionGet();
    for (int i = 0; i < 5; i++) {
      getResponse = client().prepareGet("test", "type1", "1").execute().actionGet();
      Map map1 = (Map) getResponse.getSourceAsMap().get("map");
      assertThat(map1.size(), equalTo(3));
      assertThat(map1.containsKey("map1"), equalTo(true));
      assertThat(map1.containsKey("map3"), equalTo(true));
      assertThat(map1.containsKey("commonkey"), equalTo(true));
      Map map2 = (Map) map1.get("commonkey");
      assertThat(map2.size(), equalTo(3));
      assertThat(map2.containsKey("map1"), equalTo(true));
      assertThat(map2.containsKey("map2"), equalTo(true));
      assertThat(map2.containsKey("commonkey"), equalTo(true));
    }
  }