Skip to content

Commit a4a4f19

Browse files
author
Christoph Büscher
committed
Query refactoring: Introduce toQuery() and fromXContent() in QueryBuilders and QueryParsers
The planed refactoring of search queries layed out in #9901 requires to split the "parse()" method in QueryParsers into two methods, first a "fromXContent(...)" method that allows parsing to an intermediate query representation (currently called FooQueryBuilder) and second a "Query toQuery(...)" method on these intermediate representations that create the actual lucene queries. This PR is a first step in that direction as it introduces the interface changes necessary for the further refactoring. It introduces the new interface methods while for now keeping the old Builder/Parsers still in place by delegating the new "toQuery()" implementations to the existing "parse()" methods, and by introducing a "catch-all" "fromXContent()" implementation in a BaseQueryParser that returns a temporary QueryBuilder wrapper implementation. This allows us to refactor the existing QueryBuilders step by step while already beeing able to start refactoring queries with nested inner queries. Closes #10580
1 parent fdc7db7 commit a4a4f19

File tree

81 files changed

+471
-84
lines changed

Some content is hidden

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

81 files changed

+471
-84
lines changed

src/main/java/org/elasticsearch/index/query/BaseQueryBuilder.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
package org.elasticsearch.index.query;
2121

22+
import org.apache.lucene.search.Query;
2223
import org.elasticsearch.ElasticsearchException;
2324
import org.elasticsearch.common.bytes.BytesReference;
2425
import org.elasticsearch.common.xcontent.XContentBuilder;
@@ -68,5 +69,14 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws
6869
return builder;
6970
}
7071

