Add support for ignore_indices to delete by query

Closes #2734
This commit is contained in:
Benjamin Devèze 2013-03-06 00:01:51 +01:00 committed by Simon Willnauer
parent 12a2808168
commit 35f5ca915d
5 changed files with 61 additions and 1 deletions

View File

@ -22,6 +22,7 @@ package org.elasticsearch.action.support.replication;
import org.elasticsearch.action.ActionRequest; import org.elasticsearch.action.ActionRequest;
import org.elasticsearch.action.ActionRequestValidationException; import org.elasticsearch.action.ActionRequestValidationException;
import org.elasticsearch.action.WriteConsistencyLevel; import org.elasticsearch.action.WriteConsistencyLevel;
import org.elasticsearch.action.support.IgnoreIndices;
import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamInput;
import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.io.stream.StreamOutput;
import org.elasticsearch.common.unit.TimeValue; import org.elasticsearch.common.unit.TimeValue;
@ -35,6 +36,8 @@ public class IndicesReplicationOperationRequest<T extends IndicesReplicationOper
protected TimeValue timeout = ShardReplicationOperationRequest.DEFAULT_TIMEOUT; protected TimeValue timeout = ShardReplicationOperationRequest.DEFAULT_TIMEOUT;
protected String[] indices; protected String[] indices;
private IgnoreIndices ignoreIndices = IgnoreIndices.DEFAULT;
protected ReplicationType replicationType = ReplicationType.DEFAULT; protected ReplicationType replicationType = ReplicationType.DEFAULT;
protected WriteConsistencyLevel consistencyLevel = WriteConsistencyLevel.DEFAULT; protected WriteConsistencyLevel consistencyLevel = WriteConsistencyLevel.DEFAULT;
@ -64,6 +67,15 @@ public class IndicesReplicationOperationRequest<T extends IndicesReplicationOper
return this.indices; return this.indices;
} }
public IgnoreIndices ignoreIndices() {
return ignoreIndices;
}
public T ignoreIndices(IgnoreIndices ignoreIndices) {
this.ignoreIndices = ignoreIndices;
return (T) this;
}
/** /**
* The indices the request will execute against. * The indices the request will execute against.
*/ */
@ -118,6 +130,7 @@ public class IndicesReplicationOperationRequest<T extends IndicesReplicationOper
consistencyLevel = WriteConsistencyLevel.fromId(in.readByte()); consistencyLevel = WriteConsistencyLevel.fromId(in.readByte());
timeout = TimeValue.readTimeValue(in); timeout = TimeValue.readTimeValue(in);
indices = in.readStringArray(); indices = in.readStringArray();
ignoreIndices = IgnoreIndices.fromId(in.readByte());
} }
@Override @Override
@ -127,5 +140,6 @@ public class IndicesReplicationOperationRequest<T extends IndicesReplicationOper
out.writeByte(consistencyLevel.id()); out.writeByte(consistencyLevel.id());
timeout.writeTo(out); timeout.writeTo(out);
out.writeStringArrayNullable(indices); out.writeStringArrayNullable(indices);
out.writeByte(ignoreIndices.id());
} }
} }

View File

@ -22,6 +22,7 @@ package org.elasticsearch.action.support.replication;
import org.elasticsearch.action.ActionRequestBuilder; import org.elasticsearch.action.ActionRequestBuilder;
import org.elasticsearch.action.ActionResponse; import org.elasticsearch.action.ActionResponse;
import org.elasticsearch.action.WriteConsistencyLevel; import org.elasticsearch.action.WriteConsistencyLevel;
import org.elasticsearch.action.support.IgnoreIndices;
import org.elasticsearch.client.internal.InternalGenericClient; import org.elasticsearch.client.internal.InternalGenericClient;
import org.elasticsearch.common.unit.TimeValue; import org.elasticsearch.common.unit.TimeValue;
@ -58,6 +59,14 @@ public abstract class IndicesReplicationOperationRequestBuilder<Request extends
return (RequestBuilder) this; return (RequestBuilder) this;
} }
/**
* Specifies what type of requested indices to ignore. For example indices that don't exist.
*/
public RequestBuilder setIgnoreIndices(IgnoreIndices ignoreIndices) {
request().ignoreIndices(ignoreIndices);
return (RequestBuilder) this;
}
/** /**
* Sets the replication type. * Sets the replication type.
*/ */

View File

