Skip to content

Commit a4235ca

Browse files
committed
Add fieldType to Abstract Query Builder
Signed-off-by: David Zane <[email protected]>
1 parent b30df02 commit a4235ca

38 files changed

+207
-3
lines changed

modules/mapper-extras/src/main/java/org/opensearch/index/query/RankFeatureQueryBuilder.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -400,6 +400,11 @@ protected void doXContent(XContentBuilder builder, Params params) throws IOExcep
400400
builder.endObject();
401401
}
402402

403+
@Override
404+
public final String fieldName() {
405+
return getDefaultFieldName();
406+
}
407+
403408
@Override
404409
protected Query doToQuery(QueryShardContext context) throws IOException {
405410
final MappedFieldType ft = context.fieldMapper(field);

modules/parent-join/src/main/java/org/opensearch/join/query/HasChildQueryBuilder.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,11 @@ protected void doXContent(XContentBuilder builder, Params params) throws IOExcep
264264
builder.endObject();
265265
}
266266

267+
@Override
268+
public final String fieldName() {
269+
return getDefaultFieldName();
270+
}
271+
267272
public static HasChildQueryBuilder fromXContent(XContentParser parser) throws IOException {
268273
float boost = AbstractQueryBuilder.DEFAULT_BOOST;
269274
String childType = null;

modules/parent-join/src/main/java/org/opensearch/join/query/HasParentQueryBuilder.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,11 @@ protected void doXContent(XContentBuilder builder, Params params) throws IOExcep
233233
builder.endObject();
234234
}
235235

236+
@Override
237+
public final String fieldName() {
238+
return getDefaultFieldName();
239+
}
240+
236241
public static HasParentQueryBuilder fromXContent(XContentParser parser) throws IOException {
237242
float boost = AbstractQueryBuilder.DEFAULT_BOOST;
238243
String parentType = null;

modules/parent-join/src/main/java/org/opensearch/join/query/ParentIdQueryBuilder.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,11 @@ protected void doXContent(XContentBuilder builder, Params params) throws IOExcep
130130
builder.endObject();
131131
}
132132

133+
@Override
134+
public final String fieldName() {
135+
return getDefaultFieldName();
136+
}
137+
133138
public static ParentIdQueryBuilder fromXContent(XContentParser parser) throws IOException {
134139
float boost = AbstractQueryBuilder.DEFAULT_BOOST;
135140
String type = null;

modules/percolator/src/main/java/org/opensearch/percolator/PercolateQueryBuilder.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -356,6 +356,11 @@ protected void doXContent(XContentBuilder builder, Params params) throws IOExcep
356356
builder.endObject();
357357
}
358358

359+
@Override
360+
public final String fieldName() {
361+
return getDefaultFieldName();
362+
}
363+
359364
private static final ConstructingObjectParser<PercolateQueryBuilder, Void> PARSER = new ConstructingObjectParser<>(NAME, args -> {
360365
String field = (String) args[0];
361366
BytesReference document = (BytesReference) args[1];

server/src/main/java/org/opensearch/index/mapper/FieldTypeLookup.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,8 @@ class FieldTypeLookup implements Iterable<MappedFieldType> {
101101
}
102102

103103
/**
104-
* Returns the mapped field type for the given field name.
104+
* Returns the {@link MappedFieldType} for the given field name
105+
* or null if the field name is not found.
105106
*/
106107
public MappedFieldType get(String field) {
107108
String concreteField = aliasToConcreteName.getOrDefault(field, field);

server/src/main/java/org/opensearch/index/mapper/MapperService.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -622,7 +622,8 @@ public DocumentMapperForType documentMapperWithAutoCreate() {
622622
}
623623

624624
/**
625-
* Given the full name of a field, returns its {@link MappedFieldType}.
625+
* Given the full name of a field, returns its {@link MappedFieldType}
626+
* or null if the field is not found.
626627
*/
627628
public MappedFieldType fieldType(String fullName) {
628629
return this.mapper == null ? null : this.mapper.fieldTypes().get(fullName);

server/src/main/java/org/opensearch/index/query/AbstractQueryBuilder.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ public abstract class AbstractQueryBuilder<QB extends AbstractQueryBuilder<QB>>
7474
public static final ParseField BOOST_FIELD = new ParseField("boost");
7575

7676
protected String queryName;
77+
protected String fieldType;
7778
protected float boost = DEFAULT_BOOST;
7879

7980
protected AbstractQueryBuilder() {
@@ -112,6 +113,23 @@ protected void printBoostAndQueryName(XContentBuilder builder) throws IOExceptio
112113
}
113114
}
114115

116+
/**
117+
* Returns field name as String.
118+
* Abstract method to be implemented by all child classes
119+
*/
120+
public abstract String fieldName();
121+
122+
/**
123+
* Default method for child QueryBuilder classes which do not have a custom {@link #fieldName()} implementation.
124+
*/
125+
protected static String getDefaultFieldName() {
126+
return null;
127+
};
128+
129+
public final String getFieldType() {
130+
return fieldType;
131+
};
132+
115133
@Override
116134
public final Query toQuery(QueryShardContext context) throws IOException {
117135
Query query = doToQuery(context);
@@ -125,6 +143,7 @@ public final Query toQuery(QueryShardContext context) throws IOException {
125143
context.addNamedQuery(queryName, query);
126144
}
127145
}
146+
fieldType = context.getFieldTypeString(fieldName());
128147
return query;
129148
}
130149

server/src/main/java/org/opensearch/index/query/BoolQueryBuilder.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,11 @@ protected void doXContent(XContentBuilder builder, Params params) throws IOExcep
270270
builder.endObject();
271271
}
272272

273+
@Override
274+
public final String fieldName() {
275+
return getDefaultFieldName();
276+
}
277+
273278
private static void doXArrayContent(ParseField field, List<QueryBuilder> clauses, XContentBuilder builder, Params params)
274279
throws IOException {
275280
if (clauses.isEmpty()) {

server/src/main/java/org/opensearch/index/query/BoostingQueryBuilder.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,11 @@ protected void doXContent(XContentBuilder builder, Params params) throws IOExcep
151151
builder.endObject();
152152
}
153153

154+
@Override
155+
public final String fieldName() {
156+
return getDefaultFieldName();
157+
}
158+
154159
public static BoostingQueryBuilder fromXContent(XContentParser parser) throws IOException {
155160
QueryBuilder positiveQuery = null;
156161
boolean positiveQueryFound = false;

0 commit comments

Comments
 (0)