Skip to content

Commit

Permalink
Add fieldType to Abstract Query Builder
Browse files Browse the repository at this point in the history
Signed-off-by: David Zane <davizane@amazon.com>
  • Loading branch information
dzane17 committed Aug 21, 2024
1 parent 13163ab commit 14ad54f
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,8 @@ class FieldTypeLookup implements Iterable<MappedFieldType> {
}

/**
* Returns the mapped field type for the given field name.
* Returns the {@link MappedFieldType} for the given field name
* or null if the field name is not found.
*/
public MappedFieldType get(String field) {
String concreteField = aliasToConcreteName.getOrDefault(field, field);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -622,7 +622,8 @@ public DocumentMapperForType documentMapperWithAutoCreate() {
}

/**
* Given the full name of a field, returns its {@link MappedFieldType}.
* Given the full name of a field, returns its {@link MappedFieldType}
* or null if the field is not found.
*/
public MappedFieldType fieldType(String fullName) {
return this.mapper == null ? null : this.mapper.fieldTypes().get(fullName);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
import org.opensearch.core.xcontent.XContentBuilder;
import org.opensearch.core.xcontent.XContentLocation;
import org.opensearch.core.xcontent.XContentParser;
import org.opensearch.index.mapper.MappedFieldType;

import java.io.IOException;
import java.math.BigInteger;
Expand All @@ -74,6 +75,7 @@ public abstract class AbstractQueryBuilder<QB extends AbstractQueryBuilder<QB>>
public static final ParseField BOOST_FIELD = new ParseField("boost");

protected String queryName;
protected String fieldType;
protected float boost = DEFAULT_BOOST;

protected AbstractQueryBuilder() {
Expand Down Expand Up @@ -112,6 +114,14 @@ protected void printBoostAndQueryName(XContentBuilder builder) throws IOExceptio
}
}

public String fieldName() {
return "none";
};

public final String getFieldType() {
return fieldType;
};

@Override
public final Query toQuery(QueryShardContext context) throws IOException {
Query query = doToQuery(context);
Expand All @@ -125,6 +135,8 @@ public final Query toQuery(QueryShardContext context) throws IOException {
context.addNamedQuery(queryName, query);
}
}
MappedFieldType mappedFieldType = context.getMapperService().fieldType(fieldName());
fieldType = (mappedFieldType == null) ? null : mappedFieldType.typeName();
return query;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,8 @@ protected Query doToQuery(QueryShardContext context) throws IOException {
return fieldType.distanceFeatureQuery(origin.origin(), pivot, 1.0f, context);
}

String fieldName() {
@Override
public String fieldName() {
return field;
}

Expand Down

0 comments on commit 14ad54f

Please sign in to comment.