Skip to content

Commit

Permalink
Refactor code
Browse files Browse the repository at this point in the history
Signed-off-by: Rishabh Maurya <[email protected]>
  • Loading branch information
rishabhmaurya committed Apr 4, 2024
1 parent 57c62bd commit faaae0e
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,7 @@ public DerivedFieldMapper build(BuilderContext context) {
name
);
DerivedFieldType ft = new DerivedFieldType(
buildFullName(context),
type.getValue(),
script.getValue(),
new DerivedField(buildFullName(context), type.getValue(), script.getValue()),
fieldMapper,
fieldFunction
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
import org.opensearch.index.query.DerivedFieldQuery;
import org.opensearch.index.query.QueryShardContext;
import org.opensearch.script.DerivedFieldScript;
import org.opensearch.script.Script;
import org.opensearch.search.lookup.SearchLookup;

import java.io.IOException;
Expand All @@ -40,40 +39,30 @@
*/
@PublicApi(since = "2.14.0")
public final class DerivedFieldType extends MappedFieldType {
private final String type;

private final Script script;
private final DerivedField derivedField;

FieldMapper typeFieldMapper;

final Function<Object, IndexableField> indexableFieldGenerator;

public DerivedFieldType(
String name,
String type,
Script script,
DerivedField derivedField,
boolean isIndexed,
boolean isStored,
boolean hasDocValues,
Map<String, String> meta,
FieldMapper typeFieldMapper,
Function<Object, IndexableField> fieldFunction
) {
super(name, isIndexed, isStored, hasDocValues, typeFieldMapper.fieldType().getTextSearchInfo(), meta);
this.type = type;
this.script = script;
super(derivedField.getName(), isIndexed, isStored, hasDocValues, typeFieldMapper.fieldType().getTextSearchInfo(), meta);
this.derivedField = derivedField;
this.typeFieldMapper = typeFieldMapper;
this.indexableFieldGenerator = fieldFunction;
}

public DerivedFieldType(
String name,
String type,
Script script,
FieldMapper typeFieldMapper,
Function<Object, IndexableField> fieldFunction
) {
this(name, type, script, false, false, false, Collections.emptyMap(), typeFieldMapper, fieldFunction);
public DerivedFieldType(DerivedField derivedField, FieldMapper typeFieldMapper, Function<Object, IndexableField> fieldFunction) {
this(derivedField, false, false, false, Collections.emptyMap(), typeFieldMapper, fieldFunction);
}

@Override
Expand All @@ -82,7 +71,7 @@ public String typeName() {
}

public String getType() {
return type;
return derivedField.getType();
}

public NamedAnalyzer getIndexAnalyzer() {
Expand Down Expand Up @@ -280,7 +269,7 @@ private DerivedFieldScript.LeafFactory getDerivedFieldLeafFactory(QueryShardCont
+ "]"
);
}
DerivedFieldScript.Factory factory = context.compile(script, DerivedFieldScript.CONTEXT);
return factory.newFactory(script.getParams(), context.lookup());
DerivedFieldScript.Factory factory = context.compile(derivedField.getScript(), DerivedFieldScript.CONTEXT);
return factory.newFactory(derivedField.getScript().getParams(), context.lookup());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,7 @@ private DerivedFieldType createDerivedFieldType(String type) {
Mapper.BuilderContext context = mock(Mapper.BuilderContext.class);
when(context.path()).thenReturn(new ContentPath());
return new DerivedFieldType(
type + " _derived_field",
type,
new Script(""),
new DerivedField(type + " _derived_field", type, new Script("")),
DerivedFieldSupportedTypes.getFieldMapperFromType(type, type + "_derived_field", context),
DerivedFieldSupportedTypes.getIndexableFieldGeneratorType(type, type + "_derived_field")
);
Expand Down

0 comments on commit faaae0e

Please sign in to comment.