Search in sources :

Example 6 with FlushResponse

use of org.elasticsearch.action.admin.indices.flush.FlushResponse in project elasticsearch by elastic.

the class GetActionIT method testGetFieldsComplexField.

public void testGetFieldsComplexField() throws Exception {
    assertAcked(prepareCreate("my-index").setSettings(Settings.builder().put("index.refresh_interval", -1)).addMapping("my-type2", jsonBuilder().startObject().startObject("my-type2").startObject("properties").startObject("field1").field("type", "object").startObject("properties").startObject("field2").field("type", "object").startObject("properties").startObject("field3").field("type", "object").startObject("properties").startObject("field4").field("type", "text").field("store", true).endObject().endObject().endObject().endObject().endObject().endObject().endObject().endObject().endObject().endObject()));
    BytesReference source = jsonBuilder().startObject().startArray("field1").startObject().startObject("field2").startArray("field3").startObject().field("field4", "value1").endObject().endArray().endObject().endObject().startObject().startObject("field2").startArray("field3").startObject().field("field4", "value2").endObject().endArray().endObject().endObject().endArray().endObject().bytes();
    logger.info("indexing documents");
    client().prepareIndex("my-index", "my-type1", "1").setSource(source, XContentType.JSON).get();
    client().prepareIndex("my-index", "my-type2", "1").setSource(source, XContentType.JSON).get();
    logger.info("checking real time retrieval");
    String field = "field1.field2.field3.field4";
    GetResponse getResponse = client().prepareGet("my-index", "my-type1", "1").setStoredFields(field).get();
    assertThat(getResponse.isExists(), equalTo(true));
    assertThat(getResponse.getField(field).isMetadataField(), equalTo(false));
    assertThat(getResponse.getField(field).getValues().size(), equalTo(2));
    assertThat(getResponse.getField(field).getValues().get(0).toString(), equalTo("value1"));
    assertThat(getResponse.getField(field).getValues().get(1).toString(), equalTo("value2"));
    getResponse = client().prepareGet("my-index", "my-type2", "1").setStoredFields(field).get();
    assertThat(getResponse.isExists(), equalTo(true));
    assertThat(getResponse.getField(field).isMetadataField(), equalTo(false));
    assertThat(getResponse.getField(field).getValues().size(), equalTo(2));
    assertThat(getResponse.getField(field).getValues().get(0).toString(), equalTo("value1"));
    assertThat(getResponse.getField(field).getValues().get(1).toString(), equalTo("value2"));
    logger.info("waiting for recoveries to complete");
    // Flush fails if shard has ongoing recoveries, make sure the cluster is settled down
    ensureGreen();
    logger.info("flushing");
    FlushResponse flushResponse = client().admin().indices().prepareFlush("my-index").setForce(true).get();
    if (flushResponse.getSuccessfulShards() == 0) {
        StringBuilder sb = new StringBuilder("failed to flush at least one shard. total shards [").append(flushResponse.getTotalShards()).append("], failed shards: [").append(flushResponse.getFailedShards()).append("]");
        for (ShardOperationFailedException failure : flushResponse.getShardFailures()) {
            sb.append("\nShard failure: ").append(failure);
        }
        fail(sb.toString());
    }
    logger.info("checking post-flush retrieval");
    getResponse = client().prepareGet("my-index", "my-type1", "1").setStoredFields(field).get();
    assertThat(getResponse.isExists(), equalTo(true));
    assertThat(getResponse.getField(field).isMetadataField(), equalTo(false));
    assertThat(getResponse.getField(field).getValues().size(), equalTo(2));
    assertThat(getResponse.getField(field).getValues().get(0).toString(), equalTo("value1"));
    assertThat(getResponse.getField(field).getValues().get(1).toString(), equalTo("value2"));
    getResponse = client().prepareGet("my-index", "my-type2", "1").setStoredFields(field).get();
    assertThat(getResponse.isExists(), equalTo(true));
    assertThat(getResponse.getField(field).isMetadataField(), equalTo(false));
    assertThat(getResponse.getField(field).getValues().size(), equalTo(2));
    assertThat(getResponse.getField(field).getValues().get(0).toString(), equalTo("value1"));
    assertThat(getResponse.getField(field).getValues().get(1).toString(), equalTo("value2"));
}
Also used : BytesReference(org.elasticsearch.common.bytes.BytesReference) FlushResponse(org.elasticsearch.action.admin.indices.flush.FlushResponse) ShardOperationFailedException(org.elasticsearch.action.ShardOperationFailedException) GetResponse(org.elasticsearch.action.get.GetResponse) MultiGetResponse(org.elasticsearch.action.get.MultiGetResponse)

