Skip to content

Commit

Permalink
Add tests and 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 f48357e commit 57c62bd
Show file tree
Hide file tree
Showing 12 changed files with 292 additions and 146 deletions.
72 changes: 72 additions & 0 deletions server/src/main/java/org/opensearch/index/mapper/DerivedField.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/*
* 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.index.mapper;

import org.opensearch.common.annotation.PublicApi;
import org.opensearch.core.common.io.stream.StreamInput;
import org.opensearch.core.common.io.stream.StreamOutput;
import org.opensearch.core.common.io.stream.Writeable;
import org.opensearch.core.xcontent.ToXContent;
import org.opensearch.core.xcontent.ToXContentFragment;
import org.opensearch.core.xcontent.XContentBuilder;
import org.opensearch.script.Script;

import java.io.IOException;

/**
* DerivedField representation: expects a name, type and script.
*/
@PublicApi(since = "2.14.0")
public class DerivedField implements Writeable, ToXContentFragment {

private final String name;
private final String type;
private final Script script;

public DerivedField(String name, String type, Script script) {
this.name = name;
this.type = type;
this.script = script;
}

public DerivedField(StreamInput in) throws IOException {
name = in.readString();
type = in.readString();
script = new Script(in);
}

@Override
public void writeTo(StreamOutput out) throws IOException {
out.writeString(name);
out.writeString(type);
script.writeTo(out);
}

@Override
public XContentBuilder toXContent(XContentBuilder builder, ToXContent.Params params) throws IOException {
builder.startObject(name);
builder.field(type);
builder.field("script", script);
builder.endObject();
return builder;
}

public String getName() {
return name;
}

public String getType() {
return type;
}

public Script getScript() {
return script;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@

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;

/**
Expand Down Expand Up @@ -51,6 +53,12 @@ public Builder(String name) {
super(name);
}

public Builder(DerivedField derivedField) {
super(derivedField.getName());
this.type.setValue(derivedField.getType());
this.script.setValue(derivedField.getScript());
}

@Override
protected List<Parameter<?>> getParameters() {
return Arrays.asList(type, script);
Expand Down Expand Up @@ -126,4 +134,27 @@ public String getType() {
public Script getScript() {
return script;
}

public static Map<String, DerivedFieldType> getAllDerivedFieldTypeFromObject(
Map<String, Object> derivedFieldObject,
MapperService mapperService
) {
Map<String, DerivedFieldType> derivedFieldTypes = new HashMap<>();
DocumentMapper documentMapper = mapperService.documentMapperParser().parse(DerivedFieldMapper.CONTENT_TYPE, derivedFieldObject);
if (documentMapper != null && documentMapper.mappers() != null) {
for (Mapper mapper : documentMapper.mappers()) {
if (mapper instanceof DerivedFieldMapper) {
DerivedFieldType derivedFieldType = ((DerivedFieldMapper) mapper).fieldType();
derivedFieldTypes.put(derivedFieldType.name(), derivedFieldType);
}
}
}
return derivedFieldTypes;
}

public static DerivedFieldType getDerivedFieldType(DerivedField derivedField, MapperService mapperService) {
BuilderContext builderContext = new Mapper.BuilderContext(mapperService.getIndexSettings().getSettings(), new ContentPath(1));
Builder builder = new Builder(derivedField);
return builder.build(builderContext).fieldType();
}
}
119 changes: 21 additions & 98 deletions server/src/main/java/org/opensearch/index/mapper/DerivedFieldType.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,11 @@
import org.apache.lucene.search.MultiTermQuery;
import org.apache.lucene.search.Query;
import org.opensearch.common.Nullable;
import org.opensearch.common.annotation.PublicApi;
import org.opensearch.common.geo.ShapeRelation;
import org.opensearch.common.time.DateMathParser;
import org.opensearch.common.unit.Fuzziness;
import org.opensearch.index.analysis.NamedAnalyzer;
import org.opensearch.index.query.DerivedFieldQuery;
import org.opensearch.index.query.QueryShardContext;
import org.opensearch.script.DerivedFieldScript;
Expand All @@ -36,6 +38,7 @@
* Contains logic to execute different type of queries on a derived field of given type.
* @opensearch.internal
*/
@PublicApi(since = "2.14.0")
public final class DerivedFieldType extends MappedFieldType {
private final String type;

Expand Down Expand Up @@ -82,6 +85,10 @@ public String getType() {
return type;
}

public NamedAnalyzer getIndexAnalyzer() {
return typeFieldMapper.mappedFieldType.indexAnalyzer();
}

@Override
public DerivedFieldValueFetcher valueFetcher(QueryShardContext context, SearchLookup searchLookup, String format) {
if (format != null) {
Expand All @@ -94,39 +101,21 @@ public DerivedFieldValueFetcher valueFetcher(QueryShardContext context, SearchLo
public Query termQuery(Object value, QueryShardContext context) {
Query query = typeFieldMapper.mappedFieldType.termQuery(value, context);
DerivedFieldValueFetcher valueFetcher = new DerivedFieldValueFetcher(getDerivedFieldLeafFactory(context));
return new DerivedFieldQuery(
query,
valueFetcher,
context.lookup(),
indexableFieldGenerator,
typeFieldMapper.mappedFieldType.indexAnalyzer()
);
return new DerivedFieldQuery(query, valueFetcher, context.lookup(), indexableFieldGenerator, getIndexAnalyzer());
}

@Override
public Query termQueryCaseInsensitive(Object value, @Nullable QueryShardContext context) {
Query query = typeFieldMapper.mappedFieldType.termQueryCaseInsensitive(value, context);
DerivedFieldValueFetcher valueFetcher = new DerivedFieldValueFetcher(getDerivedFieldLeafFactory(context));
return new DerivedFieldQuery(
query,
valueFetcher,
context.lookup(),
indexableFieldGenerator,
typeFieldMapper.mappedFieldType.indexAnalyzer()
);
return new DerivedFieldQuery(query, valueFetcher, context.lookup(), indexableFieldGenerator, getIndexAnalyzer());
}

@Override
public Query termsQuery(List<?> values, @Nullable QueryShardContext context) {
Query query = typeFieldMapper.mappedFieldType.termsQuery(values, context);
DerivedFieldValueFetcher valueFetcher = new DerivedFieldValueFetcher(getDerivedFieldLeafFactory(context));
return new DerivedFieldQuery(
query,
valueFetcher,
context.lookup(),
indexableFieldGenerator,
typeFieldMapper.mappedFieldType.indexAnalyzer()
);
return new DerivedFieldQuery(query, valueFetcher, context.lookup(), indexableFieldGenerator, getIndexAnalyzer());
}

@Override
Expand All @@ -151,13 +140,7 @@ public Query rangeQuery(
context
);
DerivedFieldValueFetcher valueFetcher = new DerivedFieldValueFetcher(getDerivedFieldLeafFactory(context));
return new DerivedFieldQuery(
query,
valueFetcher,
context.lookup(),
indexableFieldGenerator,
typeFieldMapper.mappedFieldType.indexAnalyzer()
);
return new DerivedFieldQuery(query, valueFetcher, context.lookup(), indexableFieldGenerator, getIndexAnalyzer());
}

@Override
Expand All @@ -171,13 +154,7 @@ public Query fuzzyQuery(
) {
Query query = typeFieldMapper.mappedFieldType.fuzzyQuery(value, fuzziness, prefixLength, maxExpansions, transpositions, context);
DerivedFieldValueFetcher valueFetcher = new DerivedFieldValueFetcher(getDerivedFieldLeafFactory(context));
return new DerivedFieldQuery(
query,
valueFetcher,
context.lookup(),
indexableFieldGenerator,
typeFieldMapper.mappedFieldType.indexAnalyzer()
);
return new DerivedFieldQuery(query, valueFetcher, context.lookup(), indexableFieldGenerator, getIndexAnalyzer());
}

@Override
Expand All @@ -200,13 +177,7 @@ public Query fuzzyQuery(
context
);
DerivedFieldValueFetcher valueFetcher = new DerivedFieldValueFetcher(getDerivedFieldLeafFactory(context));
return new DerivedFieldQuery(
query,
valueFetcher,
context.lookup(),
indexableFieldGenerator,
typeFieldMapper.mappedFieldType.indexAnalyzer()
);
return new DerivedFieldQuery(query, valueFetcher, context.lookup(), indexableFieldGenerator, getIndexAnalyzer());
}

@Override
Expand All @@ -218,13 +189,7 @@ public Query prefixQuery(
) {
Query query = typeFieldMapper.mappedFieldType.prefixQuery(value, method, caseInsensitive, context);
DerivedFieldValueFetcher valueFetcher = new DerivedFieldValueFetcher(getDerivedFieldLeafFactory(context));
return new DerivedFieldQuery(
query,
valueFetcher,
context.lookup(),
indexableFieldGenerator,
typeFieldMapper.mappedFieldType.indexAnalyzer()
);
return new DerivedFieldQuery(query, valueFetcher, context.lookup(), indexableFieldGenerator, getIndexAnalyzer());
}

@Override
Expand All @@ -236,26 +201,14 @@ public Query wildcardQuery(
) {
Query query = typeFieldMapper.mappedFieldType.wildcardQuery(value, method, caseInsensitive, context);
DerivedFieldValueFetcher valueFetcher = new DerivedFieldValueFetcher(getDerivedFieldLeafFactory(context));
return new DerivedFieldQuery(
query,
valueFetcher,
context.lookup(),
indexableFieldGenerator,
typeFieldMapper.mappedFieldType.indexAnalyzer()
);
return new DerivedFieldQuery(query, valueFetcher, context.lookup(), indexableFieldGenerator, getIndexAnalyzer());
}

@Override
public Query normalizedWildcardQuery(String value, @Nullable MultiTermQuery.RewriteMethod method, QueryShardContext context) {
Query query = typeFieldMapper.mappedFieldType.normalizedWildcardQuery(value, method, context);
DerivedFieldValueFetcher valueFetcher = new DerivedFieldValueFetcher(getDerivedFieldLeafFactory(context));
return new DerivedFieldQuery(
query,
valueFetcher,
context.lookup(),
indexableFieldGenerator,
typeFieldMapper.mappedFieldType.indexAnalyzer()
);
return new DerivedFieldQuery(query, valueFetcher, context.lookup(), indexableFieldGenerator, getIndexAnalyzer());
}

@Override
Expand All @@ -269,53 +222,29 @@ public Query regexpQuery(
) {
Query query = typeFieldMapper.mappedFieldType.regexpQuery(value, syntaxFlags, matchFlags, maxDeterminizedStates, method, context);
DerivedFieldValueFetcher valueFetcher = new DerivedFieldValueFetcher(getDerivedFieldLeafFactory(context));
return new DerivedFieldQuery(
query,
valueFetcher,
context.lookup(),
indexableFieldGenerator,
typeFieldMapper.mappedFieldType.indexAnalyzer()
);
return new DerivedFieldQuery(query, valueFetcher, context.lookup(), indexableFieldGenerator, getIndexAnalyzer());
}

@Override
public Query phraseQuery(TokenStream stream, int slop, boolean enablePositionIncrements, QueryShardContext context) throws IOException {
Query query = typeFieldMapper.mappedFieldType.phraseQuery(stream, slop, enablePositionIncrements, context);
DerivedFieldValueFetcher valueFetcher = new DerivedFieldValueFetcher(getDerivedFieldLeafFactory(context));
return new DerivedFieldQuery(
query,
valueFetcher,
context.lookup(),
indexableFieldGenerator,
typeFieldMapper.mappedFieldType.indexAnalyzer()
);
return new DerivedFieldQuery(query, valueFetcher, context.lookup(), indexableFieldGenerator, getIndexAnalyzer());
}

@Override
public Query multiPhraseQuery(TokenStream stream, int slop, boolean enablePositionIncrements, QueryShardContext context)
throws IOException {
Query query = typeFieldMapper.mappedFieldType.multiPhraseQuery(stream, slop, enablePositionIncrements, context);
DerivedFieldValueFetcher valueFetcher = new DerivedFieldValueFetcher(getDerivedFieldLeafFactory(context));
return new DerivedFieldQuery(
query,
valueFetcher,
context.lookup(),
indexableFieldGenerator,
typeFieldMapper.mappedFieldType.indexAnalyzer()
);
return new DerivedFieldQuery(query, valueFetcher, context.lookup(), indexableFieldGenerator, getIndexAnalyzer());
}

@Override
public Query phrasePrefixQuery(TokenStream stream, int slop, int maxExpansions, QueryShardContext context) throws IOException {
Query query = typeFieldMapper.mappedFieldType.phrasePrefixQuery(stream, slop, maxExpansions, context);
DerivedFieldValueFetcher valueFetcher = new DerivedFieldValueFetcher(getDerivedFieldLeafFactory(context));
return new DerivedFieldQuery(
query,
valueFetcher,
context.lookup(),
indexableFieldGenerator,
typeFieldMapper.mappedFieldType.indexAnalyzer()
);
return new DerivedFieldQuery(query, valueFetcher, context.lookup(), indexableFieldGenerator, getIndexAnalyzer());
}

@Override
Expand All @@ -329,13 +258,7 @@ public SpanQuery spanPrefixQuery(String value, SpanMultiTermQueryWrapper.SpanRew
public Query distanceFeatureQuery(Object origin, String pivot, float boost, QueryShardContext context) {
Query query = typeFieldMapper.mappedFieldType.distanceFeatureQuery(origin, pivot, boost, context);
DerivedFieldValueFetcher valueFetcher = new DerivedFieldValueFetcher(getDerivedFieldLeafFactory(context));
return new DerivedFieldQuery(
query,
valueFetcher,
context.lookup(),
indexableFieldGenerator,
typeFieldMapper.mappedFieldType.indexAnalyzer()
);
return new DerivedFieldQuery(query, valueFetcher, context.lookup(), indexableFieldGenerator, getIndexAnalyzer());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
package org.opensearch.index.mapper;

import org.apache.lucene.index.LeafReaderContext;
import org.opensearch.common.annotation.PublicApi;
import org.opensearch.script.DerivedFieldScript;
import org.opensearch.search.lookup.SourceLookup;

Expand All @@ -20,6 +21,7 @@
* It expects DerivedFieldScript.LeafFactory as an input and sets the contract with consumer to call
* {@link #setNextReader(LeafReaderContext)} whenever a segment is switched.
*/
@PublicApi(since = "2.14.0")
public final class DerivedFieldValueFetcher implements ValueFetcher {
private DerivedFieldScript derivedFieldScript;
private final DerivedFieldScript.LeafFactory derivedFieldScriptFactory;
Expand Down
Loading

0 comments on commit 57c62bd

Please sign in to comment.