UpdateByQueryRequest to implement IndicesRequest.Replaceable rather than CompositeIndicesRequest

Update by query is a shortcut to search + index. UpdateByQueryRequest gets serialized on the transport layer only when the transport client is used. Given that the request supports wildcards and allows to set its indices, it should implement IndicesRequest.Repleaceable. implementing CompositeIndicesRequest makes little sense as the indices that the request works against depend entirely on the inner search request.
This commit is contained in:
javanna 2016-08-11 17:55:09 +02:00 committed by Luca Cavanna
parent 11d770dde3
commit 4424d2263f
2 changed files with 45 additions and 40 deletions

View File

@ -19,15 +19,9 @@
package org.elasticsearch.index.reindex;
import org.elasticsearch.action.CompositeIndicesRequest;
import org.elasticsearch.action.IndicesRequest;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.search.SearchRequest;
import java.util.ArrayList;
import java.util.List;
import static java.util.Collections.unmodifiableList;
import org.elasticsearch.action.support.IndicesOptions;
/**
* Request to update some documents. That means you can't change their type, id, index, or anything like that. This implements
@ -35,7 +29,7 @@ import static java.util.Collections.unmodifiableList;
* representative set of subrequests. This is best-effort but better than {@linkplain ReindexRequest} because scripts can't change the
* destination index and things.
*/
public class UpdateByQueryRequest extends AbstractBulkIndexByScrollRequest<UpdateByQueryRequest> implements CompositeIndicesRequest {
public class UpdateByQueryRequest extends AbstractBulkIndexByScrollRequest<UpdateByQueryRequest> implements IndicesRequest.Replaceable {
/**
* Ingest pipeline to set on index requests made by this action.
*/
@ -75,25 +69,24 @@ public class UpdateByQueryRequest extends AbstractBulkIndexByScrollRequest<Updat
return b.toString();
}
// CompositeIndicesRequest implementation so plugins can reason about the request. This is really just a best effort thing.
/**
* Accessor to get the underlying {@link IndicesRequest}s that this request wraps. Note that this method is <strong>not
* accurate</strong> since it returns dummy {@link IndexRequest}s and not the actual requests that will be issued as part of the
* execution of this request.
*
* @return a list comprising of the {@link SearchRequest} and dummy {@link IndexRequest}s
*/
//update by query updates all documents that match a query. The indices and indices options that affect how
//indices are resolved depend entirely on the inner search request. That's why the following methods delegate to it.
@Override
public List<? extends IndicesRequest> subRequests() {
public IndicesRequest indices(String... indices) {
assert getSearchRequest() != null;
List<IndicesRequest> subRequests = new ArrayList<>();
// One dummy IndexRequest per destination index.
for (String index : getSearchRequest().indices()) {
IndexRequest request = new IndexRequest();
request.index(index);
subRequests.add(request);
}
subRequests.add(getSearchRequest());
return unmodifiableList(subRequests);
};
getSearchRequest().indices(indices);
return this;
}
@Override
public String[] indices() {
assert getSearchRequest() != null;
return getSearchRequest().indices();
}
@Override
public IndicesOptions indicesOptions() {
assert getSearchRequest() != null;
return getSearchRequest().indicesOptions();
}
}

View File

@ -19,31 +19,43 @@
package org.elasticsearch.index.reindex;
import org.elasticsearch.action.IndicesRequest;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.support.IndicesOptions;
import org.elasticsearch.test.ESTestCase;
import java.util.List;
import static org.apache.lucene.util.TestUtil.randomSimpleString;
import static org.hamcrest.Matchers.arrayWithSize;
import static org.hamcrest.Matchers.hasSize;
import static org.hamcrest.Matchers.sameInstance;
public class UpdateByQueryRequestTests extends ESTestCase {
public void testUpdateByQueryRequestImplementsCompositeIndicesRequestWithDummies() {
public void testUpdateByQueryRequestImplementsIndicesRequestReplaceable() {
int numIndices = between(1, 100);
String[] indices = new String[numIndices];
for (int i = 0; i < numIndices; i++) {
indices[i] = randomSimpleString(random(), 1, 30);
}
UpdateByQueryRequest request = new UpdateByQueryRequest(new SearchRequest(indices));
List<? extends IndicesRequest> subRequests = request.subRequests();
assertThat(subRequests, hasSize(numIndices + 1));
SearchRequest searchRequest = new SearchRequest(indices);
IndicesOptions indicesOptions = IndicesOptions.fromOptions(randomBoolean(), randomBoolean(), randomBoolean(), randomBoolean());
searchRequest.indicesOptions(indicesOptions);
UpdateByQueryRequest request = new UpdateByQueryRequest(searchRequest);
for (int i = 0; i < numIndices; i++) {
assertThat(subRequests.get(i).indices(), arrayWithSize(1));
assertEquals(indices[i], subRequests.get(i).indices()[0]);
assertEquals(indices[i], request.indices()[i]);
}
assertSame(indicesOptions, request.indicesOptions());
assertSame(request.indicesOptions(), request.getSearchRequest().indicesOptions());
int numNewIndices = between(1, 100);
String[] newIndices = new String[numNewIndices];
for (int i = 0; i < numNewIndices; i++) {
newIndices[i] = randomSimpleString(random(), 1, 30);
}
request.indices(newIndices);
for (int i = 0; i < numNewIndices; i++) {;
assertEquals(newIndices[i], request.indices()[i]);
}
for (int i = 0; i < numNewIndices; i++) {;
assertEquals(newIndices[i], request.getSearchRequest().indices()[i]);
}
assertThat(subRequests.get(numIndices), sameInstance(request.getSearchRequest()));
}
}