Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reindex: allow comma separated source indices #52044

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
}
}
}