Skip to content

Commit

Permalink
Move DerivedFieldType to its own file and add an enum for DerivedFiel…
Browse files Browse the repository at this point in the history
…dSupportedTypes

Signed-off-by: Mohammad Qureshi <[email protected]>
  • Loading branch information
qreshi committed Mar 20, 2024
1 parent 16ef291 commit 5c02c55
Showing 1 changed file with 22 additions and 70 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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<String> type = Parameter.stringParam(
"type",
Expand All @@ -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<Parameter<?>> getParameters() {
Expand All @@ -85,69 +82,16 @@ protected List<Parameter<?>> getParameters() {

@Override
public DerivedFieldMapper build(BuilderContext context) {
MappedFieldType ft = new DerivedFieldType(buildFullName(context));
FieldMapper fieldMapper = DerivedFieldSupportedTypes.getFieldMapperFromType(type.getValue(), name, context);
Function<Object, IndexableField> 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<String, String> 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(
Expand All @@ -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 {
Expand All @@ -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() {
Expand All @@ -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;
}
}

0 comments on commit 5c02c55

Please sign in to comment.