Skip to content

Commit 080b1a6

Browse files
committed
Support Fields API in conditional ingest processors
1 parent 2258911 commit 080b1a6

File tree

6 files changed

+94
-22
lines changed

6 files changed

+94
-22
lines changed

modules/ingest-common/src/yamlRestTest/resources/rest-api-spec/test/ingest/210_conditional_processor.yml

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,3 +74,39 @@ teardown:
7474
- match: { _source.bytes_source_field: "1kb" }
7575
- match: { _source.conditional_field: "bar" }
7676
- is_false: _source.bytes_target_field
77+
78+
---
79+
"Test conditional processor with dotted field":
80+
- do:
81+
ingest.put_pipeline:
82+
id: "my_pipeline"
83+
body: >
84+
{
85+
"description": "_description",
86+
"processors": [
87+
{
88+
"bytes" : {
89+
"if" : "field('conditional.field').get('') == 'bar'",
90+
"field" : "bytes_source_field",
91+
"target_field" : "bytes_target_field"
92+
}
93+
}
94+
]
95+
}
96+
- match: { acknowledged: true }
97+
98+
- do:
99+
index:
100+
index: test
101+
id: "1"
102+
pipeline: "my_pipeline"
103+
body: {bytes_source_field: "1kb", conditional.field: "bar"}
104+
105+
- do:
106+
get:
107+
index: test
108+
id: "1"
109+
- match: { _source.bytes_source_field: "1kb" }
110+
- match: { _source.conditional.field: "bar" }
111+
- match: { _source.bytes_target_field: 1024 }
112+
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
#
2+
# Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
3+
# or more contributor license agreements. Licensed under the "Elastic License
4+
# 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side
5+
# Public License v 1"; you may not use this file except in compliance with, at
6+
# your election, the "Elastic License 2.0", the "GNU Affero General Public
7+
# License v3.0 only", or the "Server Side Public License, v 1".
8+
#
9+
10+
# This file contains a whitelist for conditional ingest scripts
11+
12+
class org.elasticsearch.script.IngestConditionalScript {
13+
WriteField field(String)
14+
}
15+
16+
class org.elasticsearch.script.field.WriteField {
17+
String getName()
18+
boolean exists()
19+
WriteField move(def)
20+
WriteField overwrite(def)
21+
void remove()
22+
WriteField set(def)
23+
WriteField append(def)
24+
boolean isEmpty()
25+
int size()
26+
Iterator iterator()
27+
def get(def)
28+
def get(int, def)
29+
boolean hasValue(Predicate)
30+
WriteField transform(Function)
31+
WriteField deduplicate()
32+
WriteField removeValuesIf(Predicate)
33+
WriteField removeValue(int)
34+
NestedDocument doc()
35+
NestedDocument doc(int)
36+
Iterable docs()
37+
}

server/src/main/java/org/elasticsearch/ingest/ConditionalProcessor.java

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ public class ConditionalProcessor extends AbstractProcessor implements WrappingP
5656
private final Processor processor;
5757
private final IngestMetric metric;
5858
private final LongSupplier relativeTimeProvider;
59-
private final IngestConditionalScript precompiledConditionScript;
59+
private final IngestConditionalScript.Factory precompiledConditionScriptFactory;
6060