Example 7 with FlushResponse

use of org.elasticsearch.action.admin.indices.flush.FlushResponse in project elasticsearch by elastic.

the class SimpleRecoveryIT method testSimpleRecovery.

public void testSimpleRecovery() throws Exception {
    assertAcked(prepareCreate("test", 1).execute().actionGet());
    NumShards numShards = getNumShards("test");
    client().index(indexRequest("test").type("type1").id("1").source(source("1", "test"), XContentType.JSON)).actionGet();
    FlushResponse flushResponse = client().admin().indices().flush(flushRequest("test")).actionGet();
    assertThat(flushResponse.getTotalShards(), equalTo(numShards.totalNumShards));
    assertThat(flushResponse.getSuccessfulShards(), equalTo(numShards.numPrimaries));
    assertThat(flushResponse.getFailedShards(), equalTo(0));
    client().index(indexRequest("test").type("type1").id("2").source(source("2", "test"), XContentType.JSON)).actionGet();
    RefreshResponse refreshResponse = client().admin().indices().refresh(refreshRequest("test")).actionGet();
    assertThat(refreshResponse.getTotalShards(), equalTo(numShards.totalNumShards));
    assertThat(refreshResponse.getSuccessfulShards(), equalTo(numShards.numPrimaries));
    assertThat(refreshResponse.getFailedShards(), equalTo(0));
    allowNodes("test", 2);
    logger.info("Running Cluster Health");
    ensureGreen();
    GetResponse getResult;
    for (int i = 0; i < 5; i++) {
        getResult = client().get(getRequest("test").type("type1").id("1").operationThreaded(false)).actionGet();
        assertThat(getResult.getSourceAsString(), equalTo(source("1", "test")));
        getResult = client().get(getRequest("test").type("type1").id("1").operationThreaded(false)).actionGet();
        assertThat(getResult.getSourceAsString(), equalTo(source("1", "test")));
        getResult = client().get(getRequest("test").type("type1").id("2").operationThreaded(true)).actionGet();
        assertThat(getResult.getSourceAsString(), equalTo(source("2", "test")));
        getResult = client().get(getRequest("test").type("type1").id("2").operationThreaded(true)).actionGet();
        assertThat(getResult.getSourceAsString(), equalTo(source("2", "test")));
    }
    // now start another one so we move some primaries
    allowNodes("test", 3);
    Thread.sleep(200);
    logger.info("Running Cluster Health");
    ensureGreen();
    for (int i = 0; i < 5; i++) {
        getResult = client().get(getRequest("test").type("type1").id("1")).actionGet();
        assertThat(getResult.getSourceAsString(), equalTo(source("1", "test")));
        getResult = client().get(getRequest("test").type("type1").id("1")).actionGet();
        assertThat(getResult.getSourceAsString(), equalTo(source("1", "test")));
        getResult = client().get(getRequest("test").type("type1").id("1")).actionGet();
        assertThat(getResult.getSourceAsString(), equalTo(source("1", "test")));
        getResult = client().get(getRequest("test").type("type1").id("2").operationThreaded(true)).actionGet();
        assertThat(getResult.getSourceAsString(), equalTo(source("2", "test")));
        getResult = client().get(getRequest("test").type("type1").id("2").operationThreaded(true)).actionGet();
        assertThat(getResult.getSourceAsString(), equalTo(source("2", "test")));
        getResult = client().get(getRequest("test").type("type1").id("2").operationThreaded(true)).actionGet();
        assertThat(getResult.getSourceAsString(), equalTo(source("2", "test")));
    }
}
Also used : FlushResponse(org.elasticsearch.action.admin.indices.flush.FlushResponse) RefreshResponse(org.elasticsearch.action.admin.indices.refresh.RefreshResponse) GetResponse(org.elasticsearch.action.get.GetResponse)

