Skip to content

Commit 839ba0b

Browse files
dzane17jainankitk
andauthored
Add fieldType to AbstractQueryBuilder and SortBuilder (opensearch-project#15328)
* 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]>
1 parent 579f2aa commit 839ba0b

File tree

46 files changed

+379
-6
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

46 files changed

+379
-6
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
3636
- [Workload Management] Add rejection logic for co-ordinator and shard level requests ([#15428](https://github.com/opensearch-project/OpenSearch/pull/15428)))
3737
- Adding translog durability validation in index templates ([#15494](https://github.com/opensearch-project/OpenSearch/pull/15494))
3838
- Add index creation using the context field ([#15290](https://github.com/opensearch-project/OpenSearch/pull/15290))
39+
- Add fieldType to AbstractQueryBuilder and FieldSortBuilder ([#15328](https://github.com/opensearch-project/OpenSearch/pull/15328)))
3940
- [Reader Writer Separation] Add searchOnly replica routing configuration ([#15410](https://github.com/opensearch-project/OpenSearch/pull/15410))
4041
- [Workload Management] Add query group level failure tracking ([#15227](https://github.com/opensearch-project/OpenSearch/pull/15527))
4142

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: 23 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,27 @@ 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 classes which do not have a custom {@link #fieldName()} implementation.
124+
*/
125+
protected static String getDefaultFieldName() {
126+
return null;
127+
};
128+
129+
/**
130+
* Returns field type as String for QueryBuilder classes which have a defined fieldName.
131+
* Else returns null.
132+
*/
133+
public final String getFieldType() {
134+
return fieldType;
135+
};
136+
115137
@Override
116138
public final Query toQuery(QueryShardContext context) throws IOException {
117139
Query query = doToQuery(context);
@@ -125,6 +147,7 @@ public final Query toQuery(QueryShardContext context) throws IOException {
125147
context.addNamedQuery(queryName, query);
126148
}
127149
}
150+
fieldType = context.getFieldTypeString(fieldName());
128151
return query;
129152
}
130153

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()) {

0 commit comments

Comments
 (0)