Reindex: allow comma separated source indices (#52044)

Added ability to specify comma separated list of source indices without
array. Also fixed so that empty string results in validation error
rather than index does not exist.

Closes #51949
This commit is contained in:
Henning Andersen 2020-02-19 08:52:08 +01:00 committed by Henning Andersen
parent baf184c93f
commit d4bc3b75dc
2 changed files with 43 additions and 3 deletions

View File

@ -406,7 +406,7 @@ public class ReindexRequest extends AbstractBulkIndexByScrollRequest<ReindexRequ
/**
* Yank a string array from a map. Emulates XContent's permissive String to
* String array conversions.
* String array conversions and allow comma separated String.
*/
private static String[] extractStringArray(Map<String, Object> source, String name) {
Object value = source.remove(name);
@ -418,9 +418,9 @@ public class ReindexRequest extends AbstractBulkIndexByScrollRequest<ReindexRequ
List<String> list = (List<String>) value;
return list.toArray(new String[list.size()]);
} else if (value instanceof String) {
return new String[] {(String) value};
return Strings.splitStringByCommaToArray((String) value);
} else {
throw new IllegalArgumentException("Expected [" + name + "] to be a list of a string but was [" + value + ']');
throw new IllegalArgumentException("Expected [" + name + "] to be a list or a string but was [" + value + ']');
}
}

View File

@ -324,4 +324,44 @@ public class ReindexRequestTests extends AbstractBulkByScrollRequestTestCase<Rei
return ReindexRequest.buildRemoteInfo(source);
}
public void testCommaSeparatedSourceIndices() throws IOException {
ReindexRequest r = parseRequestWithSourceIndices("a,b");
assertArrayEquals(new String[]{"a", "b"}, r.getSearchRequest().indices());
}
public void testArraySourceIndices() throws IOException {
ReindexRequest r = parseRequestWithSourceIndices(new String[]{"a", "b"});
assertArrayEquals(new String[]{"a", "b"}, r.getSearchRequest().indices());
}
public void testEmptyStringSourceIndices() throws IOException {
ReindexRequest r = parseRequestWithSourceIndices("");
assertArrayEquals(new String[0], r.getSearchRequest().indices());
ActionRequestValidationException validationException = r.validate();
assertNotNull(validationException);
assertEquals(Collections.singletonList("use _all if you really want to copy from all existing indexes"),
validationException.validationErrors());
}
private ReindexRequest parseRequestWithSourceIndices(Object sourceIndices) throws IOException {
BytesReference request;
try (XContentBuilder b = JsonXContent.contentBuilder()) {
b.startObject(); {
b.startObject("source"); {
b.field("index", sourceIndices);
}
b.endObject();
b.startObject("dest"); {
b.field("index", "dest");
}
b.endObject();
}
b.endObject();
request = BytesReference.bytes(b);
}
try (XContentParser p = createParser(JsonXContent.jsonXContent, request)) {
return ReindexRequest.fromXContent(p);
}
}
}