Skip to content

Commit 9b0a097

Browse files
jimczialbertzaharovits
authored andcommitted
Move Retriever Handling to Rewrite Phase (#110641)
This change moves the handling of the retriever to the rewrite phase. It also adds validation of the search source builder after extracting the retriever into the source builder. Relates #110482
1 parent 5be41d8 commit 9b0a097

File tree

11 files changed

+407
-204
lines changed

11 files changed

+407
-204
lines changed

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

Lines changed: 4 additions & 127 deletions
Original file line numberDiff line numberDiff line change
@@ -17,20 +17,16 @@
1717
import org.elasticsearch.common.Strings;
1818
import org.elasticsearch.common.io.stream.StreamInput;
1919
import org.elasticsearch.common.io.stream.StreamOutput;
20-
import org.elasticsearch.common.util.CollectionUtils;
2120
import org.elasticsearch.core.Nullable;
2221
import org.elasticsearch.core.TimeValue;
2322
import org.elasticsearch.index.mapper.SourceLoader;
2423
import org.elasticsearch.index.query.QueryRewriteContext;
2524
import org.elasticsearch.index.query.Rewriteable;
2625
import org.elasticsearch.search.Scroll;
27-
import org.elasticsearch.search.SearchService;
2826
import org.elasticsearch.search.builder.PointInTimeBuilder;
2927
import org.elasticsearch.search.builder.SearchSourceBuilder;
3028
import org.elasticsearch.search.internal.SearchContext;
31-
import org.elasticsearch.search.rescore.RescorerBuilder;
3229
import org.elasticsearch.search.sort.FieldSortBuilder;
33-
import org.elasticsearch.search.sort.ShardDocSortField;
3430
import org.elasticsearch.search.sort.SortBuilder;
3531
import org.elasticsearch.search.sort.SortBuilders;
3632
import org.elasticsearch.tasks.TaskId;
@@ -324,124 +320,15 @@ public void writeTo(StreamOutput out) throws IOException {
324320
public ActionRequestValidationException validate() {
325321
ActionRequestValidationException validationException = null;
326322
boolean scroll = scroll() != null;
323+
324+
if (source != null) {
325+
validationException = source.validate(validationException, scroll);
326+
}
327327
if (scroll) {
328-
if (source != null) {
329-
if (source.trackTotalHitsUpTo() != null && source.trackTotalHitsUpTo() != SearchContext.TRACK_TOTAL_HITS_ACCURATE) {
330-
validationException = addValidationError(
331-
"disabling [track_total_hits] is not allowed in a scroll context",
332-
validationException
333-
);
334-
}
335-
if (source.from() > 0) {
336-
validationException = addValidationError("using [from] is not allowed in a scroll context", validationException);
337-
}
338-
if (source.size() == 0) {
339-
validationException = addValidationError("[size] cannot be [0] in a scroll context", validationException);
340-
}
341-
if (source.rescores() != null && source.rescores().isEmpty() == false) {
342-
validationException = addValidationError("using [rescore] is not allowed in a scroll context", validationException);
343-
}
344-
if (CollectionUtils.isEmpty(source.searchAfter()) == false) {
345-
validationException = addValidationError("[search_after] cannot be used in a scroll context", validationException);
346-
}
347-
if (source.collapse() != null) {
348-
validationException = addValidationError("cannot use `collapse` in a scroll context", validationException);
349-
}
350-
}
351328
if (requestCache != null && requestCache) {
352329
validationException = addValidationError("[request_cache] cannot be used in a scroll context", validationException);
353330
}
354331
}
355-
if (source != null) {
356-
if (source.slice() != null) {
357-
if (source.pointInTimeBuilder() == null && (scroll == false)) {
358-
validationException = addValidationError(
359-
"[slice] can only be used with [scroll] or [point-in-time] requests",
360-
validationException
361-
);
362-
}
363-
}
364-
if (source.from() > 0 && CollectionUtils.isEmpty(source.searchAfter()) == false) {
365-
validationException = addValidationError(
366-
"[from] parameter must be set to 0 when [search_after] is used",
367-
validationException
368-
);
369-
}
370-
if (source.storedFields() != null) {
371-
if (source.storedFields().fetchFields() == false) {
372-
if (source.fetchSource() != null && source.fetchSource().fetchSource()) {
373-
validationException = addValidationError(
374-
"[stored_fields] cannot be disabled if [_source] is requested",
375-
validationException
376-
);
377-
}
378-
if (source.fetchFields() != null) {
379-
validationException = addValidationError(
380-
"[stored_fields] cannot be disabled when using the [fields] option",
381-
validationException
382-
);
383-
}
384-
385-
}
386-
}
387-
if (source.subSearches().size() >= 2 && source.rankBuilder() == null) {
388-
validationException = addValidationError("[sub_searches] requires [rank]", validationException);
389-
}
390-
if (source.aggregations() != null) {
391-
validationException = source.aggregations().validate(validationException);
392-
}
393-
if (source.rankBuilder() != null) {
394-
int size = source.size() == -1 ? SearchService.DEFAULT_SIZE : source.size();
395-
if (size == 0) {
396-
validationException = addValidationError("[rank] requires [size] greater than [0]", validationException);
397-
}
398-
if (size > source.rankBuilder().rankWindowSize()) {
399-
validationException = addValidationError(
400-
"[rank] requires [rank_window_size: "
401-
+ source.rankBuilder().rankWindowSize()
402-
+ "]"
403-
+ " be greater than or equal to [size: "
404-
+ size
405-
+ "]",
406-
validationException
407-
);
408-
}
409-
int queryCount = source.subSearches().size() + source.knnSearch().size();
410-
if (source.rankBuilder().isCompoundBuilder() && queryCount < 2) {
411-
validationException = addValidationError(
412-
"[rank] requires a minimum of [2] result sets using a combination of sub searches and/or knn searches",
413-
validationException
414-
);
415-
}
416-
if (scroll) {
417-
validationException = addValidationError("[rank] cannot be used in a scroll context", validationException);
418-
}
419-
if (source.rescores() != null && source.rescores().isEmpty() == false) {
420-
validationException = addValidationError("[rank] cannot be used with [rescore]", validationException);
421-
}
422-
if (source.sorts() != null && source.sorts().isEmpty() == false) {
423-
validationException = addValidationError("[rank] cannot be used with [sort]", validationException);
424-
}
425-
if (source.collapse() != null) {
426-
validationException = addValidationError("[rank] cannot be used with [collapse]", validationException);
427-
}
428-
if (source.suggest() != null && source.suggest().getSuggestions().isEmpty() == false) {
429-
validationException = addValidationError("[rank] cannot be used with [suggest]", validationException);
430-
}
431-
if (source.highlighter() != null) {
432-
validationException = addValidationError("[rank] cannot be used with [highlighter]", validationException);
433-
}
434-
if (source.pointInTimeBuilder() != null) {
435-
validationException = addValidationError("[rank] cannot be used with [point in time]", validationException);
436-
}
437-
}
438-
if (source.rescores() != null) {
439-
for (@SuppressWarnings("rawtypes")
440-
RescorerBuilder rescoreBuilder : source.rescores()) {
441-
validationException = rescoreBuilder.validate(this, validationException);
442-
}
443-
}
444-
}
445332
if (pointInTimeBuilder() != null) {
446333
if (scroll) {
447334
validationException = addValidationError("using [point in time] is not allowed in a scroll context", validationException);
@@ -461,16 +348,6 @@ public ActionRequestValidationException validate() {
461348
if (preference() != null) {
462349
validationException = addValidationError("[preference] cannot be used with point in time", validationException);
463350
}
464-
} else if (source != null && source.sorts() != null) {
465-
for (SortBuilder<?> sortBuilder : source.sorts()) {
466-
if (sortBuilder instanceof FieldSortBuilder
467-
&& ShardDocSortField.NAME.equals(((FieldSortBuilder) sortBuilder).getFieldName())) {
468-
validationException = addValidationError(
469-
"[" + FieldSortBuilder.SHARD_DOC_FIELD_NAME + "] sort field cannot be used without [point in time]",
470-
validationException
471-
);
472-
}
473-
}
474351
}
475352
if (minCompatibleShardNode() != null) {
476353
if (isCcsMinimizeRoundtrips()) {

0 commit comments

Comments
 (0)