Skip to content

Commit

Permalink
Add fieldType to AbstractQueryBuilder and SortBuilder (#15328)
Browse files Browse the repository at this point in the history
* Add fieldType to AbstractQueryBuilder and FieldSortBuilder

Signed-off-by: David Zane <[email protected]>

* Add fieldType to AbstractQueryBuilder and SortBuilder

Signed-off-by: David Zane <[email protected]>

---------

Signed-off-by: David Zane <[email protected]>
Signed-off-by: Ankit Jain <[email protected]>
Co-authored-by: Ankit Jain <[email protected]>
  • Loading branch information
dzane17 and jainankitk committed Aug 30, 2024
1 parent 579f2aa commit 839ba0b
Show file tree
Hide file tree
Showing 46 changed files with 379 additions and 6 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
- [Workload Management] Add rejection logic for co-ordinator and shard level requests ([#15428](https://github.com/opensearch-project/OpenSearch/pull/15428)))
- Adding translog durability validation in index templates ([#15494](https://github.com/opensearch-project/OpenSearch/pull/15494))
- Add index creation using the context field ([#15290](https://github.com/opensearch-project/OpenSearch/pull/15290))
- Add fieldType to AbstractQueryBuilder and FieldSortBuilder ([#15328](https://github.com/opensearch-project/OpenSearch/pull/15328)))
- [Reader Writer Separation] Add searchOnly replica routing configuration ([#15410](https://github.com/opensearch-project/OpenSearch/pull/15410))
- [Workload Management] Add query group level failure tracking ([#15227](https://github.com/opensearch-project/OpenSearch/pull/15527))

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -400,6 +400,11 @@ protected void doXContent(XContentBuilder builder, Params params) throws IOExcep
builder.endObject();
}

@Override
public final String fieldName() {
return getDefaultFieldName();
}

@Override
protected Query doToQuery(QueryShardContext context) throws IOException {
final MappedFieldType ft = context.fieldMapper(field);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,11 @@ protected void doXContent(XContentBuilder builder, Params params) throws IOExcep
builder.endObject();
}

@Override
public final String fieldName() {
return getDefaultFieldName();
}

public static HasChildQueryBuilder fromXContent(XContentParser parser) throws IOException {
float boost = AbstractQueryBuilder.DEFAULT_BOOST;
String childType = null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,11 @@ protected void doXContent(XContentBuilder builder, Params params) throws IOExcep
builder.endObject();
}

@Override
public final String fieldName() {
return getDefaultFieldName();
}

public static HasParentQueryBuilder fromXContent(XContentParser parser) throws IOException {
float boost = AbstractQueryBuilder.DEFAULT_BOOST;
String parentType = null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,11 @@ protected void doXContent(XContentBuilder builder, Params params) throws IOExcep
builder.endObject();
}

@Override
public final String fieldName() {
return getDefaultFieldName();
}

public static ParentIdQueryBuilder fromXContent(XContentParser parser) throws IOException {
float boost = AbstractQueryBuilder.DEFAULT_BOOST;
String type = null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -356,6 +356,11 @@ protected void doXContent(XContentBuilder builder, Params params) throws IOExcep
builder.endObject();
}

@Override
public final String fieldName() {
return getDefaultFieldName();
}

private static final ConstructingObjectParser<PercolateQueryBuilder, Void> PARSER = new ConstructingObjectParser<>(NAME, args -> {
String field = (String) args[0];
BytesReference document = (BytesReference) args[1];
Expand Down
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 @@ -74,6 +74,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 +113,27 @@ protected void printBoostAndQueryName(XContentBuilder builder) throws IOExceptio
}
}

/**
* Returns field name as String.
* Abstract method to be implemented by all child classes.
*/
public abstract String fieldName();

/**
* Default method for child classes which do not have a custom {@link #fieldName()} implementation.
*/
protected static String getDefaultFieldName() {
return null;
};

/**
* Returns field type as String for QueryBuilder classes which have a defined fieldName.
* Else returns null.
*/
public final String getFieldType() {
return fieldType;
};

@Override
public final Query toQuery(QueryShardContext context) throws IOException {
Query query = doToQuery(context);
Expand All @@ -125,6 +147,7 @@ public final Query toQuery(QueryShardContext context) throws IOException {
context.addNamedQuery(queryName, query);
}
}
fieldType = context.getFieldTypeString(fieldName());
return query;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,11 @@ protected void doXContent(XContentBuilder builder, Params params) throws IOExcep
builder.endObject();
}

@Override
public final String fieldName() {
return getDefaultFieldName();
}

private static void doXArrayContent(ParseField field, List<QueryBuilder> clauses, XContentBuilder builder, Params params)
throws IOException {
if (clauses.isEmpty()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,11 @@ protected void doXContent(XContentBuilder builder, Params params) throws IOExcep
builder.endObject();
}

@Override
public final String fieldName() {
return getDefaultFieldName();
}

public static BoostingQueryBuilder fromXContent(XContentParser parser) throws IOException {
QueryBuilder positiveQuery = null;
boolean positiveQueryFound = false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,11 @@ protected void doXContent(XContentBuilder builder, Params params) throws IOExcep
builder.endObject();
}

@Override
public final String fieldName() {
return getDefaultFieldName();
}

public static ConstantScoreQueryBuilder fromXContent(XContentParser parser) throws IOException {
QueryBuilder query = null;
boolean queryFound = false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,11 @@ protected void doXContent(XContentBuilder builder, Params params) throws IOExcep
builder.endObject();
}

@Override
public final String fieldName() {
return getDefaultFieldName();
}

public static DisMaxQueryBuilder fromXContent(XContentParser parser) throws IOException {
float boost = AbstractQueryBuilder.DEFAULT_BOOST;
float tieBreaker = DisMaxQueryBuilder.DEFAULT_TIE_BREAKER;
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
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,11 @@ protected void doXContent(XContentBuilder builder, Params params) throws IOExcep
builder.endObject();
}

@Override
public final String fieldName() {
return getDefaultFieldName();
}

private static final ObjectParser<IdsQueryBuilder, Void> PARSER = new ObjectParser<>(NAME, IdsQueryBuilder::new);

static {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,11 @@ protected void doXContent(XContentBuilder builder, Params params) throws IOExcep
builder.endObject();
}

@Override
public final String fieldName() {
return getDefaultFieldName();
}

public static IntervalQueryBuilder fromXContent(XContentParser parser) throws IOException {
if (parser.nextToken() != XContentParser.Token.FIELD_NAME) {
throw new ParsingException(parser.getTokenLocation(), "Expected [FIELD_NAME] but got [" + parser.currentToken() + "]");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,11 @@ protected void doXContent(XContentBuilder builder, Params params) throws IOExcep
builder.endObject();
}

@Override
public final String fieldName() {
return getDefaultFieldName();
}

private static final ObjectParser<MatchAllQueryBuilder, Void> PARSER = new ObjectParser<>(NAME, MatchAllQueryBuilder::new);

static {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,11 @@ protected void doXContent(XContentBuilder builder, Params params) throws IOExcep
builder.endObject();
}

@Override
public final String fieldName() {
return getDefaultFieldName();
}

public static MatchNoneQueryBuilder fromXContent(XContentParser parser) throws IOException {
String currentFieldName = null;
XContentParser.Token token;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -792,6 +792,11 @@ protected void doXContent(XContentBuilder builder, Params params) throws IOExcep
builder.endObject();
}

@Override
public final String fieldName() {
return getDefaultFieldName();
}

public static MoreLikeThisQueryBuilder fromXContent(XContentParser parser) throws IOException {
// document inputs
List<String> fields = null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -631,6 +631,11 @@ public void doXContent(XContentBuilder builder, Params params) throws IOExceptio
builder.endObject();
}

@Override
public final String fieldName() {
return getDefaultFieldName();
}

public static MultiMatchQueryBuilder fromXContent(XContentParser parser) throws IOException {
Object value = null;
Map<String, Float> fieldsBoosts = new HashMap<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,11 @@ protected void doXContent(XContentBuilder builder, Params params) throws IOExcep
builder.endObject();
}

@Override
public final String fieldName() {
return getDefaultFieldName();
}

public static NestedQueryBuilder fromXContent(XContentParser parser) throws IOException {
float boost = AbstractQueryBuilder.DEFAULT_BOOST;
ScoreMode scoreMode = ScoreMode.Avg;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -364,6 +364,22 @@ public MappedFieldType fieldMapper(String name) {
return failIfFieldMappingNotFound(name, mapperService.fieldType(name));
}

/**
* Returns field type as String for the given field name.
* If field is not mapped or mapperService is null, returns null.
*/
public String getFieldTypeString(String fieldName) {
if (fieldName != null) {
if (mapperService != null) {
MappedFieldType mappedFieldType = mapperService.fieldType(fieldName);
if (mappedFieldType != null) {
return mappedFieldType.typeName();
}
}
}
return null;
}

public ObjectMapper getObjectMapper(String name) {
return mapperService.getObjectMapper(name);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -647,6 +647,11 @@ protected void doXContent(XContentBuilder builder, Params params) throws IOExcep
builder.endObject();
}

@Override
public final String fieldName() {
return getDefaultFieldName();
}

public static QueryStringQueryBuilder fromXContent(XContentParser parser) throws IOException {
String currentFieldName = null;
XContentParser.Token token;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,11 @@ protected void doXContent(XContentBuilder builder, Params builderParams) throws
builder.endObject();
}

@Override
public final String fieldName() {
return getDefaultFieldName();
}

public static ScriptQueryBuilder fromXContent(XContentParser parser) throws IOException {
// also, when caching, since its isCacheable is false, will result in loading all bit set...
Script script = null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -479,6 +479,11 @@ protected void doXContent(XContentBuilder builder, Params params) throws IOExcep
builder.endObject();
}

@Override
public final String fieldName() {
return getDefaultFieldName();
}

public static SimpleQueryStringBuilder fromXContent(XContentParser parser) throws IOException {
String currentFieldName = null;
String queryBody = null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,11 @@ protected void doXContent(XContentBuilder builder, Params params) throws IOExcep
builder.endObject();
}

@Override
public final String fieldName() {
return getDefaultFieldName();
}

public static SpanContainingQueryBuilder fromXContent(XContentParser parser) throws IOException {
float boost = AbstractQueryBuilder.DEFAULT_BOOST;
String queryName = null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,11 @@ protected void doXContent(XContentBuilder builder, Params params) throws IOExcep
builder.endObject();
}

@Override
public final String fieldName() {
return getDefaultFieldName();
}

public static SpanFirstQueryBuilder fromXContent(XContentParser parser) throws IOException {
float boost = AbstractQueryBuilder.DEFAULT_BOOST;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,11 @@ protected void doXContent(XContentBuilder builder, Params params) throws IOExcep
builder.endObject();
}

@Override
public final String fieldName() {
return getDefaultFieldName();
}

public static SpanMultiTermQueryBuilder fromXContent(XContentParser parser) throws IOException {
String currentFieldName = null;
MultiTermQueryBuilder subQuery = null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,11 @@ protected void doXContent(XContentBuilder builder, Params params) throws IOExcep
builder.endObject();
}

@Override
public final String fieldName() {
return getDefaultFieldName();
}

public static SpanNearQueryBuilder fromXContent(XContentParser parser) throws IOException {
float boost = AbstractQueryBuilder.DEFAULT_BOOST;
int slop = DEFAULT_SLOP;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,11 @@ protected void doXContent(XContentBuilder builder, Params params) throws IOExcep
builder.endObject();
}

@Override
public final String fieldName() {
return getDefaultFieldName();
}

public static SpanNotQueryBuilder fromXContent(XContentParser parser) throws IOException {
float boost = AbstractQueryBuilder.DEFAULT_BOOST;

Expand Down
Loading

0 comments on commit 839ba0b

Please sign in to comment.