Search in sources :

Example 16 with UpdateResponse

use of org.elasticsearch.action.update.UpdateResponse in project elasticsearch by elastic.

the class UpdateIT method testUpdate.

public void testUpdate() throws Exception {
    createTestIndex();
    ensureGreen();
    try {
        client().prepareUpdate(indexOrAlias(), "type1", "1").setScript(new Script(ScriptType.INLINE, "field_inc", "field", Collections.emptyMap())).execute().actionGet();
        fail();
    } catch (DocumentMissingException e) {
    // all is well
    }
    client().prepareIndex("test", "type1", "1").setSource("field", 1).execute().actionGet();
    UpdateResponse updateResponse = client().prepareUpdate(indexOrAlias(), "type1", "1").setScript(new Script(ScriptType.INLINE, "field_inc", "field", Collections.emptyMap())).execute().actionGet();
    assertThat(updateResponse.getVersion(), equalTo(2L));
    assertEquals(DocWriteResponse.Result.UPDATED, updateResponse.getResult());
    assertThat(updateResponse.getIndex(), equalTo("test"));
    for (int i = 0; i < 5; i++) {
        GetResponse getResponse = client().prepareGet("test", "type1", "1").execute().actionGet();
        assertThat(getResponse.getSourceAsMap().get("field").toString(), equalTo("2"));
    }
    Map<String, Object> params = new HashMap<>();
    params.put("inc", 3);
    updateResponse = client().prepareUpdate(indexOrAlias(), "type1", "1").setScript(new Script(ScriptType.INLINE, "field_inc", "field", params)).execute().actionGet();
    assertThat(updateResponse.getVersion(), equalTo(3L));
    assertEquals(DocWriteResponse.Result.UPDATED, updateResponse.getResult());
    assertThat(updateResponse.getIndex(), equalTo("test"));
    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(indexOrAlias(), "type1", "1").setScript(new Script(ScriptType.INLINE, "put_values", "", Collections.singletonMap("_ctx", Collections.singletonMap("op", "none")))).execute().actionGet();
    assertThat(updateResponse.getVersion(), equalTo(3L));
    assertEquals(DocWriteResponse.Result.NOOP, updateResponse.getResult());
    assertThat(updateResponse.getIndex(), equalTo("test"));
    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(indexOrAlias(), "type1", "1").setScript(new Script(ScriptType.INLINE, "put_values", "", Collections.singletonMap("_ctx", Collections.singletonMap("op", "delete")))).execute().actionGet();
    assertThat(updateResponse.getVersion(), equalTo(4L));
    assertEquals(DocWriteResponse.Result.DELETED, updateResponse.getResult());
    assertThat(updateResponse.getIndex(), equalTo("test"));
    for (int i = 0; i < 5; i++) {
        GetResponse getResponse = client().prepareGet("test", "type1", "1").execute().actionGet();
        assertThat(getResponse.isExists(), equalTo(false));
    }
    // check fields parameter
    client().prepareIndex("test", "type1", "1").setSource("field", 1).execute().actionGet();
    updateResponse = client().prepareUpdate(indexOrAlias(), "type1", "1").setScript(new Script(ScriptType.INLINE, "field_inc", "field", Collections.emptyMap())).setFields("field").setFetchSource(true).execute().actionGet();
    assertThat(updateResponse.getIndex(), equalTo("test"));
    assertThat(updateResponse.getGetResult(), notNullValue());
    assertThat(updateResponse.getGetResult().getIndex(), equalTo("test"));
    assertThat(updateResponse.getGetResult().sourceRef(), notNullValue());
    assertThat(updateResponse.getGetResult().field("field").getValue(), notNullValue());
    // check _source parameter
    client().prepareIndex("test", "type1", "1").setSource("field1", 1, "field2", 2).execute().actionGet();
    updateResponse = client().prepareUpdate(indexOrAlias(), "type1", "1").setScript(new Script(ScriptType.INLINE, "field_inc", "field1", Collections.emptyMap())).setFetchSource("field1", "field2").get();
    assertThat(updateResponse.getIndex(), equalTo("test"));
    assertThat(updateResponse.getGetResult(), notNullValue());
    assertThat(updateResponse.getGetResult().getIndex(), equalTo("test"));
    assertThat(updateResponse.getGetResult().sourceRef(), notNullValue());
    assertThat(updateResponse.getGetResult().field("field1"), nullValue());
    assertThat(updateResponse.getGetResult().sourceAsMap().size(), equalTo(1));
    assertThat(updateResponse.getGetResult().sourceAsMap().get("field1"), equalTo(2));
    // check updates without script
    // add new field
    client().prepareIndex("test", "type1", "1").setSource("field", 1).execute().actionGet();
    updateResponse = client().prepareUpdate(indexOrAlias(), "type1", "1").setDoc(XContentFactory.jsonBuilder().startObject().field("field2", 2).endObject()).execute().actionGet();
    for (int i = 0; i < 5; i++) {
        GetResponse 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(indexOrAlias(), "type1", "1").setDoc(XContentFactory.jsonBuilder().startObject().field("field", 3).endObject()).execute().actionGet();
    for (int i = 0; i < 5; i++) {
        GetResponse 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(indexOrAlias(), "type1", "1").setDoc(XContentFactory.jsonBuilder().startObject().field("map", testMap3).endObject()).execute().actionGet();
    for (int i = 0; i < 5; i++) {
        GetResponse 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));
    }
}
Also used : SearchScript(org.elasticsearch.script.SearchScript) Script(org.elasticsearch.script.Script) CompiledScript(org.elasticsearch.script.CompiledScript) ExecutableScript(org.elasticsearch.script.ExecutableScript) UpdateResponse(org.elasticsearch.action.update.UpdateResponse) HashMap(java.util.HashMap) DocumentMissingException(org.elasticsearch.index.engine.DocumentMissingException) Matchers.containsString(org.hamcrest.Matchers.containsString) GetResponse(org.elasticsearch.action.get.GetResponse) Map(java.util.Map) HashMap(java.util.HashMap)

