/*
 * Licensed to ElasticSearch and Shay Banon under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership. ElasticSearch licenses this
 * file to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

package org.elasticsearch.deleteByQuery;

import org.elasticsearch.action.deletebyquery.DeleteByQueryRequestBuilder;
import org.elasticsearch.action.deletebyquery.DeleteByQueryResponse;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.action.support.IgnoreIndices;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.indices.IndexMissingException;
import org.elasticsearch.rest.RestStatus;
import org.elasticsearch.test.AbstractIntegrationTest;
import org.junit.Assert;
import org.junit.Test;

import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.notNullValue;

public class DeleteByQueryTests extends AbstractIntegrationTest {

    @Test
    public void testDeleteAllNoIndices() {
                client().admin().indices().prepareRefresh().execute().actionGet();
        DeleteByQueryRequestBuilder deleteByQueryRequestBuilder = client().prepareDeleteByQuery();
        deleteByQueryRequestBuilder.setQuery(QueryBuilders.matchAllQuery());
        DeleteByQueryResponse actionGet = deleteByQueryRequestBuilder.execute().actionGet();
        assertThat(actionGet.getIndices().size(), equalTo(0));
    }
    
    @Test
    public void testDeleteAllOneIndex() {

        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 = client().prepareDeleteByQuery();
        deleteByQueryRequestBuilder.setQuery(QueryBuilders.matchAllQuery());
        
        DeleteByQueryResponse actionGet = deleteByQueryRequestBuilder.execute().actionGet();
        assertThat(actionGet.status(), equalTo(RestStatus.OK));
        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 testMissing() {

        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 = client().prepareDeleteByQuery();
        deleteByQueryRequestBuilder.setIndices("twitter", "missing");
        deleteByQueryRequestBuilder.setQuery(QueryBuilders.matchAllQuery());

        try {
            DeleteByQueryResponse actionGet = deleteByQueryRequestBuilder.execute().actionGet();
            Assert.fail("Exception should have been thrown.");
        } catch (IndexMissingException e) {
        }

        deleteByQueryRequestBuilder.setIgnoreIndices(IgnoreIndices.MISSING);
        DeleteByQueryResponse actionGet = deleteByQueryRequestBuilder.execute().actionGet();
        assertThat(actionGet.status(), equalTo(RestStatus.OK));
        assertThat(actionGet.getIndex("twitter").getFailedShards(), equalTo(0));
        assertThat(actionGet.getIndex("twitter"), notNullValue());

        client().admin().indices().prepareRefresh().execute().actionGet();
        search = client().prepareSearch().setQuery(QueryBuilders.matchAllQuery()).execute().actionGet();
        assertThat(search.getHits().totalHits(), equalTo(0l));
    }

    @Test
    public void testFailure() throws Exception {
                client().admin().indices().prepareCreate("twitter").execute().actionGet();

        DeleteByQueryResponse response = client().prepareDeleteByQuery("twitter")
                .setQuery(QueryBuilders.hasChildQuery("type", QueryBuilders.matchAllQuery()))
                .execute().actionGet();

        assertThat(response.status(), equalTo(RestStatus.BAD_REQUEST));
        assertThat(response.getIndex("twitter").getSuccessfulShards(), equalTo(0));
        assertThat(response.getIndex("twitter").getFailedShards(), equalTo(5));
    }

}

@@ -1350,16 +1350,102 @@
 .Test;%0A%0A
+import static org.elasticsearch.test.hamcrest.ElasticSearchAssertions.assertHitCount;%0A
 import s
@@ -1652,24 +1652,16 @@
 ces() %7B%0A
-        
         
@@ -4947,24 +4947,16 @@
 ption %7B%0A
-        
         
@@ -5022,32 +5022,32 @@
 ().actionGet();%0A
+
 %0A        DeleteB
@@ -5396,32 +5396,32 @@
 ), equalTo(0));%0A
-
         assertTh
@@ -5489,12 +5489,1025 @@
 );%0A    %7D
+%0A    %0A    @Test%0A    public void testDeleteByFieldQuery() throws Exception %7B%0A        client().admin().indices().prepareCreate(%22test%22).execute().actionGet();%0A        int numDocs = atLeast(10);%0A        for (int i = 0; i %3C numDocs; i++) %7B%0A            client().prepareIndex(%22test%22, %22test%22, Integer.toString(i))%0A            .setRouting(randomAsciiOfLengthBetween(1,5))%0A            .setSource(%22foo%22, %22bar%22).get();%0A        %7D%0A        refresh();%0A        assertHitCount(client().prepareCount(%22test%22).setQuery(QueryBuilders.fieldQuery(%22_id%22,Integer.toString(between(0,numDocs-1)))).get(), 1);%0A        assertHitCount(client().prepareCount(%22test%22).setQuery(QueryBuilders.matchAllQuery()).get(), numDocs);%0A        client().prepareDeleteByQuery(%22test%22)%0A                .setQuery(QueryBuilders.fieldQuery(%22_id%22, Integer.toString(between(0,numDocs-1))))%0A                .execute().actionGet();%0A        refresh();%0A        assertHitCount(client().prepareCount(%22test%22).setQuery(QueryBuilders.matchAllQuery()).get(), numDocs-1);%0A%0A    %7D
 %0A%0A%7D%0A

Add simple test for delete by field query