diff --git a/server/src/main/java/org/opensearch/index/mapper/DerivedField.java b/server/src/main/java/org/opensearch/index/mapper/DerivedField.java index 7ebe4e5f0b0e8..7bd97b0bfc81a 100644 --- a/server/src/main/java/org/opensearch/index/mapper/DerivedField.java +++ b/server/src/main/java/org/opensearch/index/mapper/DerivedField.java @@ -28,18 +28,31 @@ public class DerivedField implements Writeable, ToXContentFragment { private final String name; private final String type; + + private final String sourceIndexedField; + private final Script script; - public DerivedField(String name, String type, Script script) { + public DerivedField(String name, String type, Script script, String sourceIndexedField) { this.name = name; this.type = type; this.script = script; + this.sourceIndexedField = sourceIndexedField; + } + + public DerivedField(String name, String type, Script script) { + this(name, type, script, null); } public DerivedField(StreamInput in) throws IOException { name = in.readString(); type = in.readString(); script = new Script(in); + if (in.readBoolean()) { + sourceIndexedField = in.readString(); + } else { + sourceIndexedField = null; + } } @Override @@ -47,6 +60,12 @@ public void writeTo(StreamOutput out) throws IOException { out.writeString(name); out.writeString(type); script.writeTo(out); + if (sourceIndexedField != null) { + out.writeBoolean(true); + out.writeString(sourceIndexedField); + } else { + out.writeBoolean(false); + } } @Override @@ -54,6 +73,9 @@ public XContentBuilder toXContent(XContentBuilder builder, ToXContent.Params par builder.startObject(name); builder.field("type", type); builder.field("script", script); + if (sourceIndexedField != null) { + builder.field("source_indexed_field", sourceIndexedField); + } builder.endObject(); return builder; } @@ -70,9 +92,13 @@ public Script getScript() { return script; } + public String getSourceIndexedField() { + return sourceIndexedField; + } + @Override public int hashCode() { - return Objects.hash(name, type, script); + return Objects.hash(name, type, script, sourceIndexedField); } @Override @@ -84,7 +110,10 @@ public boolean equals(Object obj) { return false; } DerivedField other = (DerivedField) obj; - return Objects.equals(name, other.name) && Objects.equals(type, other.type) && Objects.equals(script, other.script); + return Objects.equals(name, other.name) + && Objects.equals(type, other.type) + && Objects.equals(script, other.script) + && Objects.equals(sourceIndexedField, other.sourceIndexedField); } } diff --git a/server/src/main/java/org/opensearch/index/mapper/DerivedFieldMapper.java b/server/src/main/java/org/opensearch/index/mapper/DerivedFieldMapper.java index c6ae71320c35c..c69252332da6f 100644 --- a/server/src/main/java/org/opensearch/index/mapper/DerivedFieldMapper.java +++ b/server/src/main/java/org/opensearch/index/mapper/DerivedFieldMapper.java @@ -10,13 +10,12 @@ import org.apache.lucene.index.IndexableField; import org.opensearch.core.xcontent.XContentBuilder; +import org.opensearch.index.analysis.IndexAnalyzers; import org.opensearch.script.Script; import java.io.IOException; import java.util.Arrays; -import java.util.HashMap; import java.util.List; -import java.util.Map; import java.util.function.Function; /** @@ -28,6 +27,8 @@ public class DerivedFieldMapper extends ParametrizedFieldMapper { public static final String CONTENT_TYPE = "derived"; + protected final IndexAnalyzers indexAnalyzers; + private static DerivedFieldMapper toType(FieldMapper in) { return (DerivedFieldMapper) in; } @@ -38,9 +39,10 @@ private static DerivedFieldMapper toType(FieldMapper in) { * @opensearch.internal */ public static class Builder extends ParametrizedFieldMapper.Builder { - // TODO: The type of parameter may change here if the actual underlying FieldType object is needed private final Parameter type = Parameter.stringParam("type", false, m -> toType(m).type, ""); + private final IndexAnalyzers indexAnalyzers; + private final Parameter