Example 17 with UpdateResponse

use of org.elasticsearch.action.update.UpdateResponse in project elasticsearch by elastic.

the class UpdateIT method testUpsertFields.

public void testUpsertFields() throws Exception {
    createTestIndex();
    ensureGreen();
    UpdateResponse updateResponse = client().prepareUpdate(indexOrAlias(), "type1", "1").setUpsert(XContentFactory.jsonBuilder().startObject().field("bar", "baz").endObject()).setScript(new Script(ScriptType.INLINE, "put_values", "", Collections.singletonMap("extra", "foo"))).setFetchSource(true).execute().actionGet();
    assertThat(updateResponse.getIndex(), equalTo("test"));
    assertThat(updateResponse.getGetResult(), notNullValue());
    assertThat(updateResponse.getGetResult().getIndex(), equalTo("test"));
    assertThat(updateResponse.getGetResult().sourceAsMap().get("bar").toString(), equalTo("baz"));
    assertThat(updateResponse.getGetResult().sourceAsMap().get("extra"), nullValue());
    updateResponse = client().prepareUpdate(indexOrAlias(), "type1", "1").setUpsert(XContentFactory.jsonBuilder().startObject().field("bar", "baz").endObject()).setScript(new Script(ScriptType.INLINE, "put_values", "", Collections.singletonMap("extra", "foo"))).setFields("_source").execute().actionGet();
    assertThat(updateResponse.getIndex(), equalTo("test"));
    assertThat(updateResponse.getGetResult(), notNullValue());
    assertThat(updateResponse.getGetResult().getIndex(), equalTo("test"));
    assertThat(updateResponse.getGetResult().sourceAsMap().get("bar").toString(), equalTo("baz"));
    assertThat(updateResponse.getGetResult().sourceAsMap().get("extra").toString(), equalTo("foo"));
}
Also used : UpdateResponse(org.elasticsearch.action.update.UpdateResponse) SearchScript(org.elasticsearch.script.SearchScript) Script(org.elasticsearch.script.Script) CompiledScript(org.elasticsearch.script.CompiledScript) ExecutableScript(org.elasticsearch.script.ExecutableScript)

Example 18 with UpdateResponse

use of org.elasticsearch.action.update.UpdateResponse in project elasticsearch by elastic.

the class UpdateIT method testScriptedUpsert.

public void testScriptedUpsert() throws Exception {
    createTestIndex();
    ensureGreen();
    // Script logic is
    // 1) New accounts take balance from "balance" in upsert doc and first payment is charged at 50%
    // 2) Existing accounts subtract full payment from balance stored in elasticsearch
    int openingBalance = 10;
    Map<String, Object> params = new HashMap<>();
    params.put("payment", 2);
    // Pay money from what will be a new account and opening balance comes from upsert doc
    // provided by client
    UpdateResponse updateResponse = client().prepareUpdate(indexOrAlias(), "type1", "1").setUpsert(XContentFactory.jsonBuilder().startObject().field("balance", openingBalance).endObject()).setScriptedUpsert(true).setScript(new Script(ScriptType.INLINE, "scripted_upsert", "", params)).execute().actionGet();
    assertEquals(DocWriteResponse.Result.CREATED, updateResponse.getResult());
    assertThat(updateResponse.getIndex(), equalTo("test"));
    for (int i = 0; i < 5; i++) {
        GetResponse getResponse = client().prepareGet("test", "type1", "1").execute().actionGet();
        assertThat(getResponse.getSourceAsMap().get("balance").toString(), equalTo("9"));
    }
    // Now pay money for an existing account where balance is stored in es
    updateResponse = client().prepareUpdate(indexOrAlias(), "type1", "1").setUpsert(XContentFactory.jsonBuilder().startObject().field("balance", openingBalance).endObject()).setScriptedUpsert(true).setScript(new Script(ScriptType.INLINE, "scripted_upsert", "", params)).execute().actionGet();
    assertEquals(DocWriteResponse.Result.UPDATED, updateResponse.getResult());
    assertThat(updateResponse.getIndex(), equalTo("test"));
    for (int i = 0; i < 5; i++) {
        GetResponse getResponse = client().prepareGet("test", "type1", "1").execute().actionGet();
        assertThat(getResponse.getSourceAsMap().get("balance").toString(), equalTo("7"));
    }
}
Also used : UpdateResponse(org.elasticsearch.action.update.UpdateResponse) SearchScript(org.elasticsearch.script.SearchScript) Script(org.elasticsearch.script.Script) CompiledScript(org.elasticsearch.script.CompiledScript) ExecutableScript(org.elasticsearch.script.ExecutableScript) HashMap(java.util.HashMap) Matchers.containsString(org.hamcrest.Matchers.containsString) GetResponse(org.elasticsearch.action.get.GetResponse)

Example 19 with UpdateResponse

use of org.elasticsearch.action.update.UpdateResponse in project elasticsearch by elastic.

the class UpdateIT method testUpsert.

public void testUpsert() throws Exception {
    createTestIndex();
    ensureGreen();
    UpdateResponse updateResponse = client().prepareUpdate(indexOrAlias(), "type1", "1").setUpsert(XContentFactory.jsonBuilder().startObject().field("field", 1).endObject()).setScript(new Script(ScriptType.INLINE, "field_inc", "field", Collections.emptyMap())).execute().actionGet();
    assertEquals(DocWriteResponse.Result.CREATED, updateResponse.getResult());
    assertThat(updateResponse.getIndex(), equalTo("test"));
    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(indexOrAlias(), "type1", "1").setUpsert(XContentFactory.jsonBuilder().startObject().field("field", 1).endObject()).setScript(new Script(ScriptType.INLINE, "field_inc", "field", Collections.emptyMap())).execute().actionGet();
    assertEquals(DocWriteResponse.Result.UPDATED, updateResponse.getResult());
    assertThat(updateResponse.getIndex(), equalTo("test"));
    for (int i = 0; i < 5; i++) {
        GetResponse getResponse = client().prepareGet("test", "type1", "1").execute().actionGet();
        assertThat(getResponse.getSourceAsMap().get("field").toString(), equalTo("2"));
    }
}
Also used : UpdateResponse(org.elasticsearch.action.update.UpdateResponse) SearchScript(org.elasticsearch.script.SearchScript) Script(org.elasticsearch.script.Script) CompiledScript(org.elasticsearch.script.CompiledScript) ExecutableScript(org.elasticsearch.script.ExecutableScript) GetResponse(org.elasticsearch.action.get.GetResponse)

Example 20 with UpdateResponse

use of org.elasticsearch.action.update.UpdateResponse in project elasticsearch by elastic.

the class UpdateNoopIT method updateAndCheckSource.

private void updateAndCheckSource(long expectedVersion, Boolean detectNoop, XContentBuilder xContentBuilder) {
    UpdateResponse updateResponse = update(detectNoop, expectedVersion, xContentBuilder);
    assertEquals(updateResponse.getGetResult().sourceRef().utf8ToString(), xContentBuilder.bytes().utf8ToString());
}
Also used : UpdateResponse(org.elasticsearch.action.update.UpdateResponse)

Aggregations

UpdateResponse (org.elasticsearch.action.update.UpdateResponse)23 Script (org.elasticsearch.script.Script)10 ExecutableScript (org.elasticsearch.script.ExecutableScript)8 CompiledScript (org.elasticsearch.script.CompiledScript)7 SearchScript (org.elasticsearch.script.SearchScript)7 UpdateRequest (org.elasticsearch.action.update.UpdateRequest)6 GetResponse (org.elasticsearch.action.get.GetResponse)5 HashMap (java.util.HashMap)4 IndexRequest (org.elasticsearch.action.index.IndexRequest)4 Matchers.containsString (org.hamcrest.Matchers.containsString)4 Map (java.util.Map)3 DeleteResponse (org.elasticsearch.action.delete.DeleteResponse)3 IndexResponse (org.elasticsearch.action.index.IndexResponse)3 DocumentMissingException (org.elasticsearch.index.engine.DocumentMissingException)3 IOException (java.io.IOException)2 ArrayList (java.util.ArrayList)2 DocWriteRequest (org.elasticsearch.action.DocWriteRequest)2 DocWriteResponse (org.elasticsearch.action.DocWriteResponse)2 Alias (org.elasticsearch.action.admin.indices.alias.Alias)2 DeleteRequest (org.elasticsearch.action.delete.DeleteRequest)2