-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
--------- Signed-off-by: Gao Binlong <[email protected]>
- Loading branch information
1 parent
1d8bbd5
commit 6d2d4dd
Showing
8 changed files
with
781 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
161 changes: 161 additions & 0 deletions
161
modules/ingest-common/src/main/java/org/opensearch/ingest/common/CopyProcessor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,161 @@ | ||
/* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* | ||
* The OpenSearch Contributors require contributions made to | ||
* this file be licensed under the Apache-2.0 license or a | ||
* compatible open source license. | ||
*/ | ||
|
||
package org.opensearch.ingest.common; | ||
|
||
import org.opensearch.core.common.Strings; | ||
import org.opensearch.ingest.AbstractProcessor; | ||
import org.opensearch.ingest.ConfigurationUtils; | ||
import org.opensearch.ingest.IngestDocument; | ||
import org.opensearch.ingest.Processor; | ||
import org.opensearch.script.ScriptService; | ||
import org.opensearch.script.TemplateScript; | ||
|
||
import java.util.Map; | ||
|
||
public final class CopyProcessor extends AbstractProcessor { | ||
public static final String TYPE = "copy"; | ||
|
||
private final TemplateScript.Factory sourceField; | ||
private final TemplateScript.Factory targetField; | ||
|
||
private final boolean ignoreMissing; | ||
|
||
private final boolean removeSource; | ||
|
||
private final boolean overrideTarget; | ||
|
||
CopyProcessor(String tag, String description, TemplateScript.Factory sourceField, TemplateScript.Factory targetField) { | ||
this(tag, description, sourceField, targetField, false, false, false); | ||
} | ||
|
||
CopyProcessor( | ||
String tag, | ||
String description, | ||
TemplateScript.Factory sourceField, | ||
TemplateScript.Factory targetField, | ||
boolean ignoreMissing, | ||
boolean removeSource, | ||
boolean overrideTarget | ||
) { | ||
super(tag, description); | ||
this.sourceField = sourceField; | ||
this.targetField = targetField; | ||
this.ignoreMissing = ignoreMissing; | ||
this.removeSource = removeSource; | ||
this.overrideTarget = overrideTarget; | ||
} | ||
|
||
public TemplateScript.Factory getSourceField() { | ||
return sourceField; | ||
} | ||
|
||
public TemplateScript.Factory getTargetField() { | ||
return targetField; | ||
} | ||
|
||
public boolean isIgnoreMissing() { | ||
return ignoreMissing; | ||
} | ||
|
||
public boolean isRemoveSource() { | ||
return removeSource; | ||
} | ||
|
||
public boolean isOverrideTarget() { | ||
return overrideTarget; | ||
} | ||
|
||
@Override | ||
public IngestDocument execute(IngestDocument document) { | ||
String source = document.renderTemplate(sourceField); | ||
final boolean sourceFieldPathIsNullOrEmpty = Strings.isNullOrEmpty(source); | ||
if (sourceFieldPathIsNullOrEmpty || document.hasField(source, true) == false) { | ||
if (ignoreMissing) { | ||
return document; | ||
} else if (sourceFieldPathIsNullOrEmpty) { | ||
throw new IllegalArgumentException("source field path cannot be null nor empty"); | ||
} else { | ||
throw new IllegalArgumentException("source field [" + source + "] doesn't exist"); | ||
} | ||
} | ||
|
||
String target = document.renderTemplate(targetField); | ||
if (Strings.isNullOrEmpty(target)) { | ||
throw new IllegalArgumentException("target field path cannot be null nor empty"); | ||
} | ||
if (source.equals(target)) { | ||
throw new IllegalArgumentException("source field path and target field path cannot be same"); | ||
} | ||
|
||
if (overrideTarget || document.hasField(target, true) == false || document.getFieldValue(target, Object.class) == null) { | ||
Object sourceValue = document.getFieldValue(source, Object.class); | ||
document.setFieldValue(target, IngestDocument.deepCopy(sourceValue)); | ||
} else { | ||
throw new IllegalArgumentException("target field [" + target + "] already exists"); | ||
} | ||
|
||
if (removeSource) { | ||
document.removeField(source); | ||
} | ||
|
||
return document; | ||
} | ||
|
||
@Override | ||
public String getType() { | ||
return TYPE; | ||
} | ||
|
||
public static final class Factory implements Processor.Factory { | ||
|
||
private final ScriptService scriptService; | ||
|
||
public Factory(ScriptService scriptService) { | ||
this.scriptService = scriptService; | ||
} | ||
|
||
@Override | ||
public CopyProcessor create( | ||
Map<String, Processor.Factory> registry, | ||
String processorTag, | ||
String description, | ||
Map<String, Object> config | ||
) throws Exception { | ||
String sourceField = ConfigurationUtils.readStringProperty(TYPE, processorTag, config, "source_field"); | ||
TemplateScript.Factory sourceFieldTemplate = ConfigurationUtils.compileTemplate( | ||
TYPE, | ||
processorTag, | ||
"source_field", | ||
sourceField, | ||
scriptService | ||
); | ||
String targetField = ConfigurationUtils.readStringProperty(TYPE, processorTag, config, "target_field"); | ||
TemplateScript.Factory targetFieldTemplate = ConfigurationUtils.compileTemplate( | ||
TYPE, | ||
processorTag, | ||
"target_field", | ||
targetField, | ||
scriptService | ||
); | ||
boolean ignoreMissing = ConfigurationUtils.readBooleanProperty(TYPE, processorTag, config, "ignore_missing", false); | ||
boolean removeSource = ConfigurationUtils.readBooleanProperty(TYPE, processorTag, config, "remove_source", false); | ||
boolean overrideTarget = ConfigurationUtils.readBooleanProperty(TYPE, processorTag, config, "override_target", false); | ||
|
||
return new CopyProcessor( | ||
processorTag, | ||
description, | ||
sourceFieldTemplate, | ||
targetFieldTemplate, | ||
ignoreMissing, | ||
removeSource, | ||
overrideTarget | ||
); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
101 changes: 101 additions & 0 deletions
101
...s/ingest-common/src/test/java/org/opensearch/ingest/common/CopyProcessorFactoryTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
/* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* | ||
* The OpenSearch Contributors require contributions made to | ||
* this file be licensed under the Apache-2.0 license or a | ||
* compatible open source license. | ||
*/ | ||
|
||
package org.opensearch.ingest.common; | ||
|
||
import org.opensearch.OpenSearchException; | ||
import org.opensearch.OpenSearchParseException; | ||
import org.opensearch.ingest.TestTemplateService; | ||
import org.opensearch.test.OpenSearchTestCase; | ||
import org.junit.Before; | ||
|
||
import java.util.Collections; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
import static org.hamcrest.CoreMatchers.equalTo; | ||
|
||
public class CopyProcessorFactoryTests extends OpenSearchTestCase { | ||
|
||
private CopyProcessor.Factory factory; | ||
|
||
@Before | ||
public void init() { | ||
factory = new CopyProcessor.Factory(TestTemplateService.instance()); | ||
} | ||
|
||
public void testCreate() throws Exception { | ||
boolean ignoreMissing = randomBoolean(); | ||
boolean removeSource = randomBoolean(); | ||
boolean overrideTarget = randomBoolean(); | ||
Map<String, Object> config = new HashMap<>(); | ||
config.put("source_field", "source"); | ||
config.put("target_field", "target"); | ||
config.put("ignore_missing", ignoreMissing); | ||
config.put("remove_source", removeSource); | ||
config.put("override_target", overrideTarget); | ||
String processorTag = randomAlphaOfLength(10); | ||
CopyProcessor copyProcessor = factory.create(null, processorTag, null, config); | ||
assertThat(copyProcessor.getTag(), equalTo(processorTag)); | ||
assertThat(copyProcessor.getSourceField().newInstance(Collections.emptyMap()).execute(), equalTo("source")); | ||
assertThat(copyProcessor.getTargetField().newInstance(Collections.emptyMap()).execute(), equalTo("target")); | ||
assertThat(copyProcessor.isIgnoreMissing(), equalTo(ignoreMissing)); | ||
assertThat(copyProcessor.isRemoveSource(), equalTo(removeSource)); | ||
assertThat(copyProcessor.isOverrideTarget(), equalTo(overrideTarget)); | ||
} | ||
|
||
public void testCreateWithSourceField() throws Exception { | ||
Map<String, Object> config = new HashMap<>(); | ||
try { | ||
factory.create(null, null, null, config); | ||
fail("factory create should have failed"); | ||
} catch (OpenSearchParseException e) { | ||
assertThat(e.getMessage(), equalTo("[source_field] required property is missing")); | ||
} | ||
|
||
config.put("source_field", null); | ||
try { | ||
factory.create(null, null, null, config); | ||
fail("factory create should have failed"); | ||
} catch (OpenSearchParseException e) { | ||
assertThat(e.getMessage(), equalTo("[source_field] required property is missing")); | ||
} | ||
} | ||
|
||
public void testCreateWithTargetField() throws Exception { | ||
Map<String, Object> config = new HashMap<>(); | ||
config.put("source_field", "source"); | ||
try { | ||
factory.create(null, null, null, config); | ||
fail("factory create should have failed"); | ||
} catch (OpenSearchParseException e) { | ||
assertThat(e.getMessage(), equalTo("[target_field] required property is missing")); | ||
} | ||
|
||
config.put("source_field", "source"); | ||
config.put("target_field", null); | ||
try { | ||
factory.create(null, null, null, config); | ||
fail("factory create should have failed"); | ||
} catch (OpenSearchParseException e) { | ||
assertThat(e.getMessage(), equalTo("[target_field] required property is missing")); | ||
} | ||
} | ||
|
||
public void testInvalidMustacheTemplate() throws Exception { | ||
CopyProcessor.Factory factory = new CopyProcessor.Factory(TestTemplateService.instance(true)); | ||
Map<String, Object> config = new HashMap<>(); | ||
config.put("source_field", "{{source}}"); | ||
config.put("target_field", "target"); | ||
String processorTag = randomAlphaOfLength(10); | ||
OpenSearchException exception = expectThrows(OpenSearchException.class, () -> factory.create(null, processorTag, null, config)); | ||
assertThat(exception.getMessage(), equalTo("java.lang.RuntimeException: could not compile script")); | ||
assertThat(exception.getMetadata("opensearch.processor_tag").get(0), equalTo(processorTag)); | ||
} | ||
|
||
} |
Oops, something went wrong.