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 authored Feb 19, 2020
1 parent 0a06ef5 commit 23ee043
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 @@ -365,7 +365,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 @@ -377,9 +377,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 @@ -36,6 +36,7 @@
import java.io.IOException;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static java.util.Collections.emptyMap;
Expand Down Expand Up @@ -320,4 +321,43 @@ 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(List.of("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 23ee043

Please sign in to comment.