@Test public void testInvalidSettings() throws Exception { // clean all templates setup by the framework. client().admin().indices().prepareDeleteTemplate("*").get(); // check get all templates on an empty index. GetIndexTemplatesResponse response = client().admin().indices().prepareGetTemplates().get(); assertThat(response.getIndexTemplates(), empty()); client() .admin() .indices() .preparePutTemplate("template_1") .setTemplate("te*") .setSettings(ImmutableSettings.builder().put("does_not_exist", "test")) .get(); response = client().admin().indices().prepareGetTemplates().get(); assertThat(response.getIndexTemplates(), hasSize(1)); assertThat(response.getIndexTemplates().get(0).getSettings().getAsMap().size(), equalTo(1)); assertThat( response.getIndexTemplates().get(0).getSettings().get("index.does_not_exist"), equalTo("test")); createIndex("test"); // the wrong setting has no effect but does get stored among the index settings GetSettingsResponse getSettingsResponse = client().admin().indices().prepareGetSettings("test").get(); assertThat( getSettingsResponse.getIndexToSettings().get("test").getAsMap().get("index.does_not_exist"), equalTo("test")); }
@Test public void testDeleteWarmerAcknowledgement() { createIndex("test"); index("test", "type", "1", "f", 1); assertAcked( client() .admin() .indices() .preparePutWarmer("custom_warmer") .setSearchRequest( client() .prepareSearch("test") .setTypes("test") .setQuery(QueryBuilders.matchAllQuery()))); assertAcked( client() .admin() .indices() .prepareDeleteWarmer() .setIndices("test") .setNames("custom_warmer")); for (Client client : clients()) { GetWarmersResponse getWarmersResponse = client.admin().indices().prepareGetWarmers().setLocal(true).get(); assertThat(getWarmersResponse.warmers().size(), equalTo(0)); } }
@Test public void testAliasInvalidFilterValidJson() throws Exception { // invalid filter but valid json: put index template works fine, fails during index creation client() .admin() .indices() .preparePutTemplate("template_1") .setTemplate("te*") .addAlias(new Alias("invalid_alias").filter("{ \"invalid\": {} }")) .get(); GetIndexTemplatesResponse response = client().admin().indices().prepareGetTemplates("template_1").get(); assertThat(response.getIndexTemplates().size(), equalTo(1)); assertThat(response.getIndexTemplates().get(0).getAliases().size(), equalTo(1)); assertThat( response.getIndexTemplates().get(0).getAliases().get("invalid_alias").filter().string(), equalTo("{\"invalid\":{}}")); try { createIndex("test"); fail( "index creation should have failed due to invalid alias filter in matching index template"); } catch (ElasticsearchIllegalArgumentException e) { assertThat(e.getMessage(), equalTo("failed to parse filter for alias [invalid_alias]")); assertThat(e.getCause(), instanceOf(QueryParsingException.class)); assertThat(e.getCause().getMessage(), equalTo("[test] No filter registered for [invalid]")); } }
@Test public void testUnassignedShardAndEmptyNodesInRoutingTable() throws Exception { internalCluster().startNode(); createIndex("a"); ensureSearchable("a"); ClusterState current = clusterService().state(); GatewayAllocator allocator = internalCluster().getInstance(GatewayAllocator.class); AllocationDeciders allocationDeciders = new AllocationDeciders(Settings.EMPTY, new AllocationDecider[0]); RoutingNodes routingNodes = new RoutingNodes( ClusterState.builder(current) .routingTable( RoutingTable.builder(current.routingTable()) .remove("a") .addAsRecovery(current.metaData().index("a"))) .nodes(DiscoveryNodes.EMPTY_NODES) .build()); ClusterInfo clusterInfo = new ClusterInfo(ImmutableMap.<String, DiskUsage>of(), ImmutableMap.<String, Long>of()); RoutingAllocation routingAllocation = new RoutingAllocation(allocationDeciders, routingNodes, current.nodes(), clusterInfo); allocator.allocateUnassigned(routingAllocation); }
@Test public void testBrokenMapping() throws Exception { // clean all templates setup by the framework. client().admin().indices().prepareDeleteTemplate("*").get(); // check get all templates on an empty index. GetIndexTemplatesResponse response = client().admin().indices().prepareGetTemplates().get(); assertThat(response.getIndexTemplates(), empty()); client() .admin() .indices() .preparePutTemplate("template_1") .setTemplate("te*") .addMapping("type1", "abcde") .get(); response = client().admin().indices().prepareGetTemplates().get(); assertThat(response.getIndexTemplates(), hasSize(1)); assertThat(response.getIndexTemplates().get(0).getMappings().size(), equalTo(1)); assertThat( response.getIndexTemplates().get(0).getMappings().get("type1").string(), equalTo("abcde")); try { createIndex("test"); fail("create index should have failed due to broken index templates mapping"); } catch (ElasticsearchParseException e) { // everything fine } }
public void testCloseAndReopenOrDeleteWithActiveScroll() throws IOException { createIndex("test"); for (int i = 0; i < 100; i++) { client() .prepareIndex("test", "type1", Integer.toString(i)) .setSource(jsonBuilder().startObject().field("field", i).endObject()) .execute() .actionGet(); } refresh(); SearchResponse searchResponse = client() .prepareSearch() .setQuery(matchAllQuery()) .setSize(35) .setScroll(TimeValue.timeValueMinutes(2)) .addSort("field", SortOrder.ASC) .execute() .actionGet(); long counter = 0; assertThat(searchResponse.getHits().getTotalHits(), equalTo(100l)); assertThat(searchResponse.getHits().hits().length, equalTo(35)); for (SearchHit hit : searchResponse.getHits()) { assertThat(((Number) hit.sortValues()[0]).longValue(), equalTo(counter++)); } if (randomBoolean()) { client().admin().indices().prepareClose("test").get(); client().admin().indices().prepareOpen("test").get(); ensureGreen("test"); } else { client().admin().indices().prepareDelete("test").get(); } }
@Test public void testPutMappingsWithBlocks() throws Exception { createIndex("test"); ensureGreen(); for (String block : Arrays.asList(SETTING_BLOCKS_READ, SETTING_BLOCKS_WRITE)) { try { enableIndexBlock("test", block); assertAcked( client() .admin() .indices() .preparePutMapping("test") .setType("doc") .setSource("{\"properties\":{\"date\":{\"type\":\"integer\"}}}")); } finally { disableIndexBlock("test", block); } } for (String block : Arrays.asList(SETTING_READ_ONLY, SETTING_BLOCKS_METADATA)) { try { enableIndexBlock("test", block); assertBlocked( client() .admin() .indices() .preparePutMapping("test") .setType("doc") .setSource("{\"properties\":{\"date\":{\"type\":\"integer\"}}}")); } finally { disableIndexBlock("test", block); } } }
@Test public void testPutWarmerAcknowledgement() { createIndex("test"); // make sure one shard is started so the search during put warmer will not fail index("test", "type", "1", "f", 1); assertAcked( client() .admin() .indices() .preparePutWarmer("custom_warmer") .setSearchRequest( client() .prepareSearch("test") .setTypes("test") .setQuery(QueryBuilders.matchAllQuery()))); for (Client client : clients()) { GetWarmersResponse getWarmersResponse = client.admin().indices().prepareGetWarmers().setLocal(true).get(); assertThat(getWarmersResponse.warmers().size(), equalTo(1)); ObjectObjectCursor<String, List<IndexWarmersMetaData.Entry>> entry = getWarmersResponse.warmers().iterator().next(); assertThat(entry.key, equalTo("test")); assertThat(entry.value.size(), equalTo(1)); assertThat(entry.value.get(0).name(), equalTo("custom_warmer")); } }
@Ignore("https://github.com/elasticsearch/elasticsearch/issues/9904") @Test public void testShapeFilterWithRandomGeoCollection() throws Exception { // Create a random geometry collection. GeometryCollectionBuilder gcb = RandomShapeGenerator.createGeometryCollection(getRandom()); logger.info("Created Random GeometryCollection containing " + gcb.numShapes() + " shapes"); createIndex("randshapes"); assertAcked(prepareCreate("test").addMapping("type", "location", "type=geo_shape")); XContentBuilder docSource = gcb.toXContent(jsonBuilder().startObject().field("location"), null).endObject(); indexRandom(true, client().prepareIndex("test", "type", "1").setSource(docSource)); ensureSearchable("test"); ShapeBuilder filterShape = (gcb.getShapeAt(randomIntBetween(0, gcb.numShapes() - 1))); GeoShapeFilterBuilder filter = FilterBuilders.geoShapeFilter("location", filterShape, ShapeRelation.INTERSECTS); SearchResponse result = client() .prepareSearch("test") .setQuery(QueryBuilders.matchAllQuery()) .setPostFilter(filter) .get(); assertSearchResponse(result); assertHitCount(result, 1); }
@Test public void testCloseIndexNoAcknowledgement() { createIndex("test"); ensureGreen(); CloseIndexResponse closeIndexResponse = client().admin().indices().prepareClose("test").setTimeout("0s").get(); assertThat(closeIndexResponse.isAcknowledged(), equalTo(false)); }
public void testCloseIndexAcknowledgement() { createIndex("test"); ensureGreen(); assertAcked(client().admin().indices().prepareClose("test")); for (Client client : clients()) { IndexMetaData indexMetaData = getLocalClusterState(client).metaData().indices().get("test"); assertThat(indexMetaData.getState(), equalTo(State.CLOSE)); } }
@Test public void testUpdateSettingsNoAcknowledgement() { createIndex("test"); UpdateSettingsResponse updateSettingsResponse = client() .admin() .indices() .prepareUpdateSettings("test") .setTimeout("0s") .setSettings(Settings.builder().put("refresh_interval", 9999, TimeUnit.MILLISECONDS)) .get(); assertThat(updateSettingsResponse.isAcknowledged(), equalTo(false)); }
@Test public void testIndicesAliasesNoAcknowledgement() { createIndex("test"); IndicesAliasesResponse indicesAliasesResponse = client() .admin() .indices() .prepareAliases() .addAlias("test", "alias") .setTimeout("0s") .get(); assertThat(indicesAliasesResponse.isAcknowledged(), equalTo(false)); }
@Test public void testCreateIndexAcknowledgement() { createIndex("test"); for (Client client : clients()) { assertThat( getLocalClusterState(client).metaData().indices().containsKey("test"), equalTo(true)); } // let's wait for green, otherwise there can be issues with after test checks (mock directory // wrapper etc.) // but we do want to check that the new index is on all nodes cluster state even before green ensureGreen(); }
@Test public void testAliasNameExistingIndex() throws Exception { createIndex("index"); client() .admin() .indices() .preparePutTemplate("template_1") .setTemplate("te*") .addAlias(new Alias("index")) .get(); try { createIndex("test"); fail( "index creation should have failed due to alias with existing index name in mathching index template"); } catch (InvalidAliasNameException e) { assertThat( e.getMessage(), equalTo( "[test] Invalid alias name [index], an index exists with the same name as the alias")); } }
@Test public void testPutMappingNoAcknowledgement() { createIndex("test"); ensureGreen(); PutMappingResponse putMappingResponse = client() .admin() .indices() .preparePutMapping("test") .setType("test") .setSource("field", "type=string,index=not_analyzed") .setTimeout("0s") .get(); assertThat(putMappingResponse.isAcknowledged(), equalTo(false)); }
@Test public void testClearNonExistentScrollId() throws Exception { createIndex("idx"); ClearScrollResponse response = client() .prepareClearScroll() .addScrollId( "cXVlcnlUaGVuRmV0Y2g7MzsyOlpBRC1qOUhrUjhhZ0NtQWUxU2FuWlE7MjpRcjRaNEJ2R1JZV1VEMW02ZGF1LW5ROzI6S0xUal9lZDRTd3lWNUhUU2VSb01CQTswOw==") .get(); // Whether we actually clear a scroll, we can't know, since that information isn't serialized in // the // free search context response, which is returned from each node we want to clear a particular // scroll. assertThat(response.isSucceeded(), is(true)); assertThat(response.getNumFreed(), equalTo(0)); assertThat(response.status(), equalTo(RestStatus.NOT_FOUND)); }
@Test public void testPutWarmerNoAcknowledgement() throws InterruptedException { createIndex("test"); // make sure one shard is started so the search during put warmer will not fail index("test", "type", "1", "f", 1); PutWarmerResponse putWarmerResponse = client() .admin() .indices() .preparePutWarmer("custom_warmer") .setTimeout("0s") .setSearchRequest( client() .prepareSearch("test") .setTypes("test") .setQuery(QueryBuilders.matchAllQuery())) .get(); assertThat(putWarmerResponse.isAcknowledged(), equalTo(false)); /* Since we don't wait for the ack here we have to wait until the search request has been executed from the master * otherwise the test infra might have already deleted the index and the search request fails on all shards causing * the test to fail too. We simply wait until the the warmer has been installed and also clean it up afterwards.*/ assertTrue( awaitBusy( new Predicate<Object>() { @Override public boolean apply(Object input) { for (Client client : clients()) { GetWarmersResponse getWarmersResponse = client.admin().indices().prepareGetWarmers().setLocal(true).get(); if (getWarmersResponse.warmers().size() != 1) { return false; } } return true; } })); assertAcked( client() .admin() .indices() .prepareDeleteWarmer() .setIndices("test") .setNames("custom_warmer")); }
@Test public void testPutMappingAcknowledgement() { createIndex("test"); ensureGreen(); assertAcked( client() .admin() .indices() .preparePutMapping("test") .setType("test") .setSource("field", "type=string,index=not_analyzed")); for (Client client : clients()) { assertThat( getLocalClusterState(client).metaData().indices().get("test").mapping("test"), notNullValue()); } }
@Test public void testEnforceWindowSize() { createIndex("test"); // this int iters = atLeast(10); for (int i = 0; i < iters; i++) { client() .prepareIndex("test", "type", Integer.toString(i)) .setSource("f", Integer.toString(i)) .execute() .actionGet(); } refresh(); int numShards = getNumShards("test").numPrimaries; for (int j = 0; j < iters; j++) { SearchResponse searchResponse = client() .prepareSearch() .setQuery(QueryBuilders.matchAllQuery()) .setRescorer( RescoreBuilder.queryRescorer( QueryBuilders.functionScoreQuery(QueryBuilders.matchAllQuery()) .boostMode("replace") .add(ScoreFunctionBuilders.factorFunction(100))) .setQueryWeight(0.0f) .setRescoreQueryWeight(1.0f)) .setRescoreWindow(1) .setSize(randomIntBetween(2, 10)) .execute() .actionGet(); assertFirstHit(searchResponse, hasScore(100.f)); int numPending100 = numShards; for (int i = 0; i < searchResponse.getHits().hits().length; i++) { float score = searchResponse.getHits().hits()[i].getScore(); if (score == 100f) { assertThat(numPending100--, greaterThanOrEqualTo(0)); } else { assertThat(numPending100, equalTo(0)); } } } }
@Test public void testIndicesAliasesAcknowledgement() { createIndex("test"); // testing acknowledgement when trying to submit an existing alias too // in that case it would not make any change, but we are sure about the cluster state // as the previous operation was acknowledged for (int i = 0; i < 2; i++) { assertAcked(client().admin().indices().prepareAliases().addAlias("test", "alias")); for (Client client : clients()) { AliasMetaData aliasMetaData = ((AliasOrIndex.Alias) getLocalClusterState(client).metaData().getAliasAndIndexLookup().get("alias")) .getFirstAliasMetaData(); assertThat(aliasMetaData.alias(), equalTo("alias")); } } }
@Test public void testDeleteWarmerNoAcknowledgement() throws InterruptedException { createIndex("test"); index("test", "type", "1", "f", 1); assertAcked( client() .admin() .indices() .preparePutWarmer("custom_warmer") .setSearchRequest( client() .prepareSearch("test") .setTypes("test") .setQuery(QueryBuilders.matchAllQuery()))); DeleteWarmerResponse deleteWarmerResponse = client() .admin() .indices() .prepareDeleteWarmer() .setIndices("test") .setNames("custom_warmer") .setTimeout("0s") .get(); assertFalse(deleteWarmerResponse.isAcknowledged()); assertTrue( awaitBusy( new Predicate<Object>() { // wait until they are all deleted @Override public boolean apply(Object input) { for (Client client : clients()) { GetWarmersResponse getWarmersResponse = client.admin().indices().prepareGetWarmers().setLocal(true).get(); if (getWarmersResponse.warmers().size() > 0) { return false; } } return true; } })); }
@Test public void testUpdateSettingsAcknowledgement() { createIndex("test"); assertAcked( client() .admin() .indices() .prepareUpdateSettings("test") .setSettings(Settings.builder().put("refresh_interval", 9999, TimeUnit.MILLISECONDS))); for (Client client : clients()) { String refreshInterval = getLocalClusterState(client) .metaData() .index("test") .settings() .get("index.refresh_interval"); assertThat(refreshInterval, equalTo("9999ms")); } }
@Test public void testClearIllegalScrollId() throws Exception { createIndex("idx"); try { client().prepareClearScroll().addScrollId("c2Nhbjs2OzM0NDg1ODpzRlBLc0FXNlNyNm5JWUc1").get(); fail(); } catch (IllegalArgumentException e) { } try { // Fails during base64 decoding (Base64-encoded string must have at least four characters) client().prepareClearScroll().addScrollId("a").get(); fail(); } catch (IllegalArgumentException e) { } try { client().prepareClearScroll().addScrollId("abcabc").get(); fail(); // if running without -ea this will also throw ElasticsearchIllegalArgumentException } catch (UncategorizedExecutionException e) { assertThat(e.getRootCause(), instanceOf(AssertionError.class)); } }
@Test(expected = RepositoryVerificationException.class) @Ignore public void testWrongPath() { Client client = client(); logger.info("--> creating hdfs repository with path [{}]", path); PutRepositoryResponse putRepositoryResponse = client .admin() .cluster() .preparePutRepository("test-repo") .setType("hdfs") .setSettings( Settings.settingsBuilder() .put("uri", "file://./") .put("path", path + "a@b$c#11:22") .put("chunk_size", randomIntBetween(100, 1000) + "k") .put("compress", randomBoolean())) .get(); assertThat(putRepositoryResponse.isAcknowledged(), equalTo(true)); createIndex("test-idx-1", "test-idx-2", "test-idx-3"); ensureGreen(); }
// TODO this test causes hangs, blocking on the action get when fetching the shape for some reason @Test @AwaitsFix( bugUrl = "this test causes hangs, blocking on the action get when fetching the shape for some reason") public void testIndexedShapeReference() throws Exception { String mapping = XContentFactory.jsonBuilder() .startObject() .startObject("type1") .startObject("properties") .startObject("location") .field("type", "geo_shape") .field("tree", "quadtree") .endObject() .endObject() .endObject() .endObject() .string(); prepareCreate("test").addMapping("type1", mapping).execute().actionGet(); ensureGreen(); client() .prepareIndex("test", "type1", "1") .setSource( jsonBuilder() .startObject() .field("name", "Document 1") .startObject("location") .field("type", "point") .startArray("coordinates") .value(-30) .value(-30) .endArray() .endObject() .endObject()) .execute() .actionGet(); refresh(); ShapeBuilder shape = ShapeBuilder.newEnvelope().topLeft(-45, 45).bottomRight(45, -45); XContentBuilder shapeContent = jsonBuilder().startObject().field("shape", shape); shapeContent.endObject(); createIndex("shapes"); ensureGreen(); client() .prepareIndex("shapes", "shape_type", "Big_Rectangle") .setSource(shapeContent) .execute() .actionGet(); refresh(); SearchResponse searchResponse = client() .prepareSearch("test") .setQuery( filteredQuery( matchAllQuery(), geoIntersectionFilter("location", "Big_Rectangle", "shape_type"))) .execute() .actionGet(); assertThat(searchResponse.getHits().getTotalHits(), equalTo(1l)); assertThat(searchResponse.getHits().hits().length, equalTo(1)); assertThat(searchResponse.getHits().getAt(0).id(), equalTo("1")); searchResponse = client() .prepareSearch() .setQuery(geoShapeQuery("location", "Big_Rectangle", "shape_type")) .execute() .actionGet(); assertThat(searchResponse.getHits().getTotalHits(), equalTo(1l)); assertThat(searchResponse.getHits().hits().length, equalTo(1)); assertThat(searchResponse.getHits().getAt(0).id(), equalTo("1")); }
@Test public void updateMappingConcurrently() throws Throwable { createIndex("test1", "test2"); // This is important. The test assumes all nodes are aware of all indices. Due to initializing // shard throttling // not all shards are allocated with the initial create index. Wait for it.. ensureYellow(); final Throwable[] threadException = new Throwable[1]; final AtomicBoolean stop = new AtomicBoolean(false); Thread[] threads = new Thread[3]; final CyclicBarrier barrier = new CyclicBarrier(threads.length); final ArrayList<Client> clientArray = new ArrayList<>(); for (Client c : clients()) { clientArray.add(c); } for (int j = 0; j < threads.length; j++) { threads[j] = new Thread( new Runnable() { @SuppressWarnings("unchecked") @Override public void run() { try { barrier.await(); for (int i = 0; i < 100; i++) { if (stop.get()) { return; } Client client1 = clientArray.get(i % clientArray.size()); Client client2 = clientArray.get((i + 1) % clientArray.size()); String indexName = i % 2 == 0 ? "test2" : "test1"; String typeName = "type" + (i % 10); String fieldName = Thread.currentThread().getName() + "_" + i; PutMappingResponse response = client1 .admin() .indices() .preparePutMapping(indexName) .setType(typeName) .setSource( JsonXContent.contentBuilder() .startObject() .startObject(typeName) .startObject("properties") .startObject(fieldName) .field("type", "string") .endObject() .endObject() .endObject() .endObject()) .get(); assertThat(response.isAcknowledged(), equalTo(true)); GetMappingsResponse getMappingResponse = client2.admin().indices().prepareGetMappings(indexName).get(); ImmutableOpenMap<String, MappingMetaData> mappings = getMappingResponse.getMappings().get(indexName); assertThat(mappings.containsKey(typeName), equalTo(true)); assertThat( ((Map<String, Object>) mappings.get(typeName).getSourceAsMap().get("properties")) .keySet(), Matchers.hasItem(fieldName)); } } catch (Throwable t) { threadException[0] = t; stop.set(true); } } }); threads[j].setName("t_" + j); threads[j].start(); } for (Thread t : threads) t.join(); if (threadException[0] != null) { throw threadException[0]; } }
@Test public void testSimpleWorkflow() { Client client = client(); logger.info("--> creating hdfs repository with path [{}]", path); PutRepositoryResponse putRepositoryResponse = client .admin() .cluster() .preparePutRepository("test-repo") .setType("hdfs") .setSettings( Settings.settingsBuilder() .put("uri", "file://./") .put("path", path) .put("conf", "additional-cfg.xml, conf-2.xml") .put("chunk_size", randomIntBetween(100, 1000) + "k") .put("compress", randomBoolean())) .get(); assertThat(putRepositoryResponse.isAcknowledged(), equalTo(true)); 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(); assertThat(client.prepareCount("test-idx-1").get().getCount(), equalTo(100L)); assertThat(client.prepareCount("test-idx-2").get().getCount(), equalTo(100L)); assertThat(client.prepareCount("test-idx-3").get().getCount(), equalTo(100L)); 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())); assertThat( client .admin() .cluster() .prepareGetSnapshots("test-repo") .setSnapshots("test-snap") .get() .getSnapshots() .get(0) .state(), equalTo(SnapshotState.SUCCESS)); 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(); } refresh(); assertThat(client.prepareCount("test-idx-1").get().getCount(), equalTo(50L)); assertThat(client.prepareCount("test-idx-2").get().getCount(), equalTo(50L)); assertThat(client.prepareCount("test-idx-3").get().getCount(), equalTo(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(); assertThat(client.prepareCount("test-idx-1").get().getCount(), equalTo(100L)); assertThat(client.prepareCount("test-idx-2").get().getCount(), equalTo(100L)); assertThat(client.prepareCount("test-idx-3").get().getCount(), equalTo(50L)); // Test restore after index deletion logger.info("--> delete indices"); 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(); assertThat(client.prepareCount("test-idx-1").get().getCount(), equalTo(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)); }
@Test public void testSimpleSearchRouting() { createIndex("test"); ensureGreen(); logger.info("--> indexing with id [1], and routing [0]"); client() .prepareIndex("test", "type1", "1") .setRouting("0") .setSource("field", "value1") .setRefresh(true) .execute() .actionGet(); logger.info("--> verifying get with no routing, should not find anything"); for (int i = 0; i < 5; i++) { assertThat( client().prepareGet("test", "type1", "1").execute().actionGet().isExists(), equalTo(false)); } logger.info("--> verifying get with routing, should find"); for (int i = 0; i < 5; i++) { assertThat( client() .prepareGet("test", "type1", "1") .setRouting("0") .execute() .actionGet() .isExists(), equalTo(true)); } logger.info("--> search with no routing, should fine one"); for (int i = 0; i < 5; i++) { assertThat( client() .prepareSearch() .setQuery(QueryBuilders.matchAllQuery()) .execute() .actionGet() .getHits() .totalHits(), equalTo(1l)); } logger.info("--> search with wrong routing, should not find"); for (int i = 0; i < 5; i++) { assertThat( client() .prepareSearch() .setRouting("1") .setQuery(QueryBuilders.matchAllQuery()) .execute() .actionGet() .getHits() .totalHits(), equalTo(0l)); assertThat( client() .prepareCount() .setRouting("1") .setQuery(QueryBuilders.matchAllQuery()) .execute() .actionGet() .getCount(), equalTo(0l)); } logger.info("--> search with correct routing, should find"); for (int i = 0; i < 5; i++) { assertThat( client() .prepareSearch() .setRouting("0") .setQuery(QueryBuilders.matchAllQuery()) .execute() .actionGet() .getHits() .totalHits(), equalTo(1l)); assertThat( client() .prepareCount() .setRouting("0") .setQuery(QueryBuilders.matchAllQuery()) .execute() .actionGet() .getCount(), equalTo(1l)); } logger.info("--> indexing with id [2], and routing [1]"); client() .prepareIndex("test", "type1", "2") .setRouting("1") .setSource("field", "value1") .setRefresh(true) .execute() .actionGet(); logger.info("--> search with no routing, should fine two"); for (int i = 0; i < 5; i++) { assertThat( client() .prepareSearch() .setQuery(QueryBuilders.matchAllQuery()) .execute() .actionGet() .getHits() .totalHits(), equalTo(2l)); assertThat( client() .prepareCount() .setQuery(QueryBuilders.matchAllQuery()) .execute() .actionGet() .getCount(), equalTo(2l)); } logger.info("--> search with 0 routing, should find one"); for (int i = 0; i < 5; i++) { assertThat( client() .prepareSearch() .setRouting("0") .setQuery(QueryBuilders.matchAllQuery()) .execute() .actionGet() .getHits() .totalHits(), equalTo(1l)); assertThat( client() .prepareCount() .setRouting("0") .setQuery(QueryBuilders.matchAllQuery()) .execute() .actionGet() .getCount(), equalTo(1l)); } logger.info("--> search with 1 routing, should find one"); for (int i = 0; i < 5; i++) { assertThat( client() .prepareSearch() .setRouting("1") .setQuery(QueryBuilders.matchAllQuery()) .execute() .actionGet() .getHits() .totalHits(), equalTo(1l)); assertThat( client() .prepareCount() .setRouting("1") .setQuery(QueryBuilders.matchAllQuery()) .execute() .actionGet() .getCount(), equalTo(1l)); } logger.info("--> search with 0,1 routings , should find two"); for (int i = 0; i < 5; i++) { assertThat( client() .prepareSearch() .setRouting("0", "1") .setQuery(QueryBuilders.matchAllQuery()) .execute() .actionGet() .getHits() .totalHits(), equalTo(2l)); assertThat( client() .prepareCount() .setRouting("0", "1") .setQuery(QueryBuilders.matchAllQuery()) .execute() .actionGet() .getCount(), equalTo(2l)); } logger.info("--> search with 0,1,0 routings , should find two"); for (int i = 0; i < 5; i++) { assertThat( client() .prepareSearch() .setRouting("0", "1", "0") .setQuery(QueryBuilders.matchAllQuery()) .execute() .actionGet() .getHits() .totalHits(), equalTo(2l)); assertThat( client() .prepareCount() .setRouting("0", "1", "0") .setQuery(QueryBuilders.matchAllQuery()) .execute() .actionGet() .getCount(), equalTo(2l)); } }
@Test public void testSimpleCrudRouting() throws Exception { createIndex("test"); ensureGreen(); logger.info("--> indexing with id [1], and routing [0]"); client() .prepareIndex("test", "type1", "1") .setRouting("0") .setSource("field", "value1") .setRefresh(true) .execute() .actionGet(); logger.info("--> verifying get with no routing, should not find anything"); for (int i = 0; i < 5; i++) { assertThat( client().prepareGet("test", "type1", "1").execute().actionGet().isExists(), equalTo(false)); } logger.info("--> verifying get with routing, should find"); for (int i = 0; i < 5; i++) { assertThat( client() .prepareGet("test", "type1", "1") .setRouting("0") .execute() .actionGet() .isExists(), equalTo(true)); } logger.info("--> deleting with no routing, should not delete anything"); client().prepareDelete("test", "type1", "1").setRefresh(true).execute().actionGet(); for (int i = 0; i < 5; i++) { assertThat( client().prepareGet("test", "type1", "1").execute().actionGet().isExists(), equalTo(false)); assertThat( client() .prepareGet("test", "type1", "1") .setRouting("0") .execute() .actionGet() .isExists(), equalTo(true)); } logger.info("--> deleting with routing, should delete"); client() .prepareDelete("test", "type1", "1") .setRouting("0") .setRefresh(true) .execute() .actionGet(); for (int i = 0; i < 5; i++) { assertThat( client().prepareGet("test", "type1", "1").execute().actionGet().isExists(), equalTo(false)); assertThat( client() .prepareGet("test", "type1", "1") .setRouting("0") .execute() .actionGet() .isExists(), equalTo(false)); } logger.info("--> indexing with id [1], and routing [0]"); client() .prepareIndex("test", "type1", "1") .setRouting("0") .setSource("field", "value1") .setRefresh(true) .execute() .actionGet(); logger.info("--> verifying get with no routing, should not find anything"); for (int i = 0; i < 5; i++) { assertThat( client().prepareGet("test", "type1", "1").execute().actionGet().isExists(), equalTo(false)); } logger.info("--> verifying get with routing, should find"); for (int i = 0; i < 5; i++) { assertThat( client() .prepareGet("test", "type1", "1") .setRouting("0") .execute() .actionGet() .isExists(), equalTo(true)); } logger.info("--> deleting_by_query with 1 as routing, should not delete anything"); client().prepareDeleteByQuery().setQuery(matchAllQuery()).setRouting("1").execute().actionGet(); client().admin().indices().prepareRefresh().execute().actionGet(); for (int i = 0; i < 5; i++) { assertThat( client().prepareGet("test", "type1", "1").execute().actionGet().isExists(), equalTo(false)); assertThat( client() .prepareGet("test", "type1", "1") .setRouting("0") .execute() .actionGet() .isExists(), equalTo(true)); } logger.info("--> deleting_by_query with , should delete"); client().prepareDeleteByQuery().setQuery(matchAllQuery()).setRouting("0").execute().actionGet(); client().admin().indices().prepareRefresh().execute().actionGet(); for (int i = 0; i < 5; i++) { assertThat( client().prepareGet("test", "type1", "1").execute().actionGet().isExists(), equalTo(false)); assertThat( client() .prepareGet("test", "type1", "1") .setRouting("0") .execute() .actionGet() .isExists(), equalTo(false)); } }