28
28
import org .elasticsearch .script .ExecutableScript ;
29
29
import org .elasticsearch .script .Script ;
30
30
import org .elasticsearch .script .ScriptContext ;
31
+ import org .elasticsearch .script .ScriptException ;
31
32
import org .elasticsearch .script .ScriptService ;
33
+ import org .elasticsearch .script .ScriptType ;
32
34
33
35
import static java .util .Collections .emptyMap ;
34
36
import static org .elasticsearch .common .Strings .hasLength ;
35
37
import static org .elasticsearch .ingest .ConfigurationUtils .newConfigurationException ;
36
38
import static org .elasticsearch .ingest .ConfigurationUtils .readOptionalMap ;
37
39
import static org .elasticsearch .ingest .ConfigurationUtils .readOptionalStringProperty ;
38
- import static org .elasticsearch .ingest .ConfigurationUtils .readStringProperty ;
39
40
import static org .elasticsearch .script .ScriptType .FILE ;
40
41
import static org .elasticsearch .script .ScriptType .INLINE ;
41
42
import static org .elasticsearch .script .ScriptType .STORED ;
42
43
43
44
/**
44
- * Processor that adds new fields with their corresponding values. If the field is already present, its value
45
- * will be replaced with the provided one.
45
+ * Processor that evaluates a script with an ingest document in its context.
46
46
*/
47
47
public final class ScriptProcessor extends AbstractProcessor {
48
48
@@ -51,12 +51,24 @@ public final class ScriptProcessor extends AbstractProcessor {
51
51
private final Script script ;
52
52
private final ScriptService scriptService ;
53
53
54
+ /**
55
+ * Processor that evaluates a script with an ingest document in its context
56
+ *
57
+ * @param tag The processor's tag.
58
+ * @param script The {@link Script} to execute.
59
+ * @param scriptService The {@link ScriptService} used to execute the script.
60
+ */
54
61
ScriptProcessor (String tag , Script script , ScriptService scriptService ) {
55
62
super (tag );
56
63
this .script = script ;
57
64
this .scriptService = scriptService ;
58
65
}
59
66
67
+ /**
68
+ * Executes the script with the Ingest document in context.
69
+ *
70
+ * @param document The Ingest document passed into the script context under the "ctx" object.
71
+ */
60
72
@ Override
61
73
public void execute (IngestDocument document ) {
62
74
ExecutableScript executableScript = scriptService .executable (script , ScriptContext .Standard .INGEST );
@@ -111,16 +123,27 @@ public ScriptProcessor create(Map<String, Processor.Factory> registry, String pr
111
123
}
112
124
113
125
final Script script ;
126
+ String scriptPropertyUsed ;
114
127
if (Strings .hasLength (file )) {
115
128
script = new Script (FILE , lang , file , (Map <String , Object >)params );
129
+ scriptPropertyUsed = "file" ;
116
130
} else if (Strings .hasLength (inline )) {
117
131
script = new Script (INLINE , lang , inline , (Map <String , Object >)params );
132
+ scriptPropertyUsed = "inline" ;
118
133
} else if (Strings .hasLength (id )) {
119
134
script = new Script (STORED , lang , id , (Map <String , Object >)params );
135
+ scriptPropertyUsed = "id" ;
120
136
} else {
121
137
throw newConfigurationException (TYPE , processorTag , null , "Could not initialize script" );
122
138
}
123
139
140
+ // verify script is able to be compiled before successfully creating processor.
141
+ try {
142
+ scriptService .compile (script , ScriptContext .Standard .INGEST , script .getOptions ());
143
+ } catch (ScriptException e ) {
144
+ throw newConfigurationException (TYPE , processorTag , scriptPropertyUsed , e );
145
+ }
146
+
124
147
return new ScriptProcessor (processorTag , script , scriptService );
125
148
}
126
149
}
0 commit comments