Search in sources :

Example 1 with DeleteResponse

use of org.elasticsearch.action.delete.DeleteResponse 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 2 with DeleteResponse

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

the class SimpleVersioningIT method testExternalVersioningInitialDelete.

public void testExternalVersioningInitialDelete() throws Exception {
    createIndex("test");
    ensureGreen();
    // Note - external version doesn't throw version conflicts on deletes of non existent records. This is different from internal versioning
    DeleteResponse deleteResponse = client().prepareDelete("test", "type", "1").setVersion(17).setVersionType(VersionType.EXTERNAL).execute().actionGet();
    assertEquals(DocWriteResponse.Result.NOT_FOUND, deleteResponse.getResult());
    // this should conflict with the delete command transaction which told us that the object was deleted at version 17.
    assertThrows(client().prepareIndex("test", "type", "1").setSource("field1", "value1_1").setVersion(13).setVersionType(VersionType.EXTERNAL).execute(), VersionConflictEngineException.class);
    IndexResponse indexResponse = client().prepareIndex("test", "type", "1").setSource("field1", "value1_1").setVersion(18).setVersionType(VersionType.EXTERNAL).execute().actionGet();
    assertThat(indexResponse.getVersion(), equalTo(18L));
}
Also used : DeleteResponse(org.elasticsearch.action.delete.DeleteResponse) IndexResponse(org.elasticsearch.action.index.IndexResponse)

Example 3 with DeleteResponse

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

the class SimpleVersioningIT method testInternalVersioning.

public void testInternalVersioning() throws Exception {
    createIndex("test");
    ensureGreen();
    IndexResponse indexResponse = client().prepareIndex("test", "type", "1").setSource("field1", "value1_1").execute().actionGet();
    assertThat(indexResponse.getVersion(), equalTo(1L));
    indexResponse = client().prepareIndex("test", "type", "1").setSource("field1", "value1_2").setVersion(1).execute().actionGet();
    assertThat(indexResponse.getVersion(), equalTo(2L));
    assertThrows(client().prepareIndex("test", "type", "1").setSource("field1", "value1_1").setVersion(1).execute(), VersionConflictEngineException.class);
    assertThrows(client().prepareIndex("test", "type", "1").setSource("field1", "value1_1").setVersion(1).execute(), VersionConflictEngineException.class);
    assertThrows(client().prepareIndex("test", "type", "1").setCreate(true).setSource("field1", "value1_1").execute(), VersionConflictEngineException.class);
    assertThrows(client().prepareDelete("test", "type", "1").setVersion(1).execute(), VersionConflictEngineException.class);
    assertThrows(client().prepareDelete("test", "type", "1").setVersion(1).execute(), VersionConflictEngineException.class);
    client().admin().indices().prepareRefresh().execute().actionGet();
    for (int i = 0; i < 10; i++) {
        assertThat(client().prepareGet("test", "type", "1").execute().actionGet().getVersion(), equalTo(2L));
    }
    // search with versioning
    for (int i = 0; i < 10; i++) {
        SearchResponse searchResponse = client().prepareSearch().setQuery(matchAllQuery()).setVersion(true).execute().actionGet();
        assertThat(searchResponse.getHits().getAt(0).getVersion(), equalTo(2L));
    }
    // search without versioning
    for (int i = 0; i < 10; i++) {
        SearchResponse searchResponse = client().prepareSearch().setQuery(matchAllQuery()).execute().actionGet();
        assertThat(searchResponse.getHits().getAt(0).getVersion(), equalTo(Versions.NOT_FOUND));
    }
    DeleteResponse deleteResponse = client().prepareDelete("test", "type", "1").setVersion(2).execute().actionGet();
    assertEquals(DocWriteResponse.Result.DELETED, deleteResponse.getResult());
    assertThat(deleteResponse.getVersion(), equalTo(3L));
    assertThrows(client().prepareDelete("test", "type", "1").setVersion(2).execute(), VersionConflictEngineException.class);
    // This is intricate - the object was deleted but a delete transaction was with the right version. We add another one
    // and thus the transaction is increased.
    deleteResponse = client().prepareDelete("test", "type", "1").setVersion(3).execute().actionGet();
    assertEquals(DocWriteResponse.Result.NOT_FOUND, deleteResponse.getResult());
    assertThat(deleteResponse.getVersion(), equalTo(4L));
}
Also used : DeleteResponse(org.elasticsearch.action.delete.DeleteResponse) IndexResponse(org.elasticsearch.action.index.IndexResponse) SearchResponse(org.elasticsearch.action.search.SearchResponse)

