Skip to content

Commit cffeac4

Browse files
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
1 parent 8e8419c commit cffeac4

File tree

2 files changed

+43
-3
lines changed

2 files changed

+43
-3
lines changed

server/src/main/java/org/elasticsearch/index/reindex/ReindexRequest.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -406,7 +406,7 @@ public static ReindexRequest fromXContent(XContentParser parser) throws IOExcept
406406

407407
/**
408408
* Yank a string array from a map. Emulates XContent's permissive String to
409-
* String array conversions.
409+
* String array conversions and allow comma separated String.
410410
*/
411411
private static String[] extractStringArray(Map<String, Object> source, String name) {
412412
Object value = source.remove(name);
@@ -418,9 +418,9 @@ private static String[] extractStringArray(Map<String, Object> source, String na
418418
List<String> list = (List<String>) value;
419419
return list.toArray(new String[list.size()]);
420420
} else if (value instanceof String) {
421-
return new String[] {(String) value};
421+
return Strings.splitStringByCommaToArray((String) value);
422422
} else {
423-
throw new IllegalArgumentException("Expected [" + name + "] to be a list of a string but was [" + value + ']');
423+
throw new IllegalArgumentException("Expected [" + name + "] to be a list or a string but was [" + value + ']');
424424
}
425425
}
426426

server/src/test/java/org/elasticsearch/index/reindex/ReindexRequestTests.java

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -324,4 +324,44 @@ private RemoteInfo buildRemoteInfoHostTestCase(String hostInRest) throws IOExcep
324324

325325
return ReindexRequest.buildRemoteInfo(source);
326326
}
327+
328+
public void testCommaSeparatedSourceIndices() throws IOException {
329+
ReindexRequest r = parseRequestWithSourceIndices("a,b");
330+
assertArrayEquals(new String[]{"a", "b"}, r.getSearchRequest().indices());
331+
}
332+
333+
public void testArraySourceIndices() throws IOException {
334+
ReindexRequest r = parseRequestWithSourceIndices(new String[]{"a", "b"});
335+
assertArrayEquals(new String[]{"a", "b"}, r.getSearchRequest().indices());
336+
}
337+
338+
public void testEmptyStringSourceIndices() throws IOException {
339+
ReindexRequest r = parseRequestWithSourceIndices("");
340+
assertArrayEquals(new String[0], r.getSearchRequest().indices());
341+
ActionRequestValidationException validationException = r.validate();
342+
assertNotNull(validationException);
343+
assertEquals(Collections.singletonList("use _all if you really want to copy from all existing indexes"),
344+
validationException.validationErrors());
345+
}
346+
347+
private ReindexRequest parseRequestWithSourceIndices(Object sourceIndices) throws IOException {
348+
BytesReference request;
349+
try (XContentBuilder b = JsonXContent.contentBuilder()) {
350+
b.startObject(); {
351+
b.startObject("source"); {
352+
b.field("index", sourceIndices);
353+
}
354+
b.endObject();
355+
b.startObject("dest"); {
356+
b.field("index", "dest");
357+
}
358+
b.endObject();
359+
}
360+
b.endObject();
361+
request = BytesReference.bytes(b);
362+
}
363+
try (XContentParser p = createParser(JsonXContent.jsonXContent, request)) {
364+
return ReindexRequest.fromXContent(p);
365+
}
366+
}
327367
}

0 commit comments

Comments
 (0)