Skip to content
This repository has been archived by the owner on Jun 20, 2023. It is now read-only.

Commit

Permalink
Reindex scripts now accept both Map and plain JSON String as inputs.
Browse files Browse the repository at this point in the history
New test cases are added for reindexing with scripts.
  • Loading branch information
musabgelisgen committed Aug 6, 2018
1 parent 16ef8dd commit b391425
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ public class Reindex extends GenericResultAbstractAction {

private Object source;
private Object destination;
private Object script;
Map<String, Object> body;

Reindex(Builder builder) {
Expand All @@ -22,16 +23,14 @@ public class Reindex extends GenericResultAbstractAction {

source = builder.source;
destination = builder.dest;
script = builder.script;

if (builder.conflicts != null) {
body.put("conflicts", builder.conflicts);
}
if (builder.size != null) {
body.put("size", builder.size);
}
if (builder.script != null) {
body.put("script", builder.script);
}

setURI(buildURI());
}
Expand Down Expand Up @@ -63,6 +62,22 @@ public String getData(Gson gson) {
}
}

if (this.script != null) {
if (this.script instanceof String) {
this.body.put("script", gson.fromJson((String) this.script, Map.class));
} else if (this.script instanceof Map) {
Map<String, Object> script = new HashMap<String, Object>((Map) this.script);
Object params = script.get("params");
if (params instanceof String) {
Map paramMap = gson.fromJson((String) params, Map.class);
script.put("params", paramMap);
body.put("script", script);
} else {
body.put("script", this.script);
}
}
}

return gson.toJson(this.body);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,4 +103,34 @@ public void documentShouldNotBeTransferredWithQuery() throws IOException {
assertTrue(result.getErrorMessage(), result.isSucceeded());
assertFalse(indexExists(destIndex));
}

@Test
public void testWithScriptString() throws IOException {
String sourceIndex = "my_source_index";
String destIndex = "my_dest_index";
String documentType = "my_type";
String documentId = "my_id";

createIndex(sourceIndex);
index(sourceIndex, documentType, documentId, "{\"user\": \"kimchy\"}");
flushAndRefresh(sourceIndex);

String scriptString = "{\n" +
" \"lang\": \"painless\",\n" +
" \"inline\": \"int aVariable = 12; return aVariable;\",\n" +
" \"params\": {\n" +
" \"name\": \"musab\",\n" +
" \"nick\": \"msb\"\n" +
" }\n" +
" }";

ImmutableMap<String, Object> source = ImmutableMap.of("index", sourceIndex);
ImmutableMap<String, Object> dest = ImmutableMap.of("index", destIndex);
Reindex reindex = new Reindex.Builder(source, dest).script(scriptString).refresh(true).build();

JestResult result = client.execute(reindex);
assertTrue(result.getErrorMessage(), result.isSucceeded());
assertTrue(indexExists(destIndex));
assertTrue(get(destIndex, documentType, documentId).isExists());
}
}

0 comments on commit b391425

Please sign in to comment.