Search in sources :

Example 6 with UpdateRequestBuilder

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

the class BulkWithUpdatesIT method testBulkUpdateLargerVolume.

public void testBulkUpdateLargerVolume() throws Exception {
    createIndex("test");
    ensureGreen();
    int numDocs = scaledRandomIntBetween(100, 2000);
    if (numDocs % 2 == 1) {
        // this test needs an even num of docs
        numDocs++;
    }
    final Script script = new Script(ScriptType.INLINE, CustomScriptPlugin.NAME, "ctx._source.counter += 1", Collections.emptyMap());
    BulkRequestBuilder builder = client().prepareBulk();
    for (int i = 0; i < numDocs; i++) {
        builder.add(client().prepareUpdate().setIndex("test").setType("type1").setId(Integer.toString(i)).setFields("counter").setScript(script).setUpsert(jsonBuilder().startObject().field("counter", 1).endObject()));
    }
    BulkResponse response = builder.get();
    assertThat(response.hasFailures(), equalTo(false));
    assertThat(response.getItems().length, equalTo(numDocs));
    for (int i = 0; i < numDocs; i++) {
        assertThat(response.getItems()[i].getId(), equalTo(Integer.toString(i)));
        assertThat(response.getItems()[i].getVersion(), equalTo(1L));
        assertThat(response.getItems()[i].getIndex(), equalTo("test"));
        assertThat(response.getItems()[i].getType(), equalTo("type1"));
        assertThat(response.getItems()[i].getOpType(), equalTo(OpType.UPDATE));
        assertThat(response.getItems()[i].getResponse().getId(), equalTo(Integer.toString(i)));
        assertThat(response.getItems()[i].getResponse().getVersion(), equalTo(1L));
        assertThat(((UpdateResponse) response.getItems()[i].getResponse()).getGetResult().field("counter").getValue(), equalTo(1));
        for (int j = 0; j < 5; j++) {
            GetResponse getResponse = client().prepareGet("test", "type1", Integer.toString(i)).execute().actionGet();
            assertThat(getResponse.isExists(), equalTo(true));
            assertThat(getResponse.getVersion(), equalTo(1L));
            assertThat(((Number) getResponse.getSource().get("counter")).longValue(), equalTo(1L));
        }
    }
    builder = client().prepareBulk();
    for (int i = 0; i < numDocs; i++) {
        UpdateRequestBuilder updateBuilder = client().prepareUpdate().setIndex("test").setType("type1").setId(Integer.toString(i)).setFields("counter");
        if (i % 2 == 0) {
            updateBuilder.setScript(script);
        } else {
            updateBuilder.setDoc(jsonBuilder().startObject().field("counter", 2).endObject());
        }
        if (i % 3 == 0) {
            updateBuilder.setRetryOnConflict(3);
        }
        builder.add(updateBuilder);
    }
    response = builder.execute().actionGet();
    assertThat(response.hasFailures(), equalTo(false));
    assertThat(response.getItems().length, equalTo(numDocs));
    for (int i = 0; i < numDocs; i++) {
        assertThat(response.getItems()[i].getId(), equalTo(Integer.toString(i)));
        assertThat(response.getItems()[i].getVersion(), equalTo(2L));
        assertThat(response.getItems()[i].getIndex(), equalTo("test"));
        assertThat(response.getItems()[i].getType(), equalTo("type1"));
        assertThat(response.getItems()[i].getOpType(), equalTo(OpType.UPDATE));
        assertThat(response.getItems()[i].getResponse().getId(), equalTo(Integer.toString(i)));
        assertThat(response.getItems()[i].getResponse().getVersion(), equalTo(2L));
        assertThat(((UpdateResponse) response.getItems()[i].getResponse()).getGetResult().field("counter").getValue(), equalTo(2));
    }
    builder = client().prepareBulk();
    int maxDocs = numDocs / 2 + numDocs;
    for (int i = (numDocs / 2); i < maxDocs; i++) {
        builder.add(client().prepareUpdate().setIndex("test").setType("type1").setId(Integer.toString(i)).setScript(script));
    }
    response = builder.execute().actionGet();
    assertThat(response.hasFailures(), equalTo(true));
    assertThat(response.getItems().length, equalTo(numDocs));
    for (int i = 0; i < numDocs; i++) {
        int id = i + (numDocs / 2);
        if (i >= (numDocs / 2)) {
            assertThat(response.getItems()[i].getFailure().getId(), equalTo(Integer.toString(id)));
            assertThat(response.getItems()[i].getFailure().getMessage(), containsString("document missing"));
        } else {
            assertThat(response.getItems()[i].getId(), equalTo(Integer.toString(id)));
            assertThat(response.getItems()[i].getVersion(), equalTo(3L));
            assertThat(response.getItems()[i].getIndex(), equalTo("test"));
            assertThat(response.getItems()[i].getType(), equalTo("type1"));
            assertThat(response.getItems()[i].getOpType(), equalTo(OpType.UPDATE));
        }
    }
    builder = client().prepareBulk();
    for (int i = 0; i < numDocs; i++) {
        builder.add(client().prepareUpdate().setIndex("test").setType("type1").setId(Integer.toString(i)).setScript(new Script(ScriptType.INLINE, CustomScriptPlugin.NAME, "ctx.op = \"none\"", Collections.emptyMap())));
    }
    response = builder.execute().actionGet();
    assertThat(response.buildFailureMessage(), response.hasFailures(), equalTo(false));
    assertThat(response.getItems().length, equalTo(numDocs));
    for (int i = 0; i < numDocs; i++) {
        assertThat(response.getItems()[i].getItemId(), equalTo(i));
        assertThat(response.getItems()[i].getId(), equalTo(Integer.toString(i)));
        assertThat(response.getItems()[i].getIndex(), equalTo("test"));
        assertThat(response.getItems()[i].getType(), equalTo("type1"));
        assertThat(response.getItems()[i].getOpType(), equalTo(OpType.UPDATE));
    }
    builder = client().prepareBulk();
    for (int i = 0; i < numDocs; i++) {
        builder.add(client().prepareUpdate().setIndex("test").setType("type1").setId(Integer.toString(i)).setScript(new Script(ScriptType.INLINE, CustomScriptPlugin.NAME, "ctx.op = \"delete\"", Collections.emptyMap())));
    }
    response = builder.execute().actionGet();
    assertThat(response.hasFailures(), equalTo(false));
    assertThat(response.getItems().length, equalTo(numDocs));
    for (int i = 0; i < numDocs; i++) {
        assertThat(response.getItems()[i].getItemId(), equalTo(i));
        assertThat(response.getItems()[i].getId(), equalTo(Integer.toString(i)));
        assertThat(response.getItems()[i].getIndex(), equalTo("test"));
        assertThat(response.getItems()[i].getType(), equalTo("type1"));
        assertThat(response.getItems()[i].getOpType(), equalTo(OpType.UPDATE));
        for (int j = 0; j < 5; j++) {
            GetResponse getResponse = client().prepareGet("test", "type1", Integer.toString(i)).get();
            assertThat(getResponse.isExists(), equalTo(false));
        }
    }
}
Also used : Script(org.elasticsearch.script.Script) UpdateRequestBuilder(org.elasticsearch.action.update.UpdateRequestBuilder) GetResponse(org.elasticsearch.action.get.GetResponse)

Example 7 with UpdateRequestBuilder

use of org.elasticsearch.action.update.UpdateRequestBuilder in project titan by thinkaurelius.

the class ElasticSearchIndex method mutate.

@Override
public void mutate(Map<String, Map<String, IndexMutation>> mutations, KeyInformation.IndexRetriever informations, BaseTransaction tx) throws BackendException {
    BulkRequestBuilder brb = client.prepareBulk();
    int bulkrequests = 0;
    try {
        for (Map.Entry<String, Map<String, IndexMutation>> stores : mutations.entrySet()) {
            String storename = stores.getKey();
            for (Map.Entry<String, IndexMutation> entry : stores.getValue().entrySet()) {
                String docid = entry.getKey();
                IndexMutation mutation = entry.getValue();
                assert mutation.isConsolidated();
                Preconditions.checkArgument(!(mutation.isNew() && mutation.isDeleted()));
                Preconditions.checkArgument(!mutation.isNew() || !mutation.hasDeletions());
                Preconditions.checkArgument(!mutation.isDeleted() || !mutation.hasAdditions());
                //Deletions first
                if (mutation.hasDeletions()) {
                    if (mutation.isDeleted()) {
                        log.trace("Deleting entire document {}", docid);
                        brb.add(new DeleteRequest(indexName, storename, docid));
                    } else {
                        String script = getDeletionScript(informations, storename, mutation);
                        brb.add(client.prepareUpdate(indexName, storename, docid).setScript(script, ScriptService.ScriptType.INLINE));
                        log.trace("Adding script {}", script);
                    }
                    bulkrequests++;
                }
                if (mutation.hasAdditions()) {
                    int ttl = mutation.determineTTL();
                    if (mutation.isNew()) {
                        //Index
                        log.trace("Adding entire document {}", docid);
                        brb.add(new IndexRequest(indexName, storename, docid).source(getNewDocument(mutation.getAdditions(), informations.get(storename), ttl)));
                    } else {
                        Preconditions.checkArgument(ttl == 0, "Elasticsearch only supports TTL on new documents [%s]", docid);
                        boolean needUpsert = !mutation.hasDeletions();
                        String script = getAdditionScript(informations, storename, mutation);
                        UpdateRequestBuilder update = client.prepareUpdate(indexName, storename, docid).setScript(script, ScriptService.ScriptType.INLINE);
                        if (needUpsert) {
                            XContentBuilder doc = getNewDocument(mutation.getAdditions(), informations.get(storename), ttl);
                            update.setUpsert(doc);
                        }
                        brb.add(update);
                        log.trace("Adding script {}", script);
                    }
                    bulkrequests++;
                }
            }
        }
        if (bulkrequests > 0) {
            BulkResponse bulkItemResponses = brb.execute().actionGet();
            if (bulkItemResponses.hasFailures()) {
                boolean actualFailure = false;
                for (BulkItemResponse response : bulkItemResponses.getItems()) {
                    //The document may have been deleted, which is OK
                    if (response.isFailed() && response.getFailure().getStatus() != RestStatus.NOT_FOUND) {
                        log.error("Failed to execute ES query {}", response.getFailureMessage());
                        actualFailure = true;
                    }
                }
                if (actualFailure) {
                    throw new Exception(bulkItemResponses.buildFailureMessage());
                }
            }
        }
    } catch (Exception e) {
        log.error("Failed to execute ES query {}", brb.request().timeout(), e);
        throw convert(e);
    }
}
Also used : UpdateRequestBuilder(org.elasticsearch.action.update.UpdateRequestBuilder) BulkItemResponse(org.elasticsearch.action.bulk.BulkItemResponse) BulkResponse(org.elasticsearch.action.bulk.BulkResponse) IndexRequest(org.elasticsearch.action.index.IndexRequest) DeleteIndexRequest(org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest) FileNotFoundException(java.io.FileNotFoundException) TitanException(com.thinkaurelius.titan.core.TitanException) IndexMissingException(org.elasticsearch.indices.IndexMissingException) IOException(java.io.IOException) BulkRequestBuilder(org.elasticsearch.action.bulk.BulkRequestBuilder) DeleteRequest(org.elasticsearch.action.delete.DeleteRequest) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder)

Example 8 with UpdateRequestBuilder

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

the class EsAbstractBehavior method delegateBatchRequest.

protected <BUILDER> int[] delegateBatchRequest(final List<? extends Entity> entityList, Function<EsAbstractEntity, BUILDER> call) {
    @SuppressWarnings("unchecked") final BulkList<? extends Entity, BUILDER> bulkList = (BulkList<? extends Entity, BUILDER>) entityList;
    final RequestOptionCall<BUILDER> builderEntityCall = bulkList.getEntityCall();
    final BulkRequestBuilder bulkBuilder = client.prepareBulk();
    for (final Entity entity : entityList) {
        final EsAbstractEntity esEntity = (EsAbstractEntity) entity;
        BUILDER builder = call.apply(esEntity);
        if (builder instanceof IndexRequestBuilder) {
            if (builderEntityCall != null) {
                builderEntityCall.callback(builder);
            }
            bulkBuilder.add((IndexRequestBuilder) builder);
        } else if (builder instanceof UpdateRequestBuilder) {
            if (builderEntityCall != null) {
                builderEntityCall.callback(builder);
            }
            bulkBuilder.add((UpdateRequestBuilder) builder);
        } else if (builder instanceof DeleteRequestBuilder) {
            if (builderEntityCall != null) {
                builderEntityCall.callback(builder);
            }
            bulkBuilder.add((DeleteRequestBuilder) builder);
        }
    }
    final RequestOptionCall<BulkRequestBuilder> builderCall = bulkList.getCall();
    if (builderCall != null) {
        builderCall.callback(bulkBuilder);
    }
    final BulkResponse response = bulkBuilder.execute().actionGet(bulkTimeout);
    final BulkItemResponse[] itemResponses = response.getItems();
    if (itemResponses.length != entityList.size()) {
        throw new IllegalStateException("Invalid response size: " + itemResponses.length + " != " + entityList.size());
    }
    final int[] results = new int[itemResponses.length];
    for (int i = 0; i < itemResponses.length; i++) {
        final BulkItemResponse itemResponse = itemResponses[i];
        final Entity entity = entityList.get(i);
        if (entity instanceof EsAbstractEntity) {
            ((EsAbstractEntity) entity).asDocMeta().id(itemResponse.getId());
        }
        results[i] = itemResponse.isFailed() ? 0 : 1;
    }
    return results;
}
Also used : DeleteRequestBuilder(org.elasticsearch.action.delete.DeleteRequestBuilder) Entity(org.dbflute.Entity) UpdateRequestBuilder(org.elasticsearch.action.update.UpdateRequestBuilder) BulkItemResponse(org.elasticsearch.action.bulk.BulkItemResponse) BulkResponse(org.elasticsearch.action.bulk.BulkResponse) IndexRequestBuilder(org.elasticsearch.action.index.IndexRequestBuilder) BulkRequestBuilder(org.elasticsearch.action.bulk.BulkRequestBuilder)

Example 9 with UpdateRequestBuilder

use of org.elasticsearch.action.update.UpdateRequestBuilder 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

UpdateRequestBuilder (org.elasticsearch.action.update.UpdateRequestBuilder)9 BulkItemResponse (org.elasticsearch.action.bulk.BulkItemResponse)4 BulkRequestBuilder (org.elasticsearch.action.bulk.BulkRequestBuilder)4 BulkResponse (org.elasticsearch.action.bulk.BulkResponse)4 Entity (org.dbflute.Entity)3 DeleteRequestBuilder (org.elasticsearch.action.delete.DeleteRequestBuilder)3 IndexRequestBuilder (org.elasticsearch.action.index.IndexRequestBuilder)3 Script (org.elasticsearch.script.Script)3 IOException (java.io.IOException)2 GetResponse (org.elasticsearch.action.get.GetResponse)2 UpdateResponse (org.elasticsearch.action.update.UpdateResponse)2 CompiledScript (org.elasticsearch.script.CompiledScript)2 TitanException (com.thinkaurelius.titan.core.TitanException)1 FileNotFoundException (java.io.FileNotFoundException)1 CopyOnWriteArrayList (java.util.concurrent.CopyOnWriteArrayList)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 FessEsClientException (org.codelibs.fess.es.client.FessEsClientException)1 ElasticsearchException (org.elasticsearch.ElasticsearchException)1 ElasticsearchTimeoutException (org.elasticsearch.ElasticsearchTimeoutException)1 ActionRequestValidationException (org.elasticsearch.action.ActionRequestValidationException)1