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

Commit 40e299b

Browse files
committed
Reindex scripts now accept both Map and plain JSON String as inputs.
New test cases are added for reindexing with scripts.
1 parent c72533e commit 40e299b

File tree

2 files changed

+55
-3
lines changed

2 files changed

+55
-3
lines changed

jest-common/src/main/java/io/searchbox/indices/reindex/Reindex.java

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ public class Reindex extends GenericResultAbstractAction {
1414

1515
private Object source;
1616
private Object destination;
17+
private Object script;
1718
Map<String, Object> body;
1819

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

2324
source = builder.source;
2425
destination = builder.dest;
26+
script = builder.script;
2527

2628
if (builder.conflicts != null) {
2729
body.put("conflicts", builder.conflicts);
2830
}
2931
if (builder.size != null) {
3032
body.put("size", builder.size);
3133
}
32-
if (builder.script != null) {
33-
body.put("script", builder.script);
34-
}
3534
}
3635

3736
@Override
@@ -61,6 +60,23 @@ public String getData(Gson gson) {
6160
}
6261
}
6362

63+
64+
if (this.script != null) {
65+
if (this.script instanceof String) {
66+
this.body.put("script", gson.fromJson((String) this.script, Map.class));
67+
} else if (this.script instanceof Map) {
68+
Map<String, Object> script = new HashMap<String, Object>((Map) this.script);
69+
Object params = script.get("params");
70+
if (params instanceof String) {
71+
Map paramMap = gson.fromJson((String) params, Map.class);
72+
script.put("params", paramMap);
73+
body.put("script", script);
74+
} else {
75+
body.put("script", this.script);
76+
}
77+
}
78+
}
79+
6480
return gson.toJson(this.body);
6581
}
6682

jest/src/test/java/io/searchbox/indices/reindex/ReindexIntegrationTest.java

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,4 +103,40 @@ public void documentShouldNotBeTransferredWithQuery() throws IOException {
103103
assertTrue(result.getErrorMessage(), result.isSucceeded());
104104
assertFalse(indexExists(destIndex));
105105
}
106+
107+
@Test
108+
public void testWithScriptString() throws IOException {
109+
String sourceIndex = "my_source_index";
110+
String destIndex = "my_dest_index";
111+
String documentType = "my_type";
112+
String documentId = "my_id";
113+
114+
createIndex(sourceIndex);
115+
index(sourceIndex, documentType, documentId, "{\"user\": \"kimchy\"}");
116+
flushAndRefresh(sourceIndex);
117+
118+
String scriptString = "{\n" +
119+
" \"lang\": \"painless\",\n" +
120+
" \"source\": \"ctx._source.last = params.last;\\nctx._source.nick = params.nick\",\n" +
121+
" \"params\": {\n" +
122+
" \"last\": \"gaudreau\",\n" +
123+
" \"nick\": \"hockey\"\n" +
124+
" }\n" +
125+
" }";
126+
127+
128+
Map<String, Object> script = new HashMap<>();
129+
script.put("source", scriptString);
130+
script.put("lang", "painless");
131+
132+
ImmutableMap<String, Object> source = ImmutableMap.of("index", sourceIndex);
133+
ImmutableMap<String, Object> dest = ImmutableMap.of("index", destIndex);
134+
135+
Reindex reindex = new Reindex.Builder(source, dest).script(scriptString).refresh(true).build();
136+
JestResult result = client.execute(reindex);
137+
138+
assertTrue(result.getErrorMessage(), result.isSucceeded());
139+
assertTrue(indexExists(destIndex));
140+
assertTrue(get(destIndex, documentType, documentId).isExists());
141+
}
106142
}

0 commit comments

Comments
 (0)