Skip to content

Commit b56bbf6

Browse files
committed
Validate query api: move query parsing on the coordinating node
Similarly to what we did with the search api, we can now also move query parsing on the coordinating node for the validate query api. Given that the explain api is a single shard operation (compared to search which is instead a broadcast operation), this doesn't change a lot in how the api works internally. The main benefit is that we can simplify the java api by requiring a structured query object to be provided rather than a bytes array that will get parsed on the data node. Previously if you specified a QueryBuilder it would be serialized in json format and would get reparsed on the data node, while now it doesn't go through parsing anymore (as expected), given that after the query-refactoring we are able to properly stream queries natively. Note that the WrapperQueryBuilder can be used from the java api to provide a query as a string, in that case the actual parsing of the inner query will happen on the data node. Relates to #10217 Closes #14384
1 parent ebec4bd commit b56bbf6

File tree

10 files changed

+101
-264
lines changed

10 files changed

+101
-264
lines changed

core/src/main/java/org/elasticsearch/action/admin/indices/validate/query/ShardValidateQueryRequest.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,9 @@
2222
import org.elasticsearch.action.support.broadcast.BroadcastShardRequest;
2323
import org.elasticsearch.common.Nullable;
2424
import org.elasticsearch.common.Strings;
25-
import org.elasticsearch.common.bytes.BytesReference;
2625
import org.elasticsearch.common.io.stream.StreamInput;
2726
import org.elasticsearch.common.io.stream.StreamOutput;
27+
import org.elasticsearch.index.query.QueryBuilder;
2828
import org.elasticsearch.index.shard.ShardId;
2929