Example 4 with DeleteResponse

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

the class SimpleVersioningIT method testExternalGTE.

public void testExternalGTE() throws Exception {
    createIndex("test");
    IndexResponse indexResponse = client().prepareIndex("test", "type", "1").setSource("field1", "value1_1").setVersion(12).setVersionType(VersionType.EXTERNAL_GTE).get();
    assertThat(indexResponse.getVersion(), equalTo(12L));
    indexResponse = client().prepareIndex("test", "type", "1").setSource("field1", "value1_2").setVersion(12).setVersionType(VersionType.EXTERNAL_GTE).get();
    assertThat(indexResponse.getVersion(), equalTo(12L));
    indexResponse = client().prepareIndex("test", "type", "1").setSource("field1", "value1_2").setVersion(14).setVersionType(VersionType.EXTERNAL_GTE).get();
    assertThat(indexResponse.getVersion(), equalTo(14L));
    assertThrows(client().prepareIndex("test", "type", "1").setSource("field1", "value1_1").setVersion(13).setVersionType(VersionType.EXTERNAL_GTE), VersionConflictEngineException.class);
    client().admin().indices().prepareRefresh().execute().actionGet();
    if (randomBoolean()) {
        refresh();
    }
    for (int i = 0; i < 10; i++) {
        assertThat(client().prepareGet("test", "type", "1").get().getVersion(), equalTo(14L));
    }
    // deleting with a lower version fails.
    assertThrows(client().prepareDelete("test", "type", "1").setVersion(2).setVersionType(VersionType.EXTERNAL_GTE), VersionConflictEngineException.class);
    // Delete with a higher or equal version deletes all versions up to the given one.
    long v = randomIntBetween(14, 17);
    DeleteResponse deleteResponse = client().prepareDelete("test", "type", "1").setVersion(v).setVersionType(VersionType.EXTERNAL_GTE).execute().actionGet();
    assertEquals(DocWriteResponse.Result.DELETED, deleteResponse.getResult());
    assertThat(deleteResponse.getVersion(), equalTo(v));
    // Deleting with a lower version keeps on failing after a delete.
    assertThrows(client().prepareDelete("test", "type", "1").setVersion(2).setVersionType(VersionType.EXTERNAL_GTE).execute(), VersionConflictEngineException.class);
    // But delete with a higher version is OK.
    deleteResponse = client().prepareDelete("test", "type", "1").setVersion(18).setVersionType(VersionType.EXTERNAL_GTE).execute().actionGet();
    assertEquals(DocWriteResponse.Result.NOT_FOUND, deleteResponse.getResult());
    assertThat(deleteResponse.getVersion(), equalTo(18L));
}
Also used : DeleteResponse(org.elasticsearch.action.delete.DeleteResponse) IndexResponse(org.elasticsearch.action.index.IndexResponse)

Example 5 with DeleteResponse

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

the class SimpleVersioningIT method testExternalVersioning.