6161
ConditionalProcessor(String tag, String description, Script script, ScriptService scriptService, Processor processor) {
6262
this(tag, description, script, scriptService, processor, System::nanoTime);
@@ -78,12 +78,10 @@ public class ConditionalProcessor extends AbstractProcessor implements WrappingP
7878
this.relativeTimeProvider = relativeTimeProvider;
7979

8080
try {
81-
final IngestConditionalScript.Factory factory = scriptService.compile(script, IngestConditionalScript.CONTEXT);
8281
if (ScriptType.INLINE.equals(script.getType())) {
83-
precompiledConditionScript = factory.newInstance(script.getParams());
82+
precompiledConditionScriptFactory = scriptService.compile(script, IngestConditionalScript.CONTEXT);
8483
} else {
85-
// stored script, so will have to compile at runtime
86-
precompiledConditionScript = null;
84+
precompiledConditionScriptFactory = null;
8785
}
8886
} catch (ScriptException e) {
8987
throw newConfigurationException(TYPE, tag, null, e);
@@ -141,12 +139,12 @@ public void execute(IngestDocument ingestDocument, BiConsumer<IngestDocument, Ex
141139
}
142140

143141
boolean evaluate(IngestDocument ingestDocument) {
144-
IngestConditionalScript script = precompiledConditionScript;
145-
if (script == null) {
146-
IngestConditionalScript.Factory factory = scriptService.compile(condition, IngestConditionalScript.CONTEXT);
147-
script = factory.newInstance(condition.getParams());
142+
IngestConditionalScript.Factory factory = precompiledConditionScriptFactory;
143+
if (factory == null) {
144+
factory = scriptService.compile(condition, IngestConditionalScript.CONTEXT);
148145
}
149-
return script.execute(new UnmodifiableIngestData(new DynamicMap(ingestDocument.getSourceAndMetadata(), FUNCTIONS)));
146+
return factory.newInstance(condition.getParams(), ingestDocument.getCtxMap())
147+
.execute(new UnmodifiableIngestData(new DynamicMap(ingestDocument.getSourceAndMetadata(), FUNCTIONS)));
150148
}
151149

152150
public Processor getInnerProcessor() {

server/src/main/java/org/elasticsearch/script/IngestConditionalScript.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@
1616
/**
1717
* A script used by {@link org.elasticsearch.ingest.ConditionalProcessor}.
1818
*/
19-
public abstract class IngestConditionalScript {
19+
public abstract class IngestConditionalScript extends WriteScript {
2020

21-
public static final String[] PARAMETERS = { "ctx" };
21+
public static final String[] PARAMETERS = { "ctxdsf" /*todo change param name*/ };
2222

2323
/** The context used to compile {@link IngestConditionalScript} factories. */
2424
public static final ScriptContext<Factory> CONTEXT = new ScriptContext<>(
@@ -33,7 +33,8 @@ public abstract class IngestConditionalScript {
3333
/** The generic runtime parameters for the script. */
3434
private final Map<String, Object> params;
3535

36-
public IngestConditionalScript(Map<String, Object> params) {
36+
public IngestConditionalScript(Map<String, Object> params, CtxMap<?> ctxMap) {
37+
super(ctxMap);
3738
this.params = params;
3839
}
3940

@@ -42,9 +43,9 @@ public Map<String, Object> getParams() {
4243
return params;
4344
}
4445

45-
public abstract boolean execute(Map<String, Object> ctx);
46+
public abstract boolean execute(Map<String, Object> params);
4647

4748
public interface Factory {
48-
IngestConditionalScript newInstance(Map<String, Object> params);
49+
IngestConditionalScript newInstance(Map<String, Object> params, CtxMap<?> ctxMap);
4950
}
5051
}

server/src/test/java/org/elasticsearch/ingest/ConditionalProcessorTests.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -206,9 +206,9 @@ public void testRuntimeCompileError() {
206206
if (fail.get()) {
207207
throw new ScriptException("bad script", new ParseException("error", 0), List.of(), "", "lang", null);
208208
} else {
209-
return params -> new IngestConditionalScript(params) {
209+
return (params, ctxMap) -> new IngestConditionalScript(params, ctxMap) {
210210
@Override
211-
public boolean execute(Map<String, Object> ctx) {
211+
public boolean execute(Map<String, Object> params) {
212212
return false;
213213
}
214214
};
@@ -226,9 +226,9 @@ public boolean execute(Map<String, Object> ctx) {
226226
public void testRuntimeError() {
227227
ScriptService scriptService = MockScriptService.singleContext(
228228
IngestConditionalScript.CONTEXT,
229-
code -> params -> new IngestConditionalScript(params) {
229+
code -> (params, ctxMap) -> new IngestConditionalScript(params, ctxMap) {
230230
@Override
231-
public boolean execute(Map<String, Object> ctx) {
231+
public boolean execute(Map<String, Object> params) {
232232
throw new IllegalArgumentException("runtime problem");
233233
}
234234
},

test/framework/src/main/java/org/elasticsearch/script/MockScriptEngine.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -163,10 +163,10 @@ public void execute() {
163163
} else if (context.instanceClazz.equals(AggregationScript.class)) {
164164
return context.factoryClazz.cast(new MockAggregationScript(script));
165165
} else if (context.instanceClazz.equals(IngestConditionalScript.class)) {
166-
IngestConditionalScript.Factory factory = parameters -> new IngestConditionalScript(parameters) {
166+
IngestConditionalScript.Factory factory = (parameters, ctxMap) -> new IngestConditionalScript(parameters, ctxMap) {
167167
@Override
168-
public boolean execute(Map<String, Object> ctx) {
169-
return (boolean) script.apply(ctx);
168+
public boolean execute(Map<String, Object> params) {
169+
return (boolean) script.apply(params);
170170
}
171171
};
172172
return context.factoryClazz.cast(factory);

0 commit comments

Comments
 (0)