Skip to content

Commit 63c51b7

Browse files
committed
review comment fixes
1 parent c618f75 commit 63c51b7

File tree

13 files changed

+79
-59
lines changed

13 files changed

+79
-59
lines changed

core/src/main/java/org/elasticsearch/action/search/SearchRequest.java

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -100,14 +100,15 @@ public SearchRequest(SearchRequest searchRequest, ActionRequest originalRequest)
100100
*/
101101
public SearchRequest(ActionRequest request) {
102102
super(request);
103+
this.source = new SearchSourceBuilder();
103104
}
104105

105106
/**
106107
* Constructs a new search request against the indices. No indices provided here means that search
107108
* will run against all indices.
108109
*/
109110
public SearchRequest(String... indices) {
110-
indices(indices);
111+
this(indices, new SearchSourceBuilder());
111112
}
112113

113114
/**
@@ -331,9 +332,7 @@ public void readFrom(StreamInput in) throws IOException {
331332
if (in.readBoolean()) {
332333
scroll = readScroll(in);
333334
}
334-
if (in.readBoolean()) {
335-
source = SearchSourceBuilder.readSearchSourceFrom(in);
336-
}
335+
source = SearchSourceBuilder.readSearchSourceFrom(in);
337336

338337
types = in.readStringArray();
339338
indicesOptions = IndicesOptions.readIndicesOptions(in);
@@ -361,12 +360,7 @@ public void writeTo(StreamOutput out) throws IOException {
361360
out.writeBoolean(true);
362361
scroll.writeTo(out);
363362
}
364-
if (source == null) {
365-
out.writeBoolean(false);
366-
} else {
367-
out.writeBoolean(true);
368-
source.writeTo(out);
369-
}
363+
source.writeTo(out);
370364
out.writeStringArray(types);
371365
indicesOptions.writeIndicesOptions(out);
372366
out.writeOptionalBoolean(requestCache);

core/src/main/java/org/elasticsearch/action/search/SearchRequestBuilder.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -353,10 +353,10 @@ public SearchRequestBuilder setTrackScores(boolean trackScores) {
353353
}
354354

355355
/**
356-
* Adds the fields to load and return as part of the search request. If none are specified,
357-
* the source of the document will be returned.
356+
* Sets the fields to load and return as part of the search request. If none
357+
* are specified, the source of the document will be returned.
358358
*/
359-
public SearchRequestBuilder addFields(String... fields) {
359+
public SearchRequestBuilder fields(String... fields) {
360360
sourceBuilder().fields(Arrays.asList(fields));
361361
return this;
362362
}

core/src/main/java/org/elasticsearch/rest/action/admin/indices/warmer/put/RestPutWarmerAction.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ public void handleRequest(final RestRequest request, final RestChannel channel,
7272
PutWarmerRequest putWarmerRequest = new PutWarmerRequest(request.param("name"));
7373

7474
BytesReference sourceBytes = RestActions.getRestContent(request);
75-
SearchSourceBuilder source = RestActions.getRestSearchSource(sourceBytes, queryRegistry);
75+
SearchSourceBuilder source = RestActions.getRestSearchSource(sourceBytes, queryRegistry, parseFieldMatcher);
7676
SearchRequest searchRequest = new SearchRequest(Strings.splitStringByCommaToArray(request.param("index")))
7777
.types(Strings.splitStringByCommaToArray(request.param("type")))
7878
.requestCache(request.paramAsBoolean("request_cache", null)).source(source);

core/src/main/java/org/elasticsearch/rest/action/cat/RestCountAction.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ public void doRequest(final RestRequest request, final RestChannel channel, fina
7070
String source = request.param("source");
7171
if (source != null) {
7272
QueryParseContext context = new QueryParseContext(indicesQueriesRegistry);
73+
context.parseFieldMatcher(parseFieldMatcher);
7374
countRequest.query(RestActions.getQueryContent(new BytesArray(source), context));
7475
} else {
7576
QueryBuilder<?> queryBuilder = RestActions.urlParamsToQueryBuilder(request);

core/src/main/java/org/elasticsearch/rest/action/count/RestCountAction.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ public void handleRequest(final RestRequest request, final RestChannel channel,
7171
if (RestActions.hasBodyContent(request)) {
7272
BytesReference restContent = RestActions.getRestContent(request);
7373
QueryParseContext context = new QueryParseContext(indicesQueriesRegistry);
74+
context.parseFieldMatcher(parseFieldMatcher);
7475
countRequest.query(RestActions.getQueryContent(restContent, context));
7576
} else {
7677
QueryBuilder<?> queryBuilder = RestActions.urlParamsToQueryBuilder(request);

core/src/main/java/org/elasticsearch/rest/action/search/RestMultiSearchAction.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import org.elasticsearch.action.support.IndicesOptions;
2525
import org.elasticsearch.client.Client;
2626
import org.elasticsearch.common.Nullable;
27+
import org.elasticsearch.common.ParseFieldMatcher;
2728
import org.elasticsearch.common.Strings;
2829
import org.elasticsearch.common.bytes.BytesReference;
2930
import org.elasticsearch.common.inject.Inject;
@@ -90,7 +91,9 @@ public void handleRequest(final RestRequest request, final RestChannel channel,
9091
String path = request.path();
9192
boolean isTemplateRequest = isTemplateRequest(path);
9293
IndicesOptions indicesOptions = IndicesOptions.fromRequest(request, multiSearchRequest.indicesOptions());
93-
parseRequest(multiSearchRequest, RestActions.getRestContent(request), isTemplateRequest, indices, types, request.param("search_type"), request.param("routing"), indicesOptions, allowExplicitIndex, indicesQueriesRegistry);
94+
parseRequest(multiSearchRequest, RestActions.getRestContent(request), isTemplateRequest, indices, types,
95+
request.param("search_type"), request.param("routing"), indicesOptions, allowExplicitIndex, indicesQueriesRegistry,
96+
parseFieldMatcher);
9497
client.multiSearch(multiSearchRequest, new RestToXContentListener<>(channel));
9598
}
9699

@@ -104,7 +107,8 @@ public static MultiSearchRequest parseRequest(MultiSearchRequest msr, BytesRefer
104107
@Nullable String searchType,
105108
@Nullable String routing,
106109
IndicesOptions indicesOptions,
107-
boolean allowExplicitIndex, IndicesQueriesRegistry indicesQueriesRegistry) throws Exception {
110+
boolean allowExplicitIndex, IndicesQueriesRegistry indicesQueriesRegistry,
111+
ParseFieldMatcher parseFieldMatcher) throws Exception {
108112
XContent xContent = XContentFactory.xContent(data);
109113
int from = 0;
110114
int length = data.length();
@@ -178,6 +182,7 @@ public static MultiSearchRequest parseRequest(MultiSearchRequest msr, BytesRefer
178182
if (isTemplateRequest) {
179183
try (XContentParser parser = XContentFactory.xContent(slice).createParser(slice)) {
180184
queryParseContext.reset(parser);
185+
queryParseContext.parseFieldMatcher(parseFieldMatcher);
181186
Template template = TemplateQueryParser.parse(parser, queryParseContext.parseFieldMatcher(), "params", "template");
182187
searchRequest.template(template);
183188
}

core/src/main/java/org/elasticsearch/rest/action/search/RestSearchAction.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,12 +110,13 @@ public static SearchRequest parseSearchRequest(IndicesQueriesRegistry indicesQue
110110
if (isTemplateRequest) {
111111
try (XContentParser parser = XContentFactory.xContent(restContent).createParser(restContent)) {
112112
context.reset(parser);
113+
context.parseFieldMatcher(parseFieldMatcher);
113114
Template template = TemplateQueryParser.parse(parser, context.parseFieldMatcher(), "params", "template");
114115
searchRequest.template(template);
115116
}
116117
builder = null;
117118
} else {
118-
builder = RestActions.getRestSearchSource(restContent, indicesQueriesRegistry);
119+
builder = RestActions.getRestSearchSource(restContent, indicesQueriesRegistry, parseFieldMatcher);
119120
}
120121
} else {
121122
builder = null;
@@ -155,7 +156,7 @@ public static SearchRequest parseSearchRequest(IndicesQueriesRegistry indicesQue
155156
return searchRequest;
156157
}
157158

158-
public static boolean parseSearchSource(final SearchSourceBuilder searchSourceBuilder, RestRequest request) {
159+
private static boolean parseSearchSource(final SearchSourceBuilder searchSourceBuilder, RestRequest request) {
159160

160161
boolean modified = false;
161162
QueryBuilder<?> queryBuilder = RestActions.urlParamsToQueryBuilder(request);

core/src/main/java/org/elasticsearch/rest/action/support/RestActions.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import org.elasticsearch.ExceptionsHelper;
2424
import org.elasticsearch.action.ShardOperationFailedException;
2525
import org.elasticsearch.action.support.broadcast.BroadcastResponse;
26+
import org.elasticsearch.common.ParseFieldMatcher;
2627
import org.elasticsearch.common.bytes.BytesArray;
2728
import org.elasticsearch.common.bytes.BytesReference;
2829
import org.elasticsearch.common.lucene.uid.Versions;
@@ -112,11 +113,13 @@ public static QueryBuilder<?> urlParamsToQueryBuilder(RestRequest request) {
112113
return queryBuilder;
113114
}
114115

115-
public static SearchSourceBuilder getRestSearchSource(BytesReference sourceBytes, IndicesQueriesRegistry queryRegistry)
116+
public static SearchSourceBuilder getRestSearchSource(BytesReference sourceBytes, IndicesQueriesRegistry queryRegistry,
117+
ParseFieldMatcher parseFieldMatcher)
116118
throws IOException {
117119
XContentParser parser = XContentFactory.xContent(sourceBytes).createParser(sourceBytes);
118120
QueryParseContext queryParseContext = new QueryParseContext(queryRegistry);
119121
queryParseContext.reset(parser);
122+
queryParseContext.parseFieldMatcher(parseFieldMatcher);
120123
SearchSourceBuilder source = SearchSourceBuilder.parseSearchSource(parser, queryParseContext);
121124
return source;
122125
}
@@ -142,7 +145,11 @@ public static BytesReference getRestContent(RestRequest request) {
142145

143146
public static QueryBuilder<?> getQueryContent(BytesReference source, QueryParseContext context) {
144147
try (XContentParser requestParser = XContentFactory.xContent(source).createParser(source)) {
148+
// Save the parseFieldMatcher because its about to be trashed in the
149+
// QueryParseContext
150+
ParseFieldMatcher parseFieldMatcher = context.parseFieldMatcher();
145151
context.reset(requestParser);
152+
context.parseFieldMatcher(parseFieldMatcher);
146153
return context.parseInnerQueryBuilder();
147154
} catch (IOException e) {
148155
throw new ElasticsearchException("failed to parse source", e);

core/src/main/java/org/elasticsearch/search/SearchService.java

Lines changed: 31 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -578,6 +578,7 @@ final SearchContext createContext(ShardSearchRequest request, @Nullable Engine.S
578578
try (XContentParser parser = XContentFactory.xContent(run).createParser(run)) {
579579
QueryParseContext queryParseContext = new QueryParseContext(indexService.queryParserService().indicesQueriesRegistry());
580580
queryParseContext.reset(parser);
581+
queryParseContext.parseFieldMatcher(parseFieldMatcher);
581582
parseSource(context, SearchSourceBuilder.parseSearchSource(parser, queryParseContext));
582583
}
583584
}
@@ -673,8 +674,8 @@ private void processFailure(SearchContext context, Throwable t) {
673674
private void parseSource(SearchContext context, SearchSourceBuilder source) throws SearchParseException {
674675
// nothing to parse...
675676
if (source == null) {
676-
return;
677-
}
677+
return;
678+
}
678679

679680
context.from(source.from());
680681
context.size(source.size());
@@ -686,10 +687,10 @@ private void parseSource(SearchContext context, SearchSourceBuilder source) thro
686687
}
687688
}
688689
if (source.query() != null) {
689-
context.parsedQuery(context.queryParserService().parse(source.query()));
690+
context.parsedQuery(context.queryParserService().toQuery(source.query()));
690691
}
691692
if (source.postFilter() != null) {
692-
context.parsedPostFilter(context.queryParserService().parse(source.postFilter()));
693+
context.parsedPostFilter(context.queryParserService().toQuery(source.postFilter()));
693694
}
694695
if (source.sorts() != null) {
695696
XContentParser completeSortParser = null;
@@ -775,15 +776,15 @@ private void parseSource(SearchContext context, SearchSourceBuilder source) thro
775776
}
776777
if (source.rescores() != null) {
777778
XContentParser completeRescoreParser = null;
778-
try {
779+
try {
779780
XContentBuilder completeRescoreBuilder = XContentFactory.jsonBuilder();
780781
completeRescoreBuilder.startObject();
781782
completeRescoreBuilder.startArray("rescore");
782783
for (BytesReference rescore : source.rescores()) {
783784
XContentParser parser = XContentFactory.xContent(rescore).createParser(rescore);
784785
parser.nextToken();
785786
completeRescoreBuilder.copyCurrentStructure(parser);
786-
}
787+
}
787788
completeRescoreBuilder.endArray();
788789
completeRescoreBuilder.endObject();
789790
BytesReference completeRescoreBytes = completeRescoreBuilder.bytes();
@@ -794,31 +795,31 @@ private void parseSource(SearchContext context, SearchSourceBuilder source) thro
794795
this.elementParsers.get("rescore").parse(completeRescoreParser, context);
795796
} catch (Exception e) {
796797
String sSource = "_na_";
797-
try {
798+
try {
798799
sSource = source.toString();
799800
} catch (Throwable e1) {
800801
// ignore
801802
}
802803
XContentLocation location = completeRescoreParser != null ? completeRescoreParser.getTokenLocation() : null;
803804
throw new SearchParseException(context, "failed to parse rescore source [" + sSource + "]", location, e);
804805
}
805-
}
806+
}
806807
if (source.fields() != null) {
807808
context.fieldNames().addAll(source.fields());
808-
}
809+
}
809810
if (source.explain() != null) {
810811
context.explain(source.explain());
811-
}
812+
}
812813
if (source.fetchSource() != null) {
813814
context.fetchSourceContext(source.fetchSource());
814-
}
815+
}
815816
if (source.fieldDataFields() != null) {
816817
FieldDataFieldsContext fieldDataFieldsContext = context.getFetchSubPhaseContext(FieldDataFieldsFetchSubPhase.CONTEXT_FACTORY);
817818
for (String field : source.fieldDataFields()) {
818819
fieldDataFieldsContext.add(new FieldDataField(field));
819820
}
820821
fieldDataFieldsContext.setHitExecutionNeeded(true);
821-
}
822+
}
822823
if (source.highlighter() != null) {
823824
XContentParser highlighterParser = null;
824825
try {
@@ -830,10 +831,10 @@ private void parseSource(SearchContext context, SearchSourceBuilder source) thro
830831
sSource = source.toString();
831832
} catch (Throwable e1) {
832833
// ignore
833-
}
834+
}
834835
XContentLocation location = highlighterParser != null ? highlighterParser.getTokenLocation() : null;
835836
throw new SearchParseException(context, "failed to parse suggest source [" + sSource + "]", location, e);
836-
}
837+
}
837838
}
838839
if (source.innerHits() != null) {
839840
XContentParser innerHitsParser = null;
@@ -843,7 +844,7 @@ private void parseSource(SearchContext context, SearchSourceBuilder source) thro
843844
this.elementParsers.get("inner_hits").parse(innerHitsParser, context);
844845
} catch (Exception e) {
845846
String sSource = "_na_";
846-
try {
847+
try {
847848
sSource = source.toString();
848849
} catch (Throwable e1) {
849850
// ignore
@@ -857,40 +858,40 @@ private void parseSource(SearchContext context, SearchSourceBuilder source) thro
857858
SearchScript searchScript = context.scriptService().search(context.lookup(), field.script(), ScriptContext.Standard.SEARCH);
858859
context.scriptFields().add(new ScriptField(field.fieldName(), searchScript, field.ignoreFailure()));
859860
}
860-
}
861+
}
861862
if (source.ext() != null) {
862863
XContentParser extParser = null;
863864
try {
864865
extParser = XContentFactory.xContent(source.ext()).createParser(source.ext());
865866
XContentParser.Token token = extParser.nextToken();
866867
String currentFieldName = null;
867868
while ((token = extParser.nextToken()) != XContentParser.Token.END_OBJECT) {
868-
if (token == XContentParser.Token.FIELD_NAME) {
869+
if (token == XContentParser.Token.FIELD_NAME) {
869870
currentFieldName = extParser.currentName();
870-
} else {
871+
} else {
871872
SearchParseElement parseElement = this.elementParsers.get(currentFieldName);
872873
if (parseElement == null) {
873874
throw new SearchParseException(context, "Unknown element [" + currentFieldName + "] in [ext]",
874875
extParser.getTokenLocation());
875-
} else {
876+
} else {
876877
parseElement.parse(extParser, context);
878+
}
877879
}
878880
}
879-
}
880881
} catch (Exception e) {
881-
String sSource = "_na_";
882-
try {
882+
String sSource = "_na_";
883+
try {
883884
sSource = source.toString();
884-
} catch (Throwable e1) {
885-
// ignore
886-
}
885+
} catch (Throwable e1) {
886+
// ignore
887+
}
887888
XContentLocation location = extParser != null ? extParser.getTokenLocation() : null;
888889
throw new SearchParseException(context, "failed to parse ext source [" + sSource + "]", location, e);
889890
}
890891
}
891892
if (source.version() != null) {
892893
context.version(source.version());
893-
}
894+
}
894895
if (source.stats() != null) {
895896
context.groupStats(source.stats());
896897
}
@@ -1187,9 +1188,12 @@ public void run() {
11871188
try {
11881189
long now = System.nanoTime();
11891190
final IndexService indexService = indicesService.indexServiceSafe(indexShard.shardId().index().name());
1191+
QueryParseContext queryParseContext = new QueryParseContext(indexService.queryParserService().indicesQueriesRegistry());
1192+
// NOCOMMIT get a parseFieldMatcher from somewhere and set it on the queryParseContext
1193+
queryParseContext.parseFieldMatcher(indexService.queryParserService().parseFieldMatcher());
11901194
ShardSearchRequest request = new ShardSearchLocalRequest(indexShard.shardId(), indexMetaData
11911195
.getNumberOfShards(),
1192-
SearchType.QUERY_THEN_FETCH, entry.source().build(new QueryParseContext(indexService.queryParserService().indicesQueriesRegistry())), entry.types(), entry.requestCache());
1196+
SearchType.QUERY_THEN_FETCH, entry.source().build(queryParseContext), entry.types(), entry.requestCache());
11931197
context = createContext(request, warmerContext.searcher());
11941198
// if we use sort, we need to do query to sort on
11951199
// it and load relevant field data

0 commit comments

Comments
 (0)