Example 8 with FlushResponse

use of org.elasticsearch.action.admin.indices.flush.FlushResponse in project elasticsearch by elastic.

the class SharedClusterSnapshotRestoreIT method testBasicWorkFlow.

public void testBasicWorkFlow() throws Exception {
    Client client = client();
    logger.info("-->  creating repository");
    assertAcked(client.admin().cluster().preparePutRepository("test-repo").setType("fs").setSettings(Settings.builder().put("location", randomRepoPath()).put("compress", randomBoolean()).put("chunk_size", randomIntBetween(100, 1000), ByteSizeUnit.BYTES)));
    createIndex("test-idx-1", "test-idx-2", "test-idx-3");
    ensureGreen();
    logger.info("--> indexing some data");
    for (int i = 0; i < 100; i++) {
        index("test-idx-1", "doc", Integer.toString(i), "foo", "bar" + i);
        index("test-idx-2", "doc", Integer.toString(i), "foo", "baz" + i);
        index("test-idx-3", "doc", Integer.toString(i), "foo", "baz" + i);
    }
    refresh();
    assertHitCount(client.prepareSearch("test-idx-1").setSize(0).get(), 100L);
    assertHitCount(client.prepareSearch("test-idx-2").setSize(0).get(), 100L);
    assertHitCount(client.prepareSearch("test-idx-3").setSize(0).get(), 100L);
    ListenableActionFuture<FlushResponse> flushResponseFuture = null;
    if (randomBoolean()) {
        ArrayList<String> indicesToFlush = new ArrayList<>();
        for (int i = 1; i < 4; i++) {
            if (randomBoolean()) {
                indicesToFlush.add("test-idx-" + i);
            }
        }
        if (!indicesToFlush.isEmpty()) {
            String[] indices = indicesToFlush.toArray(new String[indicesToFlush.size()]);
            logger.info("--> starting asynchronous flush for indices {}", Arrays.toString(indices));
            flushResponseFuture = client.admin().indices().prepareFlush(indices).execute();
        }
    }
    logger.info("--> snapshot");
    CreateSnapshotResponse createSnapshotResponse = client.admin().cluster().prepareCreateSnapshot("test-repo", "test-snap").setWaitForCompletion(true).setIndices("test-idx-*", "-test-idx-3").get();
    assertThat(createSnapshotResponse.getSnapshotInfo().successfulShards(), greaterThan(0));
    assertThat(createSnapshotResponse.getSnapshotInfo().successfulShards(), equalTo(createSnapshotResponse.getSnapshotInfo().totalShards()));
    List<SnapshotInfo> snapshotInfos = client.admin().cluster().prepareGetSnapshots("test-repo").setSnapshots(randomFrom("test-snap", "_all", "*", "*-snap", "test*")).get().getSnapshots();
    assertThat(snapshotInfos.size(), equalTo(1));
    SnapshotInfo snapshotInfo = snapshotInfos.get(0);
    assertThat(snapshotInfo.state(), equalTo(SnapshotState.SUCCESS));
    assertThat(snapshotInfo.version(), equalTo(Version.CURRENT));
    logger.info("--> delete some data");
    for (int i = 0; i < 50; i++) {
        client.prepareDelete("test-idx-1", "doc", Integer.toString(i)).get();
    }
    for (int i = 50; i < 100; i++) {
        client.prepareDelete("test-idx-2", "doc", Integer.toString(i)).get();
    }
    for (int i = 0; i < 100; i += 2) {
        client.prepareDelete("test-idx-3", "doc", Integer.toString(i)).get();
    }
    assertAllSuccessful(refresh());
    assertHitCount(client.prepareSearch("test-idx-1").setSize(0).get(), 50L);
    assertHitCount(client.prepareSearch("test-idx-2").setSize(0).get(), 50L);
    assertHitCount(client.prepareSearch("test-idx-3").setSize(0).get(), 50L);
    logger.info("--> close indices");
    client.admin().indices().prepareClose("test-idx-1", "test-idx-2").get();
    logger.info("--> restore all indices from the snapshot");
    RestoreSnapshotResponse restoreSnapshotResponse = client.admin().cluster().prepareRestoreSnapshot("test-repo", "test-snap").setWaitForCompletion(true).execute().actionGet();
    assertThat(restoreSnapshotResponse.getRestoreInfo().totalShards(), greaterThan(0));
    ensureGreen();
    for (int i = 0; i < 5; i++) {
        assertHitCount(client.prepareSearch("test-idx-1").setSize(0).get(), 100L);
        assertHitCount(client.prepareSearch("test-idx-2").setSize(0).get(), 100L);
        assertHitCount(client.prepareSearch("test-idx-3").setSize(0).get(), 50L);
    }
    // Test restore after index deletion
    logger.info("--> delete indices");
    cluster().wipeIndices("test-idx-1", "test-idx-2");
    logger.info("--> restore one index after deletion");
    restoreSnapshotResponse = client.admin().cluster().prepareRestoreSnapshot("test-repo", "test-snap").setWaitForCompletion(true).setIndices("test-idx-*", "-test-idx-2").execute().actionGet();
    assertThat(restoreSnapshotResponse.getRestoreInfo().totalShards(), greaterThan(0));
    ensureGreen();
    for (int i = 0; i < 5; i++) {
        assertHitCount(client.prepareSearch("test-idx-1").setSize(0).get(), 100L);
    }
    ClusterState clusterState = client.admin().cluster().prepareState().get().getState();
    assertThat(clusterState.getMetaData().hasIndex("test-idx-1"), equalTo(true));
    assertThat(clusterState.getMetaData().hasIndex("test-idx-2"), equalTo(false));
    if (flushResponseFuture != null) {
        // Finish flush
        flushResponseFuture.actionGet();
    }
}
Also used : ClusterState(org.elasticsearch.cluster.ClusterState) FlushResponse(org.elasticsearch.action.admin.indices.flush.FlushResponse) ArrayList(java.util.ArrayList) Matchers.containsString(org.hamcrest.Matchers.containsString) RestoreSnapshotResponse(org.elasticsearch.action.admin.cluster.snapshots.restore.RestoreSnapshotResponse) CreateSnapshotResponse(org.elasticsearch.action.admin.cluster.snapshots.create.CreateSnapshotResponse) Client(org.elasticsearch.client.Client)

Aggregations

FlushResponse (org.elasticsearch.action.admin.indices.flush.FlushResponse)8 GetResponse (org.elasticsearch.action.get.GetResponse)3 IOException (java.io.IOException)2 ShardOperationFailedException (org.elasticsearch.action.ShardOperationFailedException)2 FlushRequest (org.elasticsearch.action.admin.indices.flush.FlushRequest)2 RefreshResponse (org.elasticsearch.action.admin.indices.refresh.RefreshResponse)2 ArrayList (java.util.ArrayList)1 Date (java.util.Date)1 CopyOnWriteArrayList (java.util.concurrent.CopyOnWriteArrayList)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 ExecutionException (java.util.concurrent.ExecutionException)1 CreateSnapshotResponse (org.elasticsearch.action.admin.cluster.snapshots.create.CreateSnapshotResponse)1 RestoreSnapshotResponse (org.elasticsearch.action.admin.cluster.snapshots.restore.RestoreSnapshotResponse)1 ClearIndicesCacheResponse (org.elasticsearch.action.admin.indices.cache.clear.ClearIndicesCacheResponse)1 SyncedFlushResponse (org.elasticsearch.action.admin.indices.flush.SyncedFlushResponse)1 ForceMergeResponse (org.elasticsearch.action.admin.indices.forcemerge.ForceMergeResponse)1 DeleteResponse (org.elasticsearch.action.delete.DeleteResponse)1 MultiGetResponse (org.elasticsearch.action.get.MultiGetResponse)1 IndexResponse (org.elasticsearch.action.index.IndexResponse)1 SearchResponse (org.elasticsearch.action.search.SearchResponse)1