Search in sources :

Example 21 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 22 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)

Example 23 with UpdateResponse

use of org.elasticsearch.action.update.UpdateResponse in project fess by codelibs.

the class SearchService method update.

public boolean update(final String id, final Consumer<UpdateRequestBuilder> builderLambda) {
    try {
        final UpdateRequestBuilder builder = fessEsClient.prepareUpdate(fessConfig.getIndexDocumentUpdateIndex(), fessConfig.getIndexDocumentType(), id);
        builderLambda.accept(builder);
        final UpdateResponse response = builder.execute().actionGet(fessConfig.getIndexIndexTimeout());
        return response.getResult() == Result.CREATED || response.getResult() == Result.UPDATED;
    } catch (final ElasticsearchException e) {
        throw new FessEsClientException("Failed to update doc  " + id, e);
    }
}
Also used : UpdateResponse(org.elasticsearch.action.update.UpdateResponse) UpdateRequestBuilder(org.elasticsearch.action.update.UpdateRequestBuilder) FessEsClientException(org.codelibs.fess.es.client.FessEsClientException) ElasticsearchException(org.elasticsearch.ElasticsearchException)

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