From 5c02c5545c52caefb9cf23beb638f0847e377fe2 Mon Sep 17 00:00:00 2001 From: Mohammad Qureshi Date: Tue, 19 Mar 2024 12:58:53 -0700 Subject: [PATCH] Move DerivedFieldType to its own file and add an enum for DerivedFieldSupportedTypes Signed-off-by: Mohammad Qureshi --- .../index/mapper/DerivedFieldMapper.java | 92 +++++-------------- 1 file changed, 22 insertions(+), 70 deletions(-) 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 a4ff914cd657d..c02b618bfccd5 100644 --- a/server/src/main/java/org/opensearch/index/mapper/DerivedFieldMapper.java +++ b/server/src/main/java/org/opensearch/index/mapper/DerivedFieldMapper.java @@ -10,18 +10,14 @@ import org.apache.lucene.document.FieldType; import org.apache.lucene.index.IndexOptions; -import org.apache.lucene.search.Query; -import org.opensearch.common.Nullable; +import org.apache.lucene.index.IndexableField; import org.opensearch.core.xcontent.XContentBuilder; -import org.opensearch.index.query.QueryShardContext; import org.opensearch.script.Script; -import org.opensearch.search.lookup.SearchLookup; import java.io.IOException; import java.util.Arrays; -import java.util.Collections; import java.util.List; -import java.util.Map; +import java.util.function.Function; /** * A field mapper for derived fields @@ -30,7 +26,7 @@ */ public class DerivedFieldMapper extends ParametrizedFieldMapper { - public static final String CONTENT_TYPE = "derived_field"; + public static final String CONTENT_TYPE = "derived"; /** * Default parameters for the boolean field mapper @@ -59,7 +55,6 @@ 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", @@ -76,7 +71,9 @@ public static class Builder extends ParametrizedFieldMapper.Builder { m -> toType(m).script ).setSerializerCheck((id, ic, value) -> value != null); - public Builder(String name) { super(name); } + public Builder(String name) { + super(name); + } @Override protected List> getParameters() { @@ -85,69 +82,16 @@ protected List> getParameters() { @Override public DerivedFieldMapper build(BuilderContext context) { - MappedFieldType ft = new DerivedFieldType(buildFullName(context)); + FieldMapper fieldMapper = DerivedFieldSupportedTypes.getFieldMapperFromType(type.getValue(), name, context); + Function fieldFunction = + DerivedFieldSupportedTypes.getIndexableFieldGeneratorType(type.getValue(), name); + DerivedFieldType ft = new DerivedFieldType(buildFullName(context), type.getValue(), script.getValue(), fieldMapper, fieldFunction); return new DerivedFieldMapper(name, ft, multiFieldsBuilder.build(this, context), copyTo.build(), this); } } public static final TypeParser PARSER = new TypeParser((n, c) -> new Builder(n)); - - /** - * Field type for derived field mapper - * - * @opensearch.internal - */ - public static final class DerivedFieldType extends MappedFieldType { - - public DerivedFieldType( - String name, - boolean isIndexed, - boolean isStored, - boolean hasDocValues, - Map meta - ) { - super(name, isIndexed, isStored, hasDocValues, TextSearchInfo.NONE, meta); - } - - public DerivedFieldType(String name) { this(name, false, false, false, Collections.emptyMap()); } - - @Override - public String typeName() { - return CONTENT_TYPE; - } - - @Override - public ValueFetcher valueFetcher(QueryShardContext context, SearchLookup searchLookup, String format ) { - if (format != null) { - throw new IllegalArgumentException("Field [" + name() + "] of type [" + typeName() + "] doesn't support formats."); - } - - // TODO Return to this during Query implementation. The derived fields don't typically exist in _source but - // we may want fetch the field name from source if a 'script' is not provided. - return new SourceValueFetcher(name(), context) { - @Override - protected Object parseSourceValue(Object value) { - return null; - } - }; - } - - @Override - public Query termQuery(Object value, @Nullable QueryShardContext context) { - throw new IllegalArgumentException("Field [" + name() + "] of type [" + typeName() + "] does not support term queries"); - } - - @Override - public Query existsQuery(QueryShardContext context) { - throw new IllegalArgumentException("Field [" + name() + "] of type [" + typeName() + "] does not support exist queries"); - } - - @Override - public boolean isAggregatable() { return false; } - } - private final String type; - private final Script script; protected DerivedFieldMapper( @@ -163,7 +107,9 @@ protected DerivedFieldMapper( } @Override - public DerivedFieldType fieldType() { return (DerivedFieldType) super.fieldType(); } + public DerivedFieldType fieldType() { + return (DerivedFieldType) super.fieldType(); + } @Override protected void parseCreateField(ParseContext context) throws IOException { @@ -173,7 +119,9 @@ protected void parseCreateField(ParseContext context) throws IOException { } @Override - public ParametrizedFieldMapper.Builder getMergeBuilder() { return new Builder(simpleName()).init(this); } + public ParametrizedFieldMapper.Builder getMergeBuilder() { + return new Builder(simpleName()).init(this); + } @Override protected String contentType() { @@ -187,7 +135,11 @@ protected void doXContentBody(XContentBuilder builder, boolean includeDefaults, copyTo.toXContent(builder, params); } - public String getType() { return type; } + public String getType() { + return type; + } - public Script getScript() { return script; } + public Script getScript() { + return script; + } }