Skip to content

Commit

Permalink
Reindex: allow comma separated source indices (#52044)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
henningandersen committed Feb 19, 2020
1 parent baf184c commit d4bc3b7
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -406,7 +406,7 @@ public static ReindexRequest fromXContent(XContentParser parser) throws IOExcept

/**
* 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);
Expand All @@ -418,9 +418,9 @@ private static String[] extractStringArray(Map<String, Object> source, String na
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 + ']');
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -324,4 +324,44 @@ private RemoteInfo buildRemoteInfoHostTestCase(String hostInRest) throws IOExcep

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);
}
}
}

0 comments on commit d4bc3b7

Please sign in to comment.