3030
import java.io.IOException;
@@ -34,7 +34,7 @@
3434
*/
3535
public class ShardValidateQueryRequest extends BroadcastShardRequest {
3636

37-
private BytesReference source;
37+
private QueryBuilder<?> query;
3838
private String[] types = Strings.EMPTY_ARRAY;
3939
private boolean explain;
4040
private boolean rewrite;
@@ -49,16 +49,16 @@ public ShardValidateQueryRequest() {
4949

5050
ShardValidateQueryRequest(ShardId shardId, @Nullable String[] filteringAliases, ValidateQueryRequest request) {
5151
super(shardId, request);
52-
this.source = request.source();
52+
this.query = request.query();
5353
this.types = request.types();
5454
this.explain = request.explain();
5555
this.rewrite = request.rewrite();
5656
this.filteringAliases = filteringAliases;
5757
this.nowInMillis = request.nowInMillis;
5858
}
5959

60-
public BytesReference source() {
61-
return source;
60+
public QueryBuilder<?> query() {
61+
return query;
6262
}
6363

6464
public String[] types() {
@@ -84,7 +84,7 @@ public long nowInMillis() {
8484
@Override
8585
public void readFrom(StreamInput in) throws IOException {
8686
super.readFrom(in);
87-
source = in.readBytesReference();
87+
query = in.readQuery();
8888

8989
int typesSize = in.readVInt();
9090
if (typesSize > 0) {
@@ -109,7 +109,7 @@ public void readFrom(StreamInput in) throws IOException {
109109
@Override
110110
public void writeTo(StreamOutput out) throws IOException {
111111
super.writeTo(out);
112-
out.writeBytesReference(source);
112+
out.writeQuery(query);
113113

114114
out.writeVInt(types.length);
115115
for (String type : types) {

core/src/main/java/org/elasticsearch/action/admin/indices/validate/query/TransportValidateQueryAction.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,14 +36,14 @@
3636
import org.elasticsearch.cluster.metadata.IndexNameExpressionResolver;
3737
import org.elasticsearch.cluster.routing.GroupShardsIterator;
3838
import org.elasticsearch.cluster.routing.ShardRouting;
39+
import org.elasticsearch.common.ParsingException;
3940
import org.elasticsearch.common.inject.Inject;
4041
import org.elasticsearch.common.settings.Settings;
4142
import org.elasticsearch.common.util.BigArrays;
4243
import org.elasticsearch.index.IndexService;
4344
import org.elasticsearch.index.engine.Engine;
4445
import org.elasticsearch.index.query.IndexQueryParserService;
4546
import org.elasticsearch.index.query.QueryShardException;
46-
import org.elasticsearch.common.ParsingException;
4747
import org.elasticsearch.index.shard.IndexShard;
4848
import org.elasticsearch.indices.IndicesService;
4949
import org.elasticsearch.script.ScriptService;
@@ -178,9 +178,7 @@ protected ShardValidateQueryResponse shardOperation(ShardValidateQueryRequest re
178178
);
179179
SearchContext.setCurrent(searchContext);
180180
try {
181-
if (request.source() != null && request.source().length() > 0) {
182-
searchContext.parsedQuery(queryParserService.parseTopLevelQuery(request.source()));
183-
}
181+
searchContext.parsedQuery(queryParserService.toQuery(request.query()));
184182
searchContext.preProcess();
185183

186184
valid = true;

core/src/main/java/org/elasticsearch/action/admin/indices/validate/query/ValidateQueryRequest.java

Lines changed: 16 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -19,34 +19,27 @@
1919

2020
package org.elasticsearch.action.admin.indices.validate.query;
2121

22-
import org.elasticsearch.ElasticsearchGenerationException;
2322
import org.elasticsearch.action.ActionRequestValidationException;
23+
import org.elasticsearch.action.ValidateActions;
2424
import org.elasticsearch.action.support.IndicesOptions;
25-
import org.elasticsearch.action.support.QuerySourceBuilder;
2625
import org.elasticsearch.action.support.broadcast.BroadcastRequest;
27-
import org.elasticsearch.client.Requests;
2826
import org.elasticsearch.common.Strings;
29-
import org.elasticsearch.common.bytes.BytesArray;
30-
import org.elasticsearch.common.bytes.BytesReference;
3127
import org.elasticsearch.common.io.stream.StreamInput;
3228
import org.elasticsearch.common.io.stream.StreamOutput;
33-
import org.elasticsearch.common.xcontent.XContentBuilder;
34-
import org.elasticsearch.common.xcontent.XContentFactory;
35-
import org.elasticsearch.common.xcontent.XContentHelper;
29+
import org.elasticsearch.index.query.MatchAllQueryBuilder;
30+
import org.elasticsearch.index.query.QueryBuilder;
3631

3732
import java.io.IOException;
3833
import java.util.Arrays;
39-
import java.util.Map;
4034

4135
/**
4236
* A request to validate a specific query.
4337
* <p>
44-
* The request requires the query source to be set either using {@link #source(QuerySourceBuilder)},
45-
* or {@link #source(byte[])}.
38+
* The request requires the query to be set using {@link #query(QueryBuilder)}
4639
*/
4740
public class ValidateQueryRequest extends BroadcastRequest<ValidateQueryRequest> {
4841

49-
private BytesReference source;
42+
private QueryBuilder<?> query = new MatchAllQueryBuilder();
5043

5144
private boolean explain;
5245
private boolean rewrite;
@@ -71,67 +64,21 @@ public ValidateQueryRequest(String... indices) {
7164
@Override
7265
public ActionRequestValidationException validate() {
7366
ActionRequestValidationException validationException = super.validate();
74-
return validationException;
75-
}
76-
77-
/**
78-
* The source to execute.
79-
*/
80-
public BytesReference source() {
81-
return source;
82-
}
83-
84-
public ValidateQueryRequest source(QuerySourceBuilder sourceBuilder) {
85-
this.source = sourceBuilder.buildAsBytes(Requests.CONTENT_TYPE);
86-
return this;
87-
}
88-
89-
/**
90-
* The source to execute in the form of a map.
91-
*/
92-
public ValidateQueryRequest source(Map source) {
93-
try {
94-
XContentBuilder builder = XContentFactory.contentBuilder(Requests.CONTENT_TYPE);
95-
builder.map(source);
96-
return source(builder);
97-
} catch (IOException e) {
98-
throw new ElasticsearchGenerationException("Failed to generate [" + source + "]", e);
67+
if (query == null) {
68+
validationException = ValidateActions.addValidationError("query cannot be null", validationException);
9969
}
100-
}
101-
102-
public ValidateQueryRequest source(XContentBuilder builder) {
103-
this.source = builder.bytes();
104-
return this;
105-
}
106-
107-
/**
108-
* The query source to validate. It is preferable to use either {@link #source(byte[])}
109-
* or {@link #source(QuerySourceBuilder)}.
110-
*/
111-
public ValidateQueryRequest source(String source) {
112-
this.source = new BytesArray(source);
113-
return this;
114-
}
115-
116-
/**
117-
* The source to validate.
118-
*/
119-
public ValidateQueryRequest source(byte[] source) {
120-
return source(source, 0, source.length);
70+
return validationException;
12171
}
12272

12373
/**
124-
* The source to validate.
74+
* The query to validate.
12575
*/
126-
public ValidateQueryRequest source(byte[] source, int offset, int length) {
127-
return source(new BytesArray(source, offset, length));
76+
public QueryBuilder<?> query() {
77+
return query;
12878
}
12979

130-
/**
131-
* The source to validate.
132-
*/
133-
public ValidateQueryRequest source(BytesReference source) {
134-
this.source = source;
80+
public ValidateQueryRequest query(QueryBuilder<?> query) {
81+
this.query = query;
13582
return this;
13683
}
13784

@@ -181,45 +128,33 @@ public boolean rewrite() {
181128
@Override
182129
public void readFrom(StreamInput in) throws IOException {
183130
super.readFrom(in);
184-
185-
source = in.readBytesReference();
186-
131+
query = in.readQuery();
187132
int typesSize = in.readVInt();
188133
if (typesSize > 0) {
189134
types = new String[typesSize];
190135
for (int i = 0; i < typesSize; i++) {
191136
types[i] = in.readString();
192137
}
193138
}
194-
195139
explain = in.readBoolean();
196140
rewrite = in.readBoolean();
197141
}
198142

199143
@Override
200144
public void writeTo(StreamOutput out) throws IOException {
201145
super.writeTo(out);
202-
203-
out.writeBytesReference(source);
204-
146+
out.writeQuery(query);
205147
out.writeVInt(types.length);
206148
for (String type : types) {
207149
out.writeString(type);
208150
}
209-
210151
out.writeBoolean(explain);
211152
out.writeBoolean(rewrite);
212153
}
213154

214155
@Override
215156
public String toString() {
216-
String sSource = "_na_";
217-
try {
218-
sSource = XContentHelper.convertToJson(source, false);
219-
} catch (Exception e) {
220-
// ignore
221-
}
222-
return "[" + Arrays.toString(indices) + "]" + Arrays.toString(types) + ", source[" + sSource + "], explain:" + explain +
157+
return "[" + Arrays.toString(indices) + "]" + Arrays.toString(types) + ", query[" + query + "], explain:" + explain +
223158
", rewrite:" + rewrite;
224159
}
225160
}

core/src/main/java/org/elasticsearch/action/admin/indices/validate/query/ValidateQueryRequestBuilder.java

Lines changed: 2 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -19,19 +19,15 @@
1919

2020
package org.elasticsearch.action.admin.indices.validate.query;
2121

22-
import org.elasticsearch.action.support.QuerySourceBuilder;
2322
import org.elasticsearch.action.support.broadcast.BroadcastOperationRequestBuilder;
2423
import org.elasticsearch.client.ElasticsearchClient;
25-
import org.elasticsearch.common.bytes.BytesReference;
2624
import org.elasticsearch.index.query.QueryBuilder;
2725

2826
/**
2927
*
3028
*/
3129
public class ValidateQueryRequestBuilder extends BroadcastOperationRequestBuilder<ValidateQueryRequest, ValidateQueryResponse, ValidateQueryRequestBuilder> {
3230

33-
private QuerySourceBuilder sourceBuilder;
34-
3531
public ValidateQueryRequestBuilder(ElasticsearchClient client, ValidateQueryAction action) {
3632
super(client, action, new ValidateQueryRequest());
3733
}
@@ -45,32 +41,12 @@ public ValidateQueryRequestBuilder setTypes(String... types) {
4541
}
4642

4743
/**
48-
* The query source to validate.
44+
* The query to validate.
4945
*
5046
* @see org.elasticsearch.index.query.QueryBuilders
5147
*/
5248
public ValidateQueryRequestBuilder setQuery(QueryBuilder queryBuilder) {
53-
sourceBuilder().setQuery(queryBuilder);
54-
return this;
55-
}
56-
57-
/**
58-
* The source to validate.
59-
*
60-
* @see org.elasticsearch.index.query.QueryBuilders
61-
*/
62-
public ValidateQueryRequestBuilder setSource(BytesReference source) {
63-
request().source(source);
64-
return this;
65-
}
66-
67-
/**
68-
* The source to validate.
69-
*
70-
* @see org.elasticsearch.index.query.QueryBuilders
71-
*/
72-
public ValidateQueryRequestBuilder setSource(byte[] source) {
73-
request.source(source);
49+
request.query(queryBuilder);
7450
return this;
7551
}
7652

@@ -91,19 +67,4 @@ public ValidateQueryRequestBuilder setRewrite(boolean rewrite) {
9167
request.rewrite(rewrite);
9268
return this;
9369
}
94-
95-
@Override
96-
protected ValidateQueryRequest beforeExecute(ValidateQueryRequest request) {
97-
if (sourceBuilder != null) {
98-
request.source(sourceBuilder);
99-
}
100-
return request;
101-
}
102-
103-
private QuerySourceBuilder sourceBuilder() {
104-
if (sourceBuilder == null) {
105-
sourceBuilder = new QuerySourceBuilder();
106-
}
107-
return sourceBuilder;
108-
}
10970
}

core/src/main/java/org/elasticsearch/action/support/QuerySourceBuilder.java

Lines changed: 0 additions & 67 deletions
This file was deleted.

0 commit comments

Comments
 (0)