public void testExternalVersioning() throws Exception {
    createIndex("test");
    ensureGreen();
    IndexResponse indexResponse = client().prepareIndex("test", "type", "1").setSource("field1", "value1_1").setVersion(12).setVersionType(VersionType.EXTERNAL).execute().actionGet();
    assertThat(indexResponse.getVersion(), equalTo(12L));
    indexResponse = client().prepareIndex("test", "type", "1").setSource("field1", "value1_1").setVersion(14).setVersionType(VersionType.EXTERNAL).execute().actionGet();
    assertThat(indexResponse.getVersion(), equalTo(14L));
    assertThrows(client().prepareIndex("test", "type", "1").setSource("field1", "value1_1").setVersion(13).setVersionType(VersionType.EXTERNAL).execute(), VersionConflictEngineException.class);
    if (randomBoolean()) {
        refresh();
    }
    for (int i = 0; i < 10; i++) {
        assertThat(client().prepareGet("test", "type", "1").execute().actionGet().getVersion(), equalTo(14L));
    }
    // deleting with a lower version fails.
    assertThrows(client().prepareDelete("test", "type", "1").setVersion(2).setVersionType(VersionType.EXTERNAL).execute(), VersionConflictEngineException.class);
    // Delete with a higher version deletes all versions up to the given one.
    DeleteResponse deleteResponse = client().prepareDelete("test", "type", "1").setVersion(17).setVersionType(VersionType.EXTERNAL).execute().actionGet();
    assertEquals(DocWriteResponse.Result.DELETED, deleteResponse.getResult());
    assertThat(deleteResponse.getVersion(), equalTo(17L));
    // Deleting with a lower version keeps on failing after a delete.
    assertThrows(client().prepareDelete("test", "type", "1").setVersion(2).setVersionType(VersionType.EXTERNAL).execute(), VersionConflictEngineException.class);
    // But delete with a higher version is OK.
    deleteResponse = client().prepareDelete("test", "type", "1").setVersion(18).setVersionType(VersionType.EXTERNAL).execute().actionGet();
    assertEquals(DocWriteResponse.Result.NOT_FOUND, deleteResponse.getResult());
    assertThat(deleteResponse.getVersion(), equalTo(18L));
    // TODO: This behavior breaks rest api returning http status 201, good news is that it this is only the case until deletes GC kicks in.
    indexResponse = client().prepareIndex("test", "type", "1").setSource("field1", "value1_1").setVersion(19).setVersionType(VersionType.EXTERNAL).execute().actionGet();
    assertThat(indexResponse.getVersion(), equalTo(19L));
    deleteResponse = client().prepareDelete("test", "type", "1").setVersion(20).setVersionType(VersionType.EXTERNAL).execute().actionGet();
    assertEquals(DocWriteResponse.Result.DELETED, deleteResponse.getResult());
    assertThat(deleteResponse.getVersion(), equalTo(20L));
    // Make sure that the next delete will be GC. Note we do it on the index settings so it will be cleaned up
    HashMap<String, Object> newSettings = new HashMap<>();
    newSettings.put("index.gc_deletes", -1);
    client().admin().indices().prepareUpdateSettings("test").setSettings(newSettings).execute().actionGet();
    // gc works based on estimated sampled time. Give it a chance...
    Thread.sleep(300);
    // And now we have previous version return -1
    indexResponse = client().prepareIndex("test", "type", "1").setSource("field1", "value1_1").setVersion(20).setVersionType(VersionType.EXTERNAL).execute().actionGet();
    assertThat(indexResponse.getVersion(), equalTo(20L));
}
Also used : DeleteResponse(org.elasticsearch.action.delete.DeleteResponse) IndexResponse(org.elasticsearch.action.index.IndexResponse) HashMap(java.util.HashMap)

Aggregations

DeleteResponse (org.elasticsearch.action.delete.DeleteResponse)42 Test (org.junit.Test)14 IndexResponse (org.elasticsearch.action.index.IndexResponse)13 GetResponse (org.elasticsearch.action.get.GetResponse)11 DeleteRequest (org.elasticsearch.action.delete.DeleteRequest)8 HashMap (java.util.HashMap)7 DeleteRequestBuilder (org.elasticsearch.action.delete.DeleteRequestBuilder)7 MultiGetResponse (org.elasticsearch.action.get.MultiGetResponse)6 SearchResponse (org.elasticsearch.action.search.SearchResponse)6 MockFlowFile (org.apache.nifi.util.MockFlowFile)5 ElasticsearchException (org.elasticsearch.ElasticsearchException)5 IndexRequest (org.elasticsearch.action.index.IndexRequest)5 IOException (java.io.IOException)4 ExecutionException (java.util.concurrent.ExecutionException)3 ElasticsearchTimeoutException (org.elasticsearch.ElasticsearchTimeoutException)3 UpdateResponse (org.elasticsearch.action.update.UpdateResponse)3 ArrayList (java.util.ArrayList)2 Map (java.util.Map)2 CamelContext (org.apache.camel.CamelContext)2 ProducerTemplate (org.apache.camel.ProducerTemplate)2