72+
public Query toQuery(QueryParseContext parseContext) throws QueryParsingException, IOException {
73+
return parseContext.indexQueryParserService().queryParser(parserName()).parse(parseContext);
74+
}
75+
76+
/**
77+
* @return the name of the parser class the default toQuery() method delegates to
78+
*/
79+
protected abstract String parserName();
80+
7181
protected abstract void doXContent(XContentBuilder builder, Params params) throws IOException;
7282
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*
2+
* Licensed to Elasticsearch under one or more contributor
3+
* license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright
5+
* ownership. Elasticsearch licenses this file to you under
6+
* the Apache License, Version 2.0 (the "License"); you may
7+
* not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
package org.elasticsearch.index.query;
21+
22+
import org.apache.lucene.search.Query;
23+
24+
import java.io.IOException;
25+
26+
/**
27+
* Class used during the query parsers refactoring.
28+
* All query parsers that have a refactored "fromXContent" method can be changed to extend this instead of {@link BaseQueryParserTemp}.
29+
* Keeps old {@link QueryParser#parse(QueryParseContext)} method as a stub delegating to
30+
* {@link QueryParser#fromXContent(QueryParseContext)} and {@link QueryBuilder#toQuery(QueryParseContext)}}
31+
*/
32+
public abstract class BaseQueryParser implements QueryParser {
33+
34+
public final Query parse(QueryParseContext parseContext) throws IOException, QueryParsingException {
35+
return fromXContent(parseContext).toQuery(parseContext);
36+
}
37+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*
2+
* Licensed to Elasticsearch under one or more contributor
3+
* license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright
5+
* ownership. Elasticsearch licenses this file to you under
6+
* the Apache License, Version 2.0 (the "License"); you may
7+
* not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
package org.elasticsearch.index.query;
21+
22+
import org.apache.lucene.search.Query;
23+
24+
import java.io.IOException;
25+
26+
/**
27+
* This class with method impl is an intermediate step in the refactoring.
28+
* Method should be should be overwritten for all queries that already implement the toQuery/fromXContent split correctly.
29+
* To be removed once all queries are refactored.
30+
*/
31+
public abstract class BaseQueryParserTemp implements QueryParser {
32+
33+
public QueryBuilder fromXContent(QueryParseContext parseContext) throws IOException, QueryParsingException {
34+
Query query = parse(parseContext);
35+
return new QueryWrappingQueryBuilder(query);
36+
}
37+
}

src/main/java/org/elasticsearch/index/query/BoolQueryBuilder.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public class BoolQueryBuilder extends BaseQueryBuilder implements BoostableQuery
4242
private Boolean disableCoord;
4343

4444
private String minimumShouldMatch;
45-
45+
4646
private Boolean adjustPureNegative;
4747

4848
private String queryName;
@@ -126,7 +126,7 @@ public BoolQueryBuilder minimumShouldMatch(String minimumShouldMatch) {
126126
public boolean hasClauses() {
127127
return !(mustClauses.isEmpty() && shouldClauses.isEmpty() && mustNotClauses.isEmpty());
128128
}
129-
129+
130130
/**
131131
* If a boolean query contains only negative ("must not") clauses should the
132132
* BooleanQuery be enhanced with a {@link MatchAllDocsQuery} in order to act
@@ -185,4 +185,7 @@ private void doXArrayContent(String field, List<QueryBuilder> clauses, XContentB
185185
}
186186
}
187187

188+
final protected String parserName() {
189+
return BoolQueryParser.NAME;
190+
}
188191
}

src/main/java/org/elasticsearch/index/query/BoolQueryParser.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
/**
3838
*
3939
*/
40-
public class BoolQueryParser implements QueryParser {
40+
public class BoolQueryParser extends BaseQueryParserTemp {
4141

4242
public static final String NAME = "bool";
4343

@@ -62,7 +62,7 @@ public Query parse(QueryParseContext parseContext) throws IOException, QueryPars
6262
List<BooleanClause> clauses = newArrayList();
6363
boolean adjustPureNegative = true;
6464
String queryName = null;
65-
65+
6666
String currentFieldName = null;
6767
XContentParser.Token token;
6868
while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {

src/main/java/org/elasticsearch/index/query/BoostingQueryBuilder.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,4 +95,8 @@ protected void doXContent(XContentBuilder builder, Params params) throws IOExcep
9595
}
9696
builder.endObject();
9797
}
98+
99+
final protected String parserName() {
100+
return BoostingQueryParser.NAME;
101+
}
98102
}

src/main/java/org/elasticsearch/index/query/BoostingQueryParser.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
/**
3030
*
3131
*/
32-
public class BoostingQueryParser implements QueryParser {
32+
public class BoostingQueryParser extends BaseQueryParserTemp {
3333

3434
public static final String NAME = "boosting";
3535

src/main/java/org/elasticsearch/index/query/CommonTermsQueryBuilder.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@
1919

2020
package org.elasticsearch.index.query;
2121

22+
import org.apache.lucene.index.Term;
23+
import org.apache.lucene.search.BooleanQuery;
24+
import org.apache.lucene.search.similarities.Similarity;
2225
import org.elasticsearch.ElasticsearchIllegalArgumentException;
2326
import org.elasticsearch.common.xcontent.XContentBuilder;
2427

@@ -198,4 +201,8 @@ public void doXContent(XContentBuilder builder, Params params) throws IOExceptio
198201
builder.endObject();
199202
builder.endObject();
200203
}
204+
205+
final protected String parserName() {
206+
return CommonTermsQueryParser.NAME;
207+
}
201208
}

src/main/java/org/elasticsearch/index/query/CommonTermsQueryParser.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
/**
4040
*
4141
*/
42-
public class CommonTermsQueryParser implements QueryParser {
42+
public class CommonTermsQueryParser extends BaseQueryParserTemp {
4343

4444
public static final String NAME = "common";
4545

src/main/java/org/elasticsearch/index/query/ConstantScoreQueryBuilder.java

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
package org.elasticsearch.index.query;
2121

22+
import org.apache.lucene.search.Query;
2223
import org.elasticsearch.common.xcontent.XContentBuilder;
2324

2425
import java.io.IOException;
@@ -35,7 +36,7 @@ public class ConstantScoreQueryBuilder extends BaseQueryBuilder implements Boost
3536
private final QueryBuilder queryBuilder;
3637

3738
private float boost = -1;
38-
39+
3940

4041
/**
4142
* A query that wraps a filter and simply returns a constant score equal to the
@@ -56,7 +57,7 @@ public ConstantScoreQueryBuilder(FilterBuilder filterBuilder) {
5657
public ConstantScoreQueryBuilder(QueryBuilder queryBuilder) {
5758
this.filterBuilder = null;
5859
this.queryBuilder = queryBuilder;
59-
}
60+
}
6061

6162
/**
6263
* Sets the boost for this query. Documents matching this query will (in addition to the normal
@@ -77,12 +78,16 @@ protected void doXContent(XContentBuilder builder, Params params) throws IOExcep
7778
queryBuilder.toXContent(builder, params);
7879
} else {
7980
builder.field("filter");
80-
filterBuilder.toXContent(builder, params);
81+
filterBuilder.toXContent(builder, params);
8182
}
82-
83+
8384
if (boost != -1) {
8485
builder.field("boost", boost);
8586
}
8687
builder.endObject();
8788
}
88-
}
89+
90+
final protected String parserName() {
91+
return ConstantScoreQueryParser.NAME;
92+
}
93+
}

0 commit comments

Comments
 (0)