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"));
}
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")));
}
}
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();
}
}
Aggregations