Skip to content

Commit

Permalink
Query refactoring: Adding getBuilderPrototype() method to all QueryPa…
Browse files Browse the repository at this point in the history
…rsers

Currently there is a registry for all QueryParsers accessible via the IndicesQueriesModule.
For deserializing nested queries e.g. for the BoolQueryBuilder (elastic#11121) we need to look up
query builders by their name to be able to deserialize using a prototype builder of the concrete
class. This PR adds a getBuilderPrototype() method to each query parser so we can re-use the
parser registry to get the corresponding builder using the query name.

Closes elastic#11344
  • Loading branch information
cbuescher committed May 26, 2015
1 parent 00df861 commit 1e93997
Show file tree
Hide file tree
Showing 106 changed files with 407 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ public class AndQueryBuilder extends QueryBuilder {

private String queryName;

static final AndQueryBuilder PROTOTYPE = new AndQueryBuilder();

public AndQueryBuilder(QueryBuilder... filters) {
for (QueryBuilder filter : filters) {
this.filters.add(filter);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,4 +115,9 @@ public Query parse(QueryParseContext parseContext) throws IOException, QueryPars
}
return query;
}

@Override
public AndQueryBuilder getBuilderPrototype() {
return AndQueryBuilder.PROTOTYPE;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ public class BoolQueryBuilder extends QueryBuilder implements BoostableQueryBuil

private String queryName;

static final BoolQueryBuilder PROTOTYPE = new BoolQueryBuilder();

/**
* Adds a query that <b>must</b> appear in the matching documents and will
* contribute to scoring.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -171,4 +171,9 @@ public Query parse(QueryParseContext parseContext) throws IOException, QueryPars
}
return query;
}

@Override
public BoolQueryBuilder getBuilderPrototype() {
return BoolQueryBuilder.PROTOTYPE;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,9 @@ public class BoostingQueryBuilder extends QueryBuilder implements BoostableQuery

private float boost = -1;

public BoostingQueryBuilder() {
static final BoostingQueryBuilder PROTOTYPE = new BoostingQueryBuilder();

public BoostingQueryBuilder() {
}

public BoostingQueryBuilder positive(QueryBuilder positiveQuery) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,4 +98,9 @@ public Query parse(QueryParseContext parseContext) throws IOException, QueryPars
}
return boostingQuery;
}

@Override
public BoostingQueryBuilder getBuilderPrototype() {
return BoostingQueryBuilder.PROTOTYPE;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@ public static enum Operator {

private String queryName;

static final CommonTermsQueryBuilder PROTOTYPE = new CommonTermsQueryBuilder();

/**
* Constructs a new common terms query.
*/
Expand All @@ -86,6 +88,14 @@ public CommonTermsQueryBuilder(String name, Object text) {
this.name = name;
}

/**
* private constructor used onyl internally
*/
private CommonTermsQueryBuilder() {
this.text = null;
this.name = null;
}

/**
* Sets the operator to use for terms with a high document frequency
* (greater than or equal to {@link #cutoffFrequency(float)}. Defaults to
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -219,4 +219,9 @@ private final Query parseQueryString(ExtendedCommonTermsQuery query, String quer
query.setHighFreqMinimumNumberShouldMatch(highFreqMinimumShouldMatch);
return query;
}

@Override
public CommonTermsQueryBuilder getBuilderPrototype() {
return CommonTermsQueryBuilder.PROTOTYPE;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ public class ConstantScoreQueryBuilder extends QueryBuilder implements Boostable

private float boost = -1;

static final ConstantScoreQueryBuilder PROTOTYPE = new ConstantScoreQueryBuilder();

/**
* A query that wraps a query and simply returns a constant score equal to the
* query boost for every document in the query.
Expand All @@ -47,6 +49,13 @@ public ConstantScoreQueryBuilder(QueryBuilder filterBuilder) {
this.filterBuilder = Objects.requireNonNull(filterBuilder);
}

/**
* private constructor only used for serialization
*/
private ConstantScoreQueryBuilder() {
this.filterBuilder = null;
}

/**
* Sets the boost for this query. Documents matching this query will (in addition to the normal
* weightings) have their score multiplied by the boost provided.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,4 +86,9 @@ public Query parse(QueryParseContext parseContext) throws IOException, QueryPars
filter.setBoost(boost);
return filter;
}

@Override
public ConstantScoreQueryBuilder getBuilderPrototype() {
return ConstantScoreQueryBuilder.PROTOTYPE;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ public class DisMaxQueryBuilder extends QueryBuilder implements BoostableQueryBu

private String queryName;

static final DisMaxQueryBuilder PROTOTYPE = new DisMaxQueryBuilder();

/**
* Add a sub-query to this disjunction.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,4 +111,9 @@ public Query parse(QueryParseContext parseContext) throws IOException, QueryPars
}
return query;
}

@Override
public DisMaxQueryBuilder getBuilderPrototype() {
return DisMaxQueryBuilder.PROTOTYPE;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ public class ExistsQueryBuilder extends QueryBuilder {

private String queryName;

static final ExistsQueryBuilder PROTOTYPE = new ExistsQueryBuilder(null);

public ExistsQueryBuilder(String name) {
this.name = name;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,4 +122,9 @@ public static Query newFilter(QueryParseContext parseContext, String fieldPatter
return new ConstantScoreQuery(boolFilter);
}

@Override
public ExistsQueryBuilder getBuilderPrototype() {
return ExistsQueryBuilder.PROTOTYPE;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -84,4 +84,9 @@ public Query parse(QueryParseContext parseContext) throws IOException, QueryPars
}
return query;
}

@Override
public QueryFilterBuilder getBuilderPrototype() {
return QueryFilterBuilder.PROTOTYPE;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ public class FieldMaskingSpanQueryBuilder extends QueryBuilder implements SpanQu

private String queryName;

static final FieldMaskingSpanQueryBuilder PROTOTYPE = new FieldMaskingSpanQueryBuilder(null, null);

public FieldMaskingSpanQueryBuilder(SpanQueryBuilder queryBuilder, String field) {
this.queryBuilder = queryBuilder;
this.field = field;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,4 +100,9 @@ public Query parse(QueryParseContext parseContext) throws IOException, QueryPars
}
return query;
}

@Override
public FieldMaskingSpanQueryBuilder getBuilderPrototype() {
return FieldMaskingSpanQueryBuilder.PROTOTYPE;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ public class FilteredQueryBuilder extends QueryBuilder implements BoostableQuery

private String queryName;

static final FilteredQueryBuilder PROTOTYPE = new FilteredQueryBuilder(null, null);

/**
* A query that applies a filter to the results of another query.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,4 +118,9 @@ public Query parse(QueryParseContext parseContext) throws IOException, QueryPars
}
return filteredQuery;
}

@Override
public FilteredQueryBuilder getBuilderPrototype() {
return FilteredQueryBuilder.PROTOTYPE;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ public class FuzzyQueryBuilder extends MultiTermQueryBuilder implements Boostabl

private String queryName;

static final FuzzyQueryBuilder PROTOTYPE = new FuzzyQueryBuilder(null, null);

/**
* Constructs a new term query.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,4 +130,9 @@ public Query parse(QueryParseContext parseContext) throws IOException, QueryPars
}
return query;
}

@Override
public FuzzyQueryBuilder getBuilderPrototype() {
return FuzzyQueryBuilder.PROTOTYPE;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ public class GeoBoundingBoxQueryBuilder extends QueryBuilder {
private String queryName;
private String type;

static final GeoBoundingBoxQueryBuilder PROTOTYPE = new GeoBoundingBoxQueryBuilder(null);

public GeoBoundingBoxQueryBuilder(String name) {
this.name = name;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,14 +74,14 @@ public Query parse(QueryParseContext parseContext) throws IOException, QueryPars
double bottom = Double.NaN;
double left = Double.NaN;
double right = Double.NaN;

String queryName = null;
String currentFieldName = null;
XContentParser.Token token;
boolean normalize = true;

GeoPoint sparse = new GeoPoint();

String type = "memory";

while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
Expand Down Expand Up @@ -148,7 +148,7 @@ public Query parse(QueryParseContext parseContext) throws IOException, QueryPars
final GeoPoint bottomRight = new GeoPoint(bottom, right);

if (normalize) {
// Special case: if the difference bettween the left and right is 360 and the right is greater than the left, we are asking for
// Special case: if the difference bettween the left and right is 360 and the right is greater than the left, we are asking for
// the complete longitude range so need to set longitude to the complete longditude range
boolean completeLonRange = ((right - left) % 360 == 0 && right > left);
GeoUtils.normalizePoint(topLeft, true, !completeLonRange);
Expand Down Expand Up @@ -183,5 +183,10 @@ public Query parse(QueryParseContext parseContext) throws IOException, QueryPars
parseContext.addNamedQuery(queryName, filter);
}
return filter;
}
}

@Override
public GeoBoundingBoxQueryBuilder getBuilderPrototype() {
return GeoBoundingBoxQueryBuilder.PROTOTYPE;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ public class GeoDistanceQueryBuilder extends QueryBuilder {

private String queryName;

static final GeoDistanceQueryBuilder PROTOTYPE = new GeoDistanceQueryBuilder(null);

public GeoDistanceQueryBuilder(String name) {
this.name = name;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -162,4 +162,9 @@ public Query parse(QueryParseContext parseContext) throws IOException, QueryPars
}
return query;
}

@Override
public GeoDistanceQueryBuilder getBuilderPrototype() {
return GeoDistanceQueryBuilder.PROTOTYPE;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ public class GeoDistanceRangeQueryBuilder extends QueryBuilder {

private String optimizeBbox;

static final GeoDistanceRangeQueryBuilder PROTOTYPE = new GeoDistanceRangeQueryBuilder(null);

public GeoDistanceRangeQueryBuilder(String name) {
this.name = name;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -201,4 +201,9 @@ public Query parse(QueryParseContext parseContext) throws IOException, QueryPars
}
return query;
}

@Override
public GeoDistanceRangeQueryBuilder getBuilderPrototype() {
return GeoDistanceRangeQueryBuilder.PROTOTYPE;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ public class GeoPolygonQueryBuilder extends QueryBuilder {

private String queryName;

static final GeoPolygonQueryBuilder PROTOTYPE = new GeoPolygonQueryBuilder(null);

public GeoPolygonQueryBuilder(String name) {
this.name = name;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -150,4 +150,9 @@ public Query parse(QueryParseContext parseContext) throws IOException, QueryPars
}
return query;
}

@Override
public GeoPolygonQueryBuilder getBuilderPrototype() {
return GeoPolygonQueryBuilder.PROTOTYPE;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ public class GeoShapeQueryBuilder extends QueryBuilder {

private ShapeRelation relation = null;

static final GeoShapeQueryBuilder PROTOTYPE = new GeoShapeQueryBuilder(null, null);

/**
* Creates a new GeoShapeQueryBuilder whose Filter will be against the
* given field name using the given Shape
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -189,4 +189,9 @@ public static SpatialArgs getArgs(ShapeBuilder shape, ShapeRelation relation) {
throw new IllegalArgumentException("");
}
}

@Override
public GeoShapeQueryBuilder getBuilderPrototype() {
return GeoShapeQueryBuilder.PROTOTYPE;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ public static class Builder extends QueryBuilder {
private String geohash;
private int levels = -1;
private boolean neighbors;
private static final Builder PROTOTYPE = new Builder(null);


public Builder(String field) {
Expand Down Expand Up @@ -270,5 +271,10 @@ public Query parse(QueryParseContext parseContext) throws IOException, QueryPars

return filter;
}

@Override
public GeohashCellQuery.Builder getBuilderPrototype() {
return Builder.PROTOTYPE;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ public class HasChildQueryBuilder extends QueryBuilder implements BoostableQuery

private QueryInnerHitBuilder innerHit = null;

static final HasChildQueryBuilder PROTOTYPE = new HasChildQueryBuilder(null, null);

public HasChildQueryBuilder(String type, QueryBuilder queryBuilder) {
this.childType = type;
this.queryBuilder = queryBuilder;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -187,4 +187,9 @@ public Query parse(QueryParseContext parseContext) throws IOException, QueryPars
query.setBoost(boost);
return query;
}

@Override
public HasChildQueryBuilder getBuilderPrototype() {
return HasChildQueryBuilder.PROTOTYPE;
}
}
Loading

0 comments on commit 1e93997

Please sign in to comment.