@Test public void testDeleteAllOneIndex() { client.admin().indices().prepareDelete().execute().actionGet(); String json = "{" + "\"user\":\"kimchy\"," + "\"postDate\":\"2013-01-30\"," + "\"message\":\"trying out Elastic Search\"" + "}"; client.prepareIndex("twitter", "tweet").setSource(json).setRefresh(true).execute().actionGet(); SearchResponse search = client.prepareSearch().setQuery(QueryBuilders.matchAllQuery()).execute().actionGet(); assertThat(search.getHits().totalHits(), equalTo(1l)); DeleteByQueryRequestBuilder deleteByQueryRequestBuilder = new DeleteByQueryRequestBuilder(client); deleteByQueryRequestBuilder.setQuery(QueryBuilders.matchAllQuery()); DeleteByQueryResponse actionGet = deleteByQueryRequestBuilder.execute().actionGet(); assertThat(actionGet.getIndex("twitter"), notNullValue()); assertThat(actionGet.getIndex("twitter").getFailedShards(), equalTo(0)); client.admin().indices().prepareRefresh().execute().actionGet(); search = client.prepareSearch().setQuery(QueryBuilders.matchAllQuery()).execute().actionGet(); assertThat(search.getHits().totalHits(), equalTo(0l)); }
@Test public void testDeleteByQueryWithNoIndices() { DeleteByQueryRequestBuilder delete = newDeleteByQuery().setQuery(QueryBuilders.matchAllQuery()); delete.setIndicesOptions(IndicesOptions.fromOptions(false, true, true, false)); assertDBQResponse(delete.get(), 0L, 0l, 0l, 0l); assertSearchContextsClosed(); }
@Test public void testDeleteByQueryWithRouting() throws Exception { assertAcked(prepareCreate("test").setSettings("number_of_shards", 2)); ensureGreen("test"); final int docs = randomIntBetween(2, 10); logger.info("--> indexing [{}] documents with routing", docs); for (int i = 0; i < docs; i++) { client() .prepareIndex("test", "test", String.valueOf(i)) .setRouting(String.valueOf(i)) .setSource("field1", 1) .get(); } refresh(); logger.info("--> counting documents with no routing, should be equal to [{}]", docs); assertHitCount(client().prepareCount().get(), docs); String routing = String.valueOf(randomIntBetween(2, docs)); logger.info("--> counting documents with routing [{}]", routing); long expected = client().prepareCount().setRouting(routing).get().getCount(); logger.info("--> delete all documents with routing [{}] with a delete-by-query", routing); DeleteByQueryRequestBuilder delete = newDeleteByQuery().setRouting(routing).setQuery(QueryBuilders.matchAllQuery()); assertDBQResponse(delete.get(), expected, expected, 0l, 0l); refresh(); assertHitCount(client().prepareCount().get(), docs - expected); assertSearchContextsClosed(); }
@Test public void testDeleteByFieldQuery() throws Exception { assertAcked(prepareCreate("test").addAlias(new Alias("alias"))); int numDocs = scaledRandomIntBetween(10, 100); for (int i = 0; i < numDocs; i++) { client() .prepareIndex("test", "test", Integer.toString(i)) .setRouting(randomAsciiOfLengthBetween(1, 5)) .setSource("foo", "bar") .get(); } refresh(); int n = between(0, numDocs - 1); assertHitCount( client() .prepareCount("test") .setQuery(QueryBuilders.matchQuery("_id", Integer.toString(n))) .get(), 1); assertHitCount( client().prepareCount("test").setQuery(QueryBuilders.matchAllQuery()).get(), numDocs); DeleteByQueryRequestBuilder delete = newDeleteByQuery() .setIndices("alias") .setQuery(QueryBuilders.matchQuery("_id", Integer.toString(n))); assertDBQResponse(delete.get(), 1L, 1L, 0l, 0l); refresh(); assertHitCount( client().prepareCount("test").setQuery(QueryBuilders.matchAllQuery()).get(), numDocs - 1); assertSearchContextsClosed(); }
@Test public void testDeleteAllNoIndices() { client.admin().indices().prepareDelete().execute().actionGet(); client.admin().indices().prepareRefresh().execute().actionGet(); DeleteByQueryRequestBuilder deleteByQueryRequestBuilder = new DeleteByQueryRequestBuilder(client); deleteByQueryRequestBuilder.setQuery(QueryBuilders.matchAllQuery()); DeleteByQueryResponse actionGet = deleteByQueryRequestBuilder.execute().actionGet(); assertThat(actionGet.getIndices().size(), equalTo(0)); }
private void clearDocuments() { if (isAnyIndexPresent()) { DeleteByQueryRequestBuilder deleteByQueryRequestBuilder = new DeleteByQueryRequestBuilder(client); deleteByQueryRequestBuilder.setQuery(QueryBuilders.matchAllQuery()); deleteByQueryRequestBuilder.execute().actionGet(); refreshNode(); } }
public void testDeleteByQueryWithDateMath() throws Exception { index("test", "type", "1", "d", "2013-01-01"); ensureGreen(); refresh(); assertHitCount(client().prepareSearch("test").setSize(0).get(), 1); DeleteByQueryRequestBuilder delete = newDeleteByQuery().setIndices("test").setQuery(QueryBuilders.rangeQuery("d").to("now-1h")); assertDBQResponse(delete.get(), 1L, 1L, 0L, 0L); refresh(); assertHitCount(client().prepareSearch("test").setSize(0).get(), 0); assertSearchContextsClosed(); }
public void testDeleteByQueryWithOneIndex() throws Exception { final long docs = randomIntBetween(1, 50); for (int i = 0; i < docs; i++) { index("test", "test", String.valueOf(i), "fields1", 1); } refresh(); assertHitCount(client().prepareSearch("test").setSize(0).get(), docs); DeleteByQueryRequestBuilder delete = newDeleteByQuery().setIndices("t*").setQuery(QueryBuilders.matchAllQuery()); assertDBQResponse(delete.get(), docs, docs, 0L, 0L); refresh(); assertHitCount(client().prepareSearch("test").setSize(0).get(), 0); assertSearchContextsClosed(); }
public void testDeleteByQueryWithTypes() throws Exception { final long docs = randomIntBetween(1, 50); for (int i = 0; i < docs; i++) { index(randomFrom("test1", "test2", "test3"), "type1", String.valueOf(i), "foo", "bar"); index(randomFrom("test1", "test2", "test3"), "type2", String.valueOf(i), "foo", "bar"); } refresh(); assertHitCount(client().prepareSearch().setSize(0).get(), docs * 2); assertHitCount(client().prepareSearch().setSize(0).setTypes("type1").get(), docs); assertHitCount(client().prepareSearch().setSize(0).setTypes("type2").get(), docs); DeleteByQueryRequestBuilder delete = newDeleteByQuery().setTypes("type1").setQuery(QueryBuilders.matchAllQuery()); assertDBQResponse(delete.get(), docs, docs, 0L, 0L); refresh(); assertHitCount(client().prepareSearch().setSize(0).get(), docs); assertHitCount(client().prepareSearch().setSize(0).setTypes("type1").get(), 0); assertHitCount(client().prepareSearch().setSize(0).setTypes("type2").get(), docs); assertSearchContextsClosed(); }
public void testDeleteByQueryWithMissingIndex() throws Exception { client() .prepareIndex("test", "test") .setSource(jsonBuilder().startObject().field("field1", 1).endObject()) .setRefresh(true) .get(); assertHitCount(client().prepareSearch().setSize(0).get(), 1); DeleteByQueryRequestBuilder delete = newDeleteByQuery().setIndices("test", "missing").setQuery(QueryBuilders.matchAllQuery()); try { delete.get(); fail("should have thrown an exception because of a missing index"); } catch (IndexNotFoundException e) { // Ok } delete.setIndicesOptions(IndicesOptions.lenientExpandOpen()); assertDBQResponse(delete.get(), 1L, 1L, 0L, 0L); refresh(); assertHitCount(client().prepareSearch("test").setSize(0).get(), 0); assertSearchContextsClosed(); }