@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 testDeleteByQueryWithMultipleIndices() throws Exception {
    final int indices = randomIntBetween(2, 5);
    final int docs = randomIntBetween(2, 10) * 2;
    long[] candidates = new long[indices];

    for (int i = 0; i < indices; i++) {
      // number of documents to be deleted with the upcoming delete-by-query
      // (this number differs for each index)
      candidates[i] = randomIntBetween(1, docs);

      for (int j = 0; j < docs; j++) {
        boolean candidate = (j < candidates[i]);
        index("test-" + i, "test", String.valueOf(j), "candidate", candidate);
      }
    }

    // total number of expected deletions
    long deletions = 0;
    for (long i : candidates) {
      deletions = deletions + i;
    }
    refresh();

    assertHitCount(client().prepareCount().get(), docs * indices);
    for (int i = 0; i < indices; i++) {
      assertHitCount(client().prepareCount("test-" + i).get(), docs);
    }

    // Deletes all the documents with candidate=true
    DeleteByQueryResponse response =
        newDeleteByQuery()
            .setIndices("test-*")
            .setQuery(QueryBuilders.termQuery("candidate", true))
            .get();
    refresh();

    // Checks that the DBQ response returns the expected number of deletions
    assertDBQResponse(response, deletions, deletions, 0l, 0l);
    assertNotNull(response.getIndices());
    assertThat(response.getIndices().length, equalTo(indices));

    for (int i = 0; i < indices; i++) {
      String indexName = "test-" + i;
      IndexDeleteByQueryResponse indexResponse = response.getIndex(indexName);
      assertThat(indexResponse.getFound(), equalTo(candidates[i]));
      assertThat(indexResponse.getDeleted(), equalTo(candidates[i]));
      assertThat(indexResponse.getFailed(), equalTo(0L));
      assertThat(indexResponse.getMissing(), equalTo(0L));
      assertThat(indexResponse.getIndex(), equalTo(indexName));
      long remaining = docs - candidates[i];
      assertHitCount(client().prepareCount(indexName).get(), remaining);
    }

    assertHitCount(client().prepareCount().get(), (indices * docs) - deletions);
    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 assertDBQResponse(
     DeleteByQueryResponse response, long found, long deleted, long failed, long missing) {
   assertNotNull(response);
   assertThat(response.isTimedOut(), equalTo(false));
   assertThat(response.getShardFailures().length, equalTo(0));
   assertThat(response.getTotalFound(), equalTo(found));
   assertThat(response.getTotalDeleted(), equalTo(deleted));
   assertThat(response.getTotalFailed(), equalTo(failed));
   assertThat(response.getTotalMissing(), equalTo(missing));
 }