@ -75,7 +75,7 @@ public abstract class TransportIndicesReplicationOperationAction<Request extends
throw blockException; throw blockException;
} }
// get actual indices // get actual indices
String[] concreteIndices = clusterState.metaData().concreteIndices(request.indices()); String[] concreteIndices = clusterState.metaData().concreteIndices(request.indices(), request.ignoreIndices(), false);
blockException = checkRequestBlock(clusterState, request, concreteIndices); blockException = checkRequestBlock(clusterState, request, concreteIndices);
if (blockException != null) { if (blockException != null) {
throw blockException; throw blockException;

View File

@ -25,6 +25,7 @@ import org.elasticsearch.action.deletebyquery.DeleteByQueryRequest;
import org.elasticsearch.action.deletebyquery.DeleteByQueryResponse; import org.elasticsearch.action.deletebyquery.DeleteByQueryResponse;
import org.elasticsearch.action.deletebyquery.IndexDeleteByQueryResponse; import org.elasticsearch.action.deletebyquery.IndexDeleteByQueryResponse;
import org.elasticsearch.action.deletebyquery.ShardDeleteByQueryRequest; import org.elasticsearch.action.deletebyquery.ShardDeleteByQueryRequest;
import org.elasticsearch.action.support.IgnoreIndices;
import org.elasticsearch.action.support.replication.ReplicationType; import org.elasticsearch.action.support.replication.ReplicationType;
import org.elasticsearch.client.Client; import org.elasticsearch.client.Client;
import org.elasticsearch.common.bytes.BytesReference; import org.elasticsearch.common.bytes.BytesReference;
@ -83,6 +84,9 @@ public class RestDeleteByQueryAction extends BaseRestHandler {
if (consistencyLevel != null) { if (consistencyLevel != null) {
deleteByQueryRequest.consistencyLevel(WriteConsistencyLevel.fromString(consistencyLevel)); deleteByQueryRequest.consistencyLevel(WriteConsistencyLevel.fromString(consistencyLevel));
} }
if (request.hasParam("ignore_indices")) {
deleteByQueryRequest.ignoreIndices(IgnoreIndices.fromString(request.param("ignore_indices")));
}
} catch (Exception e) { } catch (Exception e) {
try { try {
XContentBuilder builder = RestXContentBuilder.restContentBuilder(request); XContentBuilder builder = RestXContentBuilder.restContentBuilder(request);

View File

@ -22,8 +22,10 @@ package org.elasticsearch.test.integration.deleteByQuery;
import org.elasticsearch.action.deletebyquery.DeleteByQueryRequestBuilder; import org.elasticsearch.action.deletebyquery.DeleteByQueryRequestBuilder;
import org.elasticsearch.action.deletebyquery.DeleteByQueryResponse; import org.elasticsearch.action.deletebyquery.DeleteByQueryResponse;
import org.elasticsearch.action.search.SearchResponse; import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.action.support.IgnoreIndices;
import org.elasticsearch.client.Client; import org.elasticsearch.client.Client;
import org.elasticsearch.index.query.QueryBuilders; import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.indices.IndexMissingException;
import org.elasticsearch.test.integration.AbstractNodesTests; import org.elasticsearch.test.integration.AbstractNodesTests;
import org.testng.annotations.AfterClass; import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass; import org.testng.annotations.BeforeClass;
@ -32,6 +34,7 @@ import org.testng.annotations.Test;
import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.notNullValue; import static org.hamcrest.Matchers.notNullValue;
import static org.testng.Assert.fail;
public class DeleteByQueryTests extends AbstractNodesTests { public class DeleteByQueryTests extends AbstractNodesTests {
@ -85,4 +88,34 @@ public class DeleteByQueryTests extends AbstractNodesTests {
search = client.prepareSearch().setQuery(QueryBuilders.matchAllQuery()).execute().actionGet(); search = client.prepareSearch().setQuery(QueryBuilders.matchAllQuery()).execute().actionGet();
assertThat(search.getHits().totalHits(), equalTo(0l)); assertThat(search.getHits().totalHits(), equalTo(0l));
} }
@Test
public void testMissing() {
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.setIndices("twitter", "missing");
deleteByQueryRequestBuilder.setQuery(QueryBuilders.matchAllQuery());
try {
DeleteByQueryResponse actionGet = deleteByQueryRequestBuilder.execute().actionGet();
fail("Exception should have been thrown.");
} catch (IndexMissingException e) {
}
deleteByQueryRequestBuilder.setIgnoreIndices(IgnoreIndices.MISSING);
DeleteByQueryResponse actionGet = deleteByQueryRequestBuilder.execute().actionGet();
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));
}
} }