|
@@ -43,10 +43,16 @@ import org.elasticsearch.cluster.node.DiscoveryNode;
|
|
import org.elasticsearch.cluster.service.ClusterService;
|
|
import org.elasticsearch.cluster.service.ClusterService;
|
|
import org.elasticsearch.common.bytes.BytesArray;
|
|
import org.elasticsearch.common.bytes.BytesArray;
|
|
import org.elasticsearch.common.logging.Loggers;
|
|
import org.elasticsearch.common.logging.Loggers;
|
|
|
|
+import org.elasticsearch.common.settings.Settings;
|
|
import org.elasticsearch.common.util.concurrent.EsExecutors;
|
|
import org.elasticsearch.common.util.concurrent.EsExecutors;
|
|
import org.elasticsearch.common.xcontent.XContentType;
|
|
import org.elasticsearch.common.xcontent.XContentType;
|
|
import org.elasticsearch.index.VersionType;
|
|
import org.elasticsearch.index.VersionType;
|
|
import org.elasticsearch.plugins.IngestPlugin;
|
|
import org.elasticsearch.plugins.IngestPlugin;
|
|
|
|
+import org.elasticsearch.script.MockScriptEngine;
|
|
|
|
+import org.elasticsearch.script.Script;
|
|
|
|
+import org.elasticsearch.script.ScriptModule;
|
|
|
|
+import org.elasticsearch.script.ScriptService;
|
|
|
|
+import org.elasticsearch.script.ScriptType;
|
|
import org.elasticsearch.test.ESTestCase;
|
|
import org.elasticsearch.test.ESTestCase;
|
|
import org.elasticsearch.test.MockLogAppender;
|
|
import org.elasticsearch.test.MockLogAppender;
|
|
import org.elasticsearch.threadpool.ThreadPool;
|
|
import org.elasticsearch.threadpool.ThreadPool;
|
|
@@ -64,6 +70,7 @@ import java.util.Objects;
|
|
import java.util.concurrent.ExecutorService;
|
|
import java.util.concurrent.ExecutorService;
|
|
import java.util.function.BiConsumer;
|
|
import java.util.function.BiConsumer;
|
|
import java.util.function.Consumer;
|
|
import java.util.function.Consumer;
|
|
|
|
+import java.util.function.LongSupplier;
|
|
|
|
|
|
import static java.util.Collections.emptyMap;
|
|
import static java.util.Collections.emptyMap;
|
|
import static java.util.Collections.emptySet;
|
|
import static java.util.Collections.emptySet;
|
|
@@ -263,6 +270,89 @@ public class IngestServiceTests extends ESTestCase {
|
|
ingestService.validatePipeline(Collections.singletonMap(discoveryNode, ingestInfo), putRequest);
|
|
ingestService.validatePipeline(Collections.singletonMap(discoveryNode, ingestInfo), putRequest);
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ public void testHasProcessor() throws Exception {
|
|
|
|
+ IngestService ingestService = createWithProcessors();
|
|
|
|
+ String id = "_id";
|
|
|
|
+ Pipeline pipeline = ingestService.getPipeline(id);
|
|
|
|
+ assertThat(pipeline, nullValue());
|
|
|
|
+ ClusterState clusterState = ClusterState.builder(new ClusterName("_name")).build(); // Start empty
|
|
|
|
+
|
|
|
|
+ PutPipelineRequest putRequest = new PutPipelineRequest("_id", new BytesArray(
|
|
|
|
+ "{\"processors\": [{\"set\" : {\"field\": \"_field\", \"value\": \"_value\", \"tag\": \"tag1\"}}," +
|
|
|
|
+ "{\"remove\" : {\"field\": \"_field\", \"tag\": \"tag2\"}}]}"),
|
|
|
|
+ XContentType.JSON);
|
|
|
|
+ ClusterState previousClusterState = clusterState;
|
|
|
|
+ clusterState = IngestService.innerPut(putRequest, clusterState);
|
|
|
|
+ ingestService.applyClusterState(new ClusterChangedEvent("", clusterState, previousClusterState));
|
|
|
|
+ pipeline = ingestService.getPipeline(id);
|
|
|
|
+ assertThat(pipeline, notNullValue());
|
|
|
|
+
|
|
|
|
+ assertTrue(ingestService.hasProcessor(id, Processor.class));
|
|
|
|
+ assertTrue(ingestService.hasProcessor(id, WrappingProcessorImpl.class));
|
|
|
|
+ assertTrue(ingestService.hasProcessor(id, WrappingProcessor.class));
|
|
|
|
+ assertTrue(ingestService.hasProcessor(id, FakeProcessor.class));
|
|
|
|
+
|
|
|
|
+ assertFalse(ingestService.hasProcessor(id, ConditionalProcessor.class));
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public void testHasProcessorComplexConditional() throws Exception {
|
|
|
|
+ LongSupplier relativeTimeProvider = mock(LongSupplier.class);
|
|
|
|
+ String scriptName = "conditionalScript";
|
|
|
|
+ ScriptService scriptService = new ScriptService(Settings.builder().build(),
|
|
|
|
+ Collections.singletonMap(
|
|
|
|
+ Script.DEFAULT_SCRIPT_LANG,
|
|
|
|
+ new MockScriptEngine(
|
|
|
|
+ Script.DEFAULT_SCRIPT_LANG,
|
|
|
|
+ Collections.singletonMap(
|
|
|
|
+ scriptName, ctx -> {
|
|
|
|
+ ctx.get("_type");
|
|
|
|
+ return true;
|
|
|
|
+ }
|
|
|
|
+ ),
|
|
|
|
+ Collections.emptyMap()
|
|
|
|
+ )
|
|
|
|
+ ),
|
|
|
|
+ new HashMap<>(ScriptModule.CORE_CONTEXTS)
|
|
|
|
+ );
|
|
|
|
+
|
|
|
|
+ Map<String, Processor.Factory> processors = new HashMap<>();
|
|
|
|
+ processors.put("complexSet", (factories, tag, config) -> {
|
|
|
|
+ String field = (String) config.remove("field");
|
|
|
|
+ String value = (String) config.remove("value");
|
|
|
|
+
|
|
|
|
+ return new ConditionalProcessor(randomAlphaOfLength(10),
|
|
|
|
+ new Script(
|
|
|
|
+ ScriptType.INLINE, Script.DEFAULT_SCRIPT_LANG,
|
|
|
|
+ scriptName, Collections.emptyMap()), scriptService,
|
|
|
|
+ new ConditionalProcessor(randomAlphaOfLength(10) + "-nested",
|
|
|
|
+ new Script(
|
|
|
|
+ ScriptType.INLINE, Script.DEFAULT_SCRIPT_LANG,
|
|
|
|
+ scriptName, Collections.emptyMap()), scriptService,
|
|
|
|
+ new FakeProcessor("complexSet", tag, (ingestDocument) -> ingestDocument.setFieldValue(field, value))));
|
|
|
|
+ });
|
|
|
|
+
|
|
|
|
+ IngestService ingestService = createWithProcessors(processors);
|
|
|
|
+ String id = "_id";
|
|
|
|
+ Pipeline pipeline = ingestService.getPipeline(id);
|
|
|
|
+ assertThat(pipeline, nullValue());
|
|
|
|
+ ClusterState clusterState = ClusterState.builder(new ClusterName("_name")).build(); // Start empty
|
|
|
|
+
|
|
|
|
+ PutPipelineRequest putRequest = new PutPipelineRequest(id,
|
|
|
|
+ new BytesArray("{\"processors\": [{\"complexSet\" : {\"field\": \"_field\", \"value\": \"_value\"}}]}"), XContentType.JSON);
|
|
|
|
+ ClusterState previousClusterState = clusterState;
|
|
|
|
+ clusterState = IngestService.innerPut(putRequest, clusterState);
|
|
|
|
+ ingestService.applyClusterState(new ClusterChangedEvent("", clusterState, previousClusterState));
|
|
|
|
+ pipeline = ingestService.getPipeline(id);
|
|
|
|
+ assertThat(pipeline, notNullValue());
|
|
|
|
+
|
|
|
|
+ assertTrue(ingestService.hasProcessor(id, Processor.class));
|
|
|
|
+ assertTrue(ingestService.hasProcessor(id, WrappingProcessor.class));
|
|
|
|
+ assertTrue(ingestService.hasProcessor(id, FakeProcessor.class));
|
|
|
|
+ assertTrue(ingestService.hasProcessor(id, ConditionalProcessor.class));
|
|
|
|
+
|
|
|
|
+ assertFalse(ingestService.hasProcessor(id, WrappingProcessorImpl.class));
|
|
|
|
+ }
|
|
|
|
+
|
|
public void testCrud() throws Exception {
|
|
public void testCrud() throws Exception {
|
|
IngestService ingestService = createWithProcessors();
|
|
IngestService ingestService = createWithProcessors();
|
|
String id = "_id";
|
|
String id = "_id";
|
|
@@ -946,7 +1036,7 @@ public class IngestServiceTests extends ESTestCase {
|
|
assertThat(IngestService.getProcessorName(processor), equalTo(name + ":" + tag));
|
|
assertThat(IngestService.getProcessorName(processor), equalTo(name + ":" + tag));
|
|
|
|
|
|
ConditionalProcessor conditionalProcessor = mock(ConditionalProcessor.class);
|
|
ConditionalProcessor conditionalProcessor = mock(ConditionalProcessor.class);
|
|
- when(conditionalProcessor.getProcessor()).thenReturn(processor);
|
|
|
|
|
|
+ when(conditionalProcessor.getInnerProcessor()).thenReturn(processor);
|
|
assertThat(IngestService.getProcessorName(conditionalProcessor), equalTo(name + ":" + tag));
|
|
assertThat(IngestService.getProcessorName(conditionalProcessor), equalTo(name + ":" + tag));
|
|
|
|
|
|
PipelineProcessor pipelineProcessor = mock(PipelineProcessor.class);
|
|
PipelineProcessor pipelineProcessor = mock(PipelineProcessor.class);
|
|
@@ -1012,42 +1102,11 @@ public class IngestServiceTests extends ESTestCase {
|
|
processors.put("set", (factories, tag, config) -> {
|
|
processors.put("set", (factories, tag, config) -> {
|
|
String field = (String) config.remove("field");
|
|
String field = (String) config.remove("field");
|
|
String value = (String) config.remove("value");
|
|
String value = (String) config.remove("value");
|
|
- return new Processor() {
|
|
|
|
- @Override
|
|
|
|
- public IngestDocument execute(IngestDocument ingestDocument) {
|
|
|
|
- ingestDocument.setFieldValue(field, value);
|
|
|
|
- return ingestDocument;
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- @Override
|
|
|
|
- public String getType() {
|
|
|
|
- return "set";
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- @Override
|
|
|
|
- public String getTag() {
|
|
|
|
- return tag;
|
|
|
|
- }
|
|
|
|
- };
|
|
|
|
|
|
+ return new FakeProcessor("set", tag, (ingestDocument) ->ingestDocument.setFieldValue(field, value));
|
|
});
|
|
});
|
|
processors.put("remove", (factories, tag, config) -> {
|
|
processors.put("remove", (factories, tag, config) -> {
|
|
String field = (String) config.remove("field");
|
|
String field = (String) config.remove("field");
|
|
- return new Processor() {
|
|
|
|
- @Override
|
|
|
|
- public IngestDocument execute(IngestDocument ingestDocument) {
|
|
|
|
- ingestDocument.removeField(field);
|
|
|
|
- return ingestDocument;
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- @Override
|
|
|
|
- public String getType() {
|
|
|
|
- return "remove";
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- @Override
|
|
|
|
- public String getTag() {
|
|
|
|
- return tag;
|
|
|
|
- }
|
|
|
|
|
|
+ return new WrappingProcessorImpl("remove", tag, (ingestDocument -> ingestDocument.removeField(field))) {
|
|
};
|
|
};
|
|
});
|
|
});
|
|
return createWithProcessors(processors);
|
|
return createWithProcessors(processors);
|