Search in sources :

Example 1 with UpdateResponse

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

the class UpdateIT method testContextVariables.

public void testContextVariables() throws Exception {
    assertAcked(prepareCreate("test").addAlias(new Alias("alias")).addMapping("type1", XContentFactory.jsonBuilder().startObject().startObject("type1").endObject().endObject()).addMapping("subtype1", XContentFactory.jsonBuilder().startObject().startObject("subtype1").startObject("_parent").field("type", "type1").endObject().endObject().endObject()));
    ensureGreen();
    // Index some documents
    client().prepareIndex().setIndex("test").setType("type1").setId("parentId1").setSource("field1", 0, "content", "bar").execute().actionGet();
    client().prepareIndex().setIndex("test").setType("subtype1").setId("id1").setParent("parentId1").setRouting("routing1").setSource("field1", 1, "content", "foo").execute().actionGet();
    // Update the first object and note context variables values
    UpdateResponse updateResponse = client().prepareUpdate("test", "subtype1", "id1").setRouting("routing1").setScript(new Script(ScriptType.INLINE, "extract_ctx", "", Collections.emptyMap())).execute().actionGet();
    assertEquals(2, updateResponse.getVersion());
    GetResponse getResponse = client().prepareGet("test", "subtype1", "id1").setRouting("routing1").execute().actionGet();
    Map<String, Object> updateContext = (Map<String, Object>) getResponse.getSourceAsMap().get("update_context");
    assertEquals("test", updateContext.get("_index"));
    assertEquals("subtype1", updateContext.get("_type"));
    assertEquals("id1", updateContext.get("_id"));
    assertEquals(1, updateContext.get("_version"));
    assertEquals("parentId1", updateContext.get("_parent"));
    assertEquals("routing1", updateContext.get("_routing"));
    // Idem with the second object
    updateResponse = client().prepareUpdate("test", "type1", "parentId1").setScript(new Script(ScriptType.INLINE, "extract_ctx", "", Collections.emptyMap())).execute().actionGet();
    assertEquals(2, updateResponse.getVersion());
    getResponse = client().prepareGet("test", "type1", "parentId1").execute().actionGet();
    updateContext = (Map<String, Object>) getResponse.getSourceAsMap().get("update_context");
    assertEquals("test", updateContext.get("_index"));
    assertEquals("type1", updateContext.get("_type"));
    assertEquals("parentId1", updateContext.get("_id"));
    assertEquals(1, updateContext.get("_version"));
    assertNull(updateContext.get("_parent"));
    assertNull(updateContext.get("_routing"));
    assertNull(updateContext.get("_ttl"));
}
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) Alias(org.elasticsearch.action.admin.indices.alias.Alias) Matchers.containsString(org.hamcrest.Matchers.containsString) GetResponse(org.elasticsearch.action.get.GetResponse) Map(java.util.Map) HashMap(java.util.HashMap)

Example 2 with UpdateResponse

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

the class UpdateIT method testUpsertDoc.

public void testUpsertDoc() throws Exception {
    createTestIndex();
    ensureGreen();
    UpdateResponse updateResponse = client().prepareUpdate(indexOrAlias(), "type1", "1").setDoc(XContentFactory.jsonBuilder().startObject().field("bar", "baz").endObject()).setDocAsUpsert(true).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"));
}
Also used : UpdateResponse(org.elasticsearch.action.update.UpdateResponse)

Example 3 with UpdateResponse

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

the class UpdateIT method testIndexAutoCreation.

public void testIndexAutoCreation() throws Exception {
    UpdateResponse updateResponse = client().prepareUpdate("test", "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());
}
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 4 with UpdateResponse

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

the class UpdateNoopIT method update.

private UpdateResponse update(Boolean detectNoop, long expectedVersion, XContentBuilder xContentBuilder) {
    UpdateRequestBuilder updateRequest = client().prepareUpdate("test", "type1", "1").setDoc(xContentBuilder).setDocAsUpsert(true).setFields("_source");
    if (detectNoop != null) {
        updateRequest.setDetectNoop(detectNoop);
    }
    UpdateResponse updateResponse = updateRequest.get();
    assertThat(updateResponse.getGetResult(), notNullValue());
    assertEquals(expectedVersion, updateResponse.getVersion());
    return updateResponse;
}
Also used : UpdateResponse(org.elasticsearch.action.update.UpdateResponse) UpdateRequestBuilder(org.elasticsearch.action.update.UpdateRequestBuilder)

Example 5 with UpdateResponse

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

the class IndicesRequestIT method testUpdateUpsert.

public void testUpdateUpsert() {
    //update action goes to the primary, index op gets executed locally, then replicated
    String[] updateShardActions = new String[] { UpdateAction.NAME + "[s]", BulkAction.NAME + "[s][p]", BulkAction.NAME + "[s][r]" };
    interceptTransportActions(updateShardActions);
    String indexOrAlias = randomIndexOrAlias();
    UpdateRequest updateRequest = new UpdateRequest(indexOrAlias, "type", "id").upsert(Requests.INDEX_CONTENT_TYPE, "field", "value").doc(Requests.INDEX_CONTENT_TYPE, "field1", "value1");
    UpdateResponse updateResponse = internalCluster().coordOnlyNodeClient().update(updateRequest).actionGet();
    assertEquals(DocWriteResponse.Result.CREATED, updateResponse.getResult());
    clearInterceptedActions();
    assertSameIndices(updateRequest, updateShardActions);
}
Also used : UpdateResponse(org.elasticsearch.action.update.UpdateResponse) UpdateRequest(org.elasticsearch.action.update.UpdateRequest)

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