Search in sources :

Example 6 with DeleteRequest

use of org.elasticsearch.action.delete.DeleteRequest in project elasticsearch by elastic.

the class TransportShardBulkActionTests method testExecuteBulkDeleteRequest.

public void testExecuteBulkDeleteRequest() throws Exception {
    IndexMetaData metaData = indexMetaData();
    IndexShard shard = newStartedShard(true);
    BulkItemRequest[] items = new BulkItemRequest[1];
    DocWriteRequest writeRequest = new DeleteRequest("index", "type", "id");
    items[0] = new BulkItemRequest(0, writeRequest);
    BulkShardRequest bulkShardRequest = new BulkShardRequest(shardId, RefreshPolicy.NONE, items);
    Translog.Location location = new Translog.Location(0, 0, 0);
    UpdateHelper updateHelper = null;
    Translog.Location newLocation = TransportShardBulkAction.executeBulkItemRequest(metaData, shard, bulkShardRequest, location, 0, updateHelper, threadPool::absoluteTimeInMillis, new NoopMappingUpdatePerformer());
    // Translog changes, even though the document didn't exist
    assertThat(newLocation, not(location));
    BulkItemRequest replicaRequest = bulkShardRequest.items()[0];
    DocWriteRequest replicaDeleteRequest = replicaRequest.request();
    BulkItemResponse primaryResponse = replicaRequest.getPrimaryResponse();
    DeleteResponse response = primaryResponse.getResponse();
    // Any version can be matched on replica
    assertThat(replicaDeleteRequest.version(), equalTo(Versions.MATCH_ANY));
    assertThat(replicaDeleteRequest.versionType(), equalTo(VersionType.INTERNAL));
    assertThat(primaryResponse.getItemId(), equalTo(0));
    assertThat(primaryResponse.getId(), equalTo("id"));
    assertThat(primaryResponse.getOpType(), equalTo(DocWriteRequest.OpType.DELETE));
    assertFalse(primaryResponse.isFailed());
    assertThat(response.getResult(), equalTo(DocWriteResponse.Result.NOT_FOUND));
    assertThat(response.getShardId(), equalTo(shard.shardId()));
    assertThat(response.getIndex(), equalTo("index"));
    assertThat(response.getType(), equalTo("type"));
    assertThat(response.getId(), equalTo("id"));
    assertThat(response.getVersion(), equalTo(1L));
    assertThat(response.getSeqNo(), equalTo(0L));
    assertThat(response.forcedRefresh(), equalTo(false));
    // Now do the same after indexing the document, it should now find and delete the document
    indexDoc(shard, "type", "id", "{\"foo\": \"bar\"}");
    writeRequest = new DeleteRequest("index", "type", "id");
    items[0] = new BulkItemRequest(0, writeRequest);
    bulkShardRequest = new BulkShardRequest(shardId, RefreshPolicy.NONE, items);
    location = newLocation;
    newLocation = TransportShardBulkAction.executeBulkItemRequest(metaData, shard, bulkShardRequest, location, 0, updateHelper, threadPool::absoluteTimeInMillis, new NoopMappingUpdatePerformer());
    // Translog changes, because the document was deleted
    assertThat(newLocation, not(location));
    replicaRequest = bulkShardRequest.items()[0];
    replicaDeleteRequest = replicaRequest.request();
    primaryResponse = replicaRequest.getPrimaryResponse();
    response = primaryResponse.getResponse();
    // Any version can be matched on replica
    assertThat(replicaDeleteRequest.version(), equalTo(Versions.MATCH_ANY));
    assertThat(replicaDeleteRequest.versionType(), equalTo(VersionType.INTERNAL));
    assertThat(primaryResponse.getItemId(), equalTo(0));
    assertThat(primaryResponse.getId(), equalTo("id"));
    assertThat(primaryResponse.getOpType(), equalTo(DocWriteRequest.OpType.DELETE));
    assertFalse(primaryResponse.isFailed());
    assertThat(response.getResult(), equalTo(DocWriteResponse.Result.DELETED));
    assertThat(response.getShardId(), equalTo(shard.shardId()));
    assertThat(response.getIndex(), equalTo("index"));
    assertThat(response.getType(), equalTo("type"));
    assertThat(response.getId(), equalTo("id"));
    assertThat(response.getVersion(), equalTo(3L));
    assertThat(response.getSeqNo(), equalTo(2L));
    assertThat(response.forcedRefresh(), equalTo(false));
    assertDocCount(shard, 0);
    closeShards(shard);
}
Also used : IndexShard(org.elasticsearch.index.shard.IndexShard) IndexMetaData(org.elasticsearch.cluster.metadata.IndexMetaData) Translog(org.elasticsearch.index.translog.Translog) UpdateHelper(org.elasticsearch.action.update.UpdateHelper) DeleteResponse(org.elasticsearch.action.delete.DeleteResponse) DocWriteRequest(org.elasticsearch.action.DocWriteRequest) DeleteRequest(org.elasticsearch.action.delete.DeleteRequest)

Example 7 with DeleteRequest

use of org.elasticsearch.action.delete.DeleteRequest in project elasticsearch by elastic.

the class BulkRequestTests method testBulkAddIterable.

public void testBulkAddIterable() {
    BulkRequest bulkRequest = Requests.bulkRequest();
    List<DocWriteRequest> requests = new ArrayList<>();
    requests.add(new IndexRequest("test", "test", "id").source(Requests.INDEX_CONTENT_TYPE, "field", "value"));
    requests.add(new UpdateRequest("test", "test", "id").doc(Requests.INDEX_CONTENT_TYPE, "field", "value"));
    requests.add(new DeleteRequest("test", "test", "id"));
    bulkRequest.add(requests);
    assertThat(bulkRequest.requests().size(), equalTo(3));
    assertThat(bulkRequest.requests().get(0), instanceOf(IndexRequest.class));
    assertThat(bulkRequest.requests().get(1), instanceOf(UpdateRequest.class));
    assertThat(bulkRequest.requests().get(2), instanceOf(DeleteRequest.class));
}
Also used : UpdateRequest(org.elasticsearch.action.update.UpdateRequest) ArrayList(java.util.ArrayList) DocWriteRequest(org.elasticsearch.action.DocWriteRequest) IndexRequest(org.elasticsearch.action.index.IndexRequest) DeleteRequest(org.elasticsearch.action.delete.DeleteRequest)

Example 8 with DeleteRequest

use of org.elasticsearch.action.delete.DeleteRequest in project elasticsearch by elastic.

the class BulkWithUpdatesIT method testThatFailedUpdateRequestReturnsCorrectType.

// issue 6630
public void testThatFailedUpdateRequestReturnsCorrectType() throws Exception {
    BulkResponse indexBulkItemResponse = client().prepareBulk().add(new IndexRequest("test", "type", "3").source("{ \"title\" : \"Great Title of doc 3\" }", XContentType.JSON)).add(new IndexRequest("test", "type", "4").source("{ \"title\" : \"Great Title of doc 4\" }", XContentType.JSON)).add(new IndexRequest("test", "type", "5").source("{ \"title\" : \"Great Title of doc 5\" }", XContentType.JSON)).add(new IndexRequest("test", "type", "6").source("{ \"title\" : \"Great Title of doc 6\" }", XContentType.JSON)).setRefreshPolicy(RefreshPolicy.IMMEDIATE).get();
    assertNoFailures(indexBulkItemResponse);
    BulkResponse bulkItemResponse = client().prepareBulk().add(new IndexRequest("test", "type", "1").source("{ \"title\" : \"Great Title of doc 1\" }", XContentType.JSON)).add(new IndexRequest("test", "type", "2").source("{ \"title\" : \"Great Title of doc 2\" }", XContentType.JSON)).add(new UpdateRequest("test", "type", "3").doc("{ \"date\" : \"2014-01-30T23:59:57\"}", XContentType.JSON)).add(new UpdateRequest("test", "type", "4").doc("{ \"date\" : \"2014-13-30T23:59:57\"}", XContentType.JSON)).add(new DeleteRequest("test", "type", "5")).add(new DeleteRequest("test", "type", "6")).get();
    assertNoFailures(indexBulkItemResponse);
    assertThat(bulkItemResponse.getItems().length, is(6));
    assertThat(bulkItemResponse.getItems()[0].getOpType(), is(OpType.INDEX));
    assertThat(bulkItemResponse.getItems()[1].getOpType(), is(OpType.INDEX));
    assertThat(bulkItemResponse.getItems()[2].getOpType(), is(OpType.UPDATE));
    assertThat(bulkItemResponse.getItems()[3].getOpType(), is(OpType.UPDATE));
    assertThat(bulkItemResponse.getItems()[4].getOpType(), is(OpType.DELETE));
    assertThat(bulkItemResponse.getItems()[5].getOpType(), is(OpType.DELETE));
}
Also used : UpdateRequest(org.elasticsearch.action.update.UpdateRequest) IndexRequest(org.elasticsearch.action.index.IndexRequest) DeleteRequest(org.elasticsearch.action.delete.DeleteRequest)

Example 9 with DeleteRequest

use of org.elasticsearch.action.delete.DeleteRequest in project elasticsearch by elastic.

the class BulkWithUpdatesIT method testThatMissingIndexDoesNotAbortFullBulkRequest.

// issue 6410
public void testThatMissingIndexDoesNotAbortFullBulkRequest() throws Exception {
    createIndex("bulkindex1", "bulkindex2");
    BulkRequest bulkRequest = new BulkRequest();
    bulkRequest.add(new IndexRequest("bulkindex1", "index1_type", "1").source(Requests.INDEX_CONTENT_TYPE, "text", "hallo1")).add(new IndexRequest("bulkindex2", "index2_type", "1").source(Requests.INDEX_CONTENT_TYPE, "text", "hallo2")).add(new IndexRequest("bulkindex2", "index2_type").source(Requests.INDEX_CONTENT_TYPE, "text", "hallo2")).add(new UpdateRequest("bulkindex2", "index2_type", "2").doc(Requests.INDEX_CONTENT_TYPE, "foo", "bar")).add(new DeleteRequest("bulkindex2", "index2_type", "3")).setRefreshPolicy(RefreshPolicy.IMMEDIATE);
    client().bulk(bulkRequest).get();
    SearchResponse searchResponse = client().prepareSearch("bulkindex*").get();
    assertHitCount(searchResponse, 3);
    assertAcked(client().admin().indices().prepareClose("bulkindex2"));
    BulkResponse bulkResponse = client().bulk(bulkRequest).get();
    assertThat(bulkResponse.hasFailures(), is(true));
    assertThat(bulkResponse.getItems().length, is(5));
}
Also used : UpdateRequest(org.elasticsearch.action.update.UpdateRequest) IndexRequest(org.elasticsearch.action.index.IndexRequest) DeleteRequest(org.elasticsearch.action.delete.DeleteRequest) SearchResponse(org.elasticsearch.action.search.SearchResponse)

Example 10 with DeleteRequest

use of org.elasticsearch.action.delete.DeleteRequest in project sonarqube by SonarSource.

the class ProxyBulkRequestBuilder method toString.

@Override
public String toString() {
    StringBuilder message = new StringBuilder();
    message.append("Bulk[");
    Multiset<BulkRequestKey> groupedRequests = LinkedHashMultiset.create();
    for (int i = 0; i < request.requests().size(); i++) {
        ActionRequest<?> item = request.requests().get(i);
        String requestType;
        String index;
        String docType;
        if (item instanceof IndexRequest) {
            IndexRequest request = (IndexRequest) item;
            requestType = "index";
            index = request.index();
            docType = request.type();
        } else if (item instanceof UpdateRequest) {
            UpdateRequest request = (UpdateRequest) item;
            requestType = "update";
            index = request.index();
            docType = request.type();
        } else if (item instanceof DeleteRequest) {
            DeleteRequest request = (DeleteRequest) item;
            requestType = "delete";
            index = request.index();
            docType = request.type();
        } else {
            // Cannot happen, not allowed by BulkRequest's contract
            throw new IllegalStateException("Unsupported bulk request type: " + item.getClass());
        }
        groupedRequests.add(new BulkRequestKey(requestType, index, docType));
    }
    Set<Multiset.Entry<BulkRequestKey>> entrySet = groupedRequests.entrySet();
    int size = entrySet.size();
    int current = 0;
    for (Multiset.Entry<BulkRequestKey> requestEntry : entrySet) {
        message.append(requestEntry.getCount()).append(" ").append(requestEntry.getElement().toString());
        current++;
        if (current < size) {
            message.append(", ");
        }
    }
    message.append("]");
    return message.toString();
}
Also used : UpdateRequest(org.elasticsearch.action.update.UpdateRequest) IndexRequest(org.elasticsearch.action.index.IndexRequest) Multiset(com.google.common.collect.Multiset) LinkedHashMultiset(com.google.common.collect.LinkedHashMultiset) DeleteRequest(org.elasticsearch.action.delete.DeleteRequest)

Aggregations

DeleteRequest (org.elasticsearch.action.delete.DeleteRequest)33 IndexRequest (org.elasticsearch.action.index.IndexRequest)25 UpdateRequest (org.elasticsearch.action.update.UpdateRequest)18 DocWriteRequest (org.elasticsearch.action.DocWriteRequest)8 IOException (java.io.IOException)7 BulkRequest (org.elasticsearch.action.bulk.BulkRequest)7 DeleteResponse (org.elasticsearch.action.delete.DeleteResponse)7 ArrayList (java.util.ArrayList)5 DeleteIndexRequest (org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest)5 HashMap (java.util.HashMap)4 Map (java.util.Map)4 BulkItemResponse (org.elasticsearch.action.bulk.BulkItemResponse)4 GetRequest (org.elasticsearch.action.get.GetRequest)4 BytesReference (org.elasticsearch.common.bytes.BytesReference)4 XContentType (org.elasticsearch.common.xcontent.XContentType)4 BulkRequestBuilder (org.elasticsearch.action.bulk.BulkRequestBuilder)3 BulkResponse (org.elasticsearch.action.bulk.BulkResponse)3 VersionConflictEngineException (org.elasticsearch.index.engine.VersionConflictEngineException)3 TitanException (com.thinkaurelius.titan.core.TitanException)2 FileNotFoundException (java.io.FileNotFoundException)2