Skip to content

Commit

Permalink
Adding WithFieldName interface for QueryBuilders with fieldName (#15705
Browse files Browse the repository at this point in the history
…) (#15763)

Signed-off-by: David Zane <davizane@amazon.com>
Signed-off-by: Ankit Jain <akjain@amazon.com>
Co-authored-by: Ankit Jain <akjain@amazon.com>
(cherry picked from commit f5c897c)
  • Loading branch information
dzane17 authored Sep 9, 2024
1 parent 085a2f5 commit 4742400
Show file tree
Hide file tree
Showing 17 changed files with 58 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,9 @@
*
* @opensearch.internal
*/
public abstract class AbstractGeometryQueryBuilder<QB extends AbstractGeometryQueryBuilder<QB>> extends AbstractQueryBuilder<QB> {
public abstract class AbstractGeometryQueryBuilder<QB extends AbstractGeometryQueryBuilder<QB>> extends AbstractQueryBuilder<QB>
implements
WithFieldName {

public static final String DEFAULT_SHAPE_INDEX_NAME = "shapes";
public static final String DEFAULT_SHAPE_FIELD_NAME = "shape";
Expand Down Expand Up @@ -218,6 +220,7 @@ protected void doWriteTo(StreamOutput out) throws IOException {
/**
* @return the name of the field that will be queried
*/
@Override
public String fieldName() {
return fieldName;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
*
* @opensearch.internal
*/
public abstract class BaseTermQueryBuilder<QB extends BaseTermQueryBuilder<QB>> extends AbstractQueryBuilder<QB> {
public abstract class BaseTermQueryBuilder<QB extends BaseTermQueryBuilder<QB>> extends AbstractQueryBuilder<QB> implements WithFieldName {

public static final ParseField VALUE_FIELD = new ParseField("value");

Expand Down Expand Up @@ -153,6 +153,7 @@ protected void doWriteTo(StreamOutput out) throws IOException {
}

/** Returns the field name used in this query. */
@Override
public String fieldName() {
return this.fieldName;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@
* @opensearch.internal
*/
@Deprecated
public class CommonTermsQueryBuilder extends AbstractQueryBuilder<CommonTermsQueryBuilder> {
public class CommonTermsQueryBuilder extends AbstractQueryBuilder<CommonTermsQueryBuilder> implements WithFieldName {

public static final String COMMON_TERMS_QUERY_DEPRECATION_MSG = "[match] query which can efficiently "
+ "skip blocks of documents if the total number of hits is not tracked";
Expand Down Expand Up @@ -152,6 +152,7 @@ protected void doWriteTo(StreamOutput out) throws IOException {
out.writeFloat(cutoffFrequency);
}

@Override
public String fieldName() {
return this.fieldName;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@
*
* @opensearch.internal
*/
public class DistanceFeatureQueryBuilder extends AbstractQueryBuilder<DistanceFeatureQueryBuilder> {
public class DistanceFeatureQueryBuilder extends AbstractQueryBuilder<DistanceFeatureQueryBuilder> implements WithFieldName {
public static final String NAME = "distance_feature";

private static final ParseField FIELD_FIELD = new ParseField("field");
Expand Down 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 @@ -59,7 +59,7 @@
*
* @opensearch.internal
*/
public class ExistsQueryBuilder extends AbstractQueryBuilder<ExistsQueryBuilder> {
public class ExistsQueryBuilder extends AbstractQueryBuilder<ExistsQueryBuilder> implements WithFieldName {
public static final String NAME = "exists";

public static final ParseField FIELD_FIELD = new ParseField("field");
Expand Down Expand Up @@ -89,6 +89,7 @@ protected void doWriteTo(StreamOutput out) throws IOException {
/**
* @return the field name that has to exist for this query to match
*/
@Override
public String fieldName() {
return this.fieldName;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,10 @@
*
* @opensearch.internal
*/
public class FieldMaskingSpanQueryBuilder extends AbstractQueryBuilder<FieldMaskingSpanQueryBuilder> implements SpanQueryBuilder {
public class FieldMaskingSpanQueryBuilder extends AbstractQueryBuilder<FieldMaskingSpanQueryBuilder>
implements
SpanQueryBuilder,
WithFieldName {

public static final String NAME = "span_field_masking";
public static final ParseField SPAN_FIELD_MASKING_FIELD = new ParseField(NAME, "field_masking_span");
Expand Down Expand Up @@ -100,6 +103,7 @@ protected void doWriteTo(StreamOutput out) throws IOException {
/**
* @return the field name for this query
*/
@Override
public String fieldName() {
return this.fieldName;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@
*
* @opensearch.internal
* */
public class GeoBoundingBoxQueryBuilder extends AbstractQueryBuilder<GeoBoundingBoxQueryBuilder> {
public class GeoBoundingBoxQueryBuilder extends AbstractQueryBuilder<GeoBoundingBoxQueryBuilder> implements WithFieldName {
public static final String NAME = "geo_bounding_box";

/** Default type for executing this query (memory as of this writing). */
Expand Down Expand Up @@ -263,6 +263,7 @@ public GeoExecType type() {
}

/** Returns the name of the field to base the bounding box computation on. */
@Override
public String fieldName() {
return this.fieldName;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@
*
* @opensearch.internal
*/
public class GeoDistanceQueryBuilder extends AbstractQueryBuilder<GeoDistanceQueryBuilder> {
public class GeoDistanceQueryBuilder extends AbstractQueryBuilder<GeoDistanceQueryBuilder> implements WithFieldName {
public static final String NAME = "geo_distance";

/** Default for distance unit computation. */
Expand Down Expand Up @@ -129,6 +129,7 @@ protected void doWriteTo(StreamOutput out) throws IOException {
}

/** Name of the field this query is operating on. */
@Override
public String fieldName() {
return this.fieldName;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@
*
* @opensearch.internal
*/
public class GeoPolygonQueryBuilder extends AbstractQueryBuilder<GeoPolygonQueryBuilder> {
public class GeoPolygonQueryBuilder extends AbstractQueryBuilder<GeoPolygonQueryBuilder> implements WithFieldName {
public static final String NAME = "geo_polygon";

/**
Expand Down Expand Up @@ -131,6 +131,7 @@ protected void doWriteTo(StreamOutput out) throws IOException {
out.writeBoolean(ignoreUnmapped);
}

@Override
public String fieldName() {
return fieldName;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@
*
* @opensearch.internal
*/
public class MatchBoolPrefixQueryBuilder extends AbstractQueryBuilder<MatchBoolPrefixQueryBuilder> {
public class MatchBoolPrefixQueryBuilder extends AbstractQueryBuilder<MatchBoolPrefixQueryBuilder> implements WithFieldName {

public static final String NAME = "match_bool_prefix";

Expand Down Expand Up @@ -127,6 +127,7 @@ protected void doWriteTo(StreamOutput out) throws IOException {
}

/** Returns the field name used in this query. */
@Override
public String fieldName() {
return this.fieldName;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@
*
* @opensearch.internal
*/
public class MatchPhrasePrefixQueryBuilder extends AbstractQueryBuilder<MatchPhrasePrefixQueryBuilder> {
public class MatchPhrasePrefixQueryBuilder extends AbstractQueryBuilder<MatchPhrasePrefixQueryBuilder> implements WithFieldName {
public static final String NAME = "match_phrase_prefix";
public static final ParseField MAX_EXPANSIONS_FIELD = new ParseField("max_expansions");
public static final ParseField ZERO_TERMS_QUERY_FIELD = new ParseField("zero_terms_query");
Expand Down Expand Up @@ -109,6 +109,7 @@ protected void doWriteTo(StreamOutput out) throws IOException {
}

/** Returns the field name used in this query. */
@Override
public String fieldName() {
return this.fieldName;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@
*
* @opensearch.internal
*/
public class MatchPhraseQueryBuilder extends AbstractQueryBuilder<MatchPhraseQueryBuilder> {
public class MatchPhraseQueryBuilder extends AbstractQueryBuilder<MatchPhraseQueryBuilder> implements WithFieldName {
public static final String NAME = "match_phrase";
public static final ParseField SLOP_FIELD = new ParseField("slop");
public static final ParseField ZERO_TERMS_QUERY_FIELD = new ParseField("zero_terms_query");
Expand Down Expand Up @@ -100,6 +100,7 @@ protected void doWriteTo(StreamOutput out) throws IOException {
}

/** Returns the field name used in this query. */
@Override
public String fieldName() {
return this.fieldName;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@
*
* @opensearch.internal
*/
public class MatchQueryBuilder extends AbstractQueryBuilder<MatchQueryBuilder> {
public class MatchQueryBuilder extends AbstractQueryBuilder<MatchQueryBuilder> implements WithFieldName {

private static final String CUTOFF_FREQUENCY_DEPRECATION_MSG = "you can omit this option, "
+ "the [match] query can skip block of documents efficiently if the total number of hits is not tracked";
Expand Down Expand Up @@ -171,6 +171,7 @@ protected void doWriteTo(StreamOutput out) throws IOException {
}

/** Returns the field name used in this query. */
@Override
public String fieldName() {
return this.fieldName;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,4 @@
*
* @opensearch.internal
*/
public interface MultiTermQueryBuilder extends QueryBuilder {
/**
* Get the field name for this query.
*/
String fieldName();
}
public interface MultiTermQueryBuilder extends QueryBuilder, WithFieldName {}
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,7 @@ public void visit(QueryBuilderVisitor visitor) {
*
* @opensearch.internal
*/
public static class SpanGapQueryBuilder implements SpanQueryBuilder {
public static class SpanGapQueryBuilder implements SpanQueryBuilder, WithFieldName {
public static final String NAME = "span_gap";

/** Name of field to match against. */
Expand Down Expand Up @@ -358,6 +358,7 @@ public SpanGapQueryBuilder(StreamInput in) throws IOException {
/**
* @return fieldName The name of the field
*/
@Override
public String fieldName() {
return fieldName;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@
*
* @opensearch.internal
*/
public class TermsQueryBuilder extends AbstractQueryBuilder<TermsQueryBuilder> {
public class TermsQueryBuilder extends AbstractQueryBuilder<TermsQueryBuilder> implements WithFieldName {
public static final String NAME = "terms";

private final String fieldName;
Expand Down Expand Up @@ -269,6 +269,7 @@ protected void doWriteTo(StreamOutput out) throws IOException {
}
}

@Override
public String fieldName() {
return this.fieldName;
}
Expand Down
21 changes: 21 additions & 0 deletions server/src/main/java/org/opensearch/index/query/WithFieldName.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/*
* 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.query;

/**
* Interface for classes with a fieldName method
*
* @opensearch.internal
*/
public interface WithFieldName {
/**
* Get the field name for this query.
*/
String fieldName();
}

0 comments on commit 4742400

Please sign in to comment.