-
Notifications
You must be signed in to change notification settings - Fork 2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Optimize the canMatch phase on the data node #14511
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -83,7 +83,6 @@ | |
import org.opensearch.index.query.InnerHitContextBuilder; | ||
import org.opensearch.index.query.MatchAllQueryBuilder; | ||
import org.opensearch.index.query.MatchNoneQueryBuilder; | ||
import org.opensearch.index.query.QueryBuilder; | ||
import org.opensearch.index.query.QueryRewriteContext; | ||
import org.opensearch.index.query.QueryShardContext; | ||
import org.opensearch.index.query.Rewriteable; | ||
|
@@ -1597,8 +1596,7 @@ public CanMatchResponse canMatch(ShardSearchRequest request) throws IOException | |
private CanMatchResponse canMatch(ShardSearchRequest request, boolean checkRefreshPending) throws IOException { | ||
assert request.searchType() == SearchType.QUERY_THEN_FETCH : "unexpected search type: " + request.searchType(); | ||
final ReaderContext readerContext = request.readerId() != null ? findReaderContext(request.readerId(), request) : null; | ||
final Releasable markAsUsed = readerContext != null ? readerContext.markAsUsed(getKeepAlive(request)) : () -> {}; | ||
try (Releasable ignored = markAsUsed) { | ||
try (Releasable ignored = readerContext != null ? readerContext.markAsUsed(getKeepAlive(request)) : () -> {}) { | ||
final IndexService indexService; | ||
final Engine.Searcher canMatchSearcher; | ||
final boolean hasRefreshPending; | ||
|
@@ -1621,22 +1619,35 @@ private CanMatchResponse canMatch(ShardSearchRequest request, boolean checkRefre | |
request.getClusterAlias() | ||
); | ||
Rewriteable.rewrite(request.getRewriteable(), context, false); | ||
final boolean aliasFilterCanMatch = request.getAliasFilter().getQueryBuilder() instanceof MatchNoneQueryBuilder == false; | ||
FieldSortBuilder sortBuilder = FieldSortBuilder.getPrimaryFieldSortOrNull(request.source()); | ||
MinAndMax<?> minMax = sortBuilder != null ? FieldSortBuilder.getMinMaxOrNull(context, sortBuilder) : null; | ||
boolean canMatch; | ||
if (canRewriteToMatchNone(request.source())) { | ||
QueryBuilder queryBuilder = request.source().query(); | ||
canMatch = aliasFilterCanMatch && queryBuilder instanceof MatchNoneQueryBuilder == false; | ||
} else { | ||
// null query means match_all | ||
canMatch = aliasFilterCanMatch; | ||
|
||
if (hasRefreshPending == false) { | ||
final boolean aliasFilterCannotMatch = request.getAliasFilter().getQueryBuilder() instanceof MatchNoneQueryBuilder; | ||
if (aliasFilterCannotMatch | ||
|| (canRewriteToMatchNone(request.source()) && request.source().query() instanceof MatchNoneQueryBuilder)) { | ||
return new CanMatchResponse(false, null); | ||
} | ||
} | ||
final FieldDoc searchAfterFieldDoc = getSearchAfterFieldDoc(request, context); | ||
final Integer trackTotalHitsUpto = request.source() == null ? null : request.source().trackTotalHitsUpTo(); | ||
canMatch = canMatch && canMatchSearchAfter(searchAfterFieldDoc, minMax, sortBuilder, trackTotalHitsUpto); | ||
|
||
return new CanMatchResponse(canMatch || hasRefreshPending, minMax); | ||
final FieldSortBuilder sortBuilder = FieldSortBuilder.getPrimaryFieldSortOrNull(request.source()); | ||
final MinAndMax<?> minMax = sortBuilder != null ? FieldSortBuilder.getMinMaxOrNull(context, sortBuilder) : null; | ||
if (hasRefreshPending || minMax == null) { | ||
return new CanMatchResponse(true, minMax); | ||
} | ||
|
||
boolean canMatch = true; | ||
// Skipping search on shard/segment entirely can cause mismatch on total_tracking_hits, hence skip only if | ||
// track_total_hits is false. | ||
// Check for sort.missing == null, since in case of missing values sort queries, if segment/shard's min/max | ||
// is out of search_after range, it still should be printed and hence we should not skip segment/shard. | ||
final Integer trackTotalHitsUpto = request.source() == null ? null : request.source().trackTotalHitsUpTo(); | ||
if (Objects.equals(trackTotalHitsUpto, SearchContext.TRACK_TOTAL_HITS_DISABLED) && sortBuilder.missing() == null) { | ||
final Object primarySearchAfterField = SearchAfterBuilder.getPrimarySearchAfterFieldOrNull(request.source()); | ||
if (primarySearchAfterField != null) { | ||
final FieldDoc searchAfterFieldDoc = getPrimarySearchAfterFieldDoc(sortBuilder, primarySearchAfterField, context); | ||
canMatch = canMatchSearchAfter(searchAfterFieldDoc, minMax, sortBuilder, trackTotalHitsUpto); | ||
} | ||
} | ||
return new CanMatchResponse(canMatch, canMatch ? minMax : null); | ||
} | ||
} | ||
} | ||
|
@@ -1647,15 +1658,9 @@ public static boolean canMatchSearchAfter( | |
FieldSortBuilder primarySortField, | ||
Integer trackTotalHitsUpto | ||
) { | ||
// Check for sort.missing == null, since in case of missing values sort queries, if segment/shard's min/max | ||
// is out of search_after range, it still should be printed and hence we should not skip segment/shard. | ||
// Skipping search on shard/segment entirely can cause mismatch on total_tracking_hits, hence skip only if | ||
// track_total_hits is false. | ||
if (searchAfter != null | ||
&& minMax != null | ||
&& primarySortField != null | ||
&& primarySortField.missing() == null | ||
&& Objects.equals(trackTotalHitsUpto, SearchContext.TRACK_TOTAL_HITS_DISABLED)) { | ||
assert primarySortField != null && primarySortField.missing() == null; | ||
assert Objects.equals(trackTotalHitsUpto, SearchContext.TRACK_TOTAL_HITS_DISABLED); | ||
if (searchAfter != null && minMax != null) { | ||
final Object searchAfterPrimary = searchAfter.fields[0]; | ||
if (primarySortField.order() == SortOrder.DESC) { | ||
if (minMax.compareMin(searchAfterPrimary) > 0) { | ||
|
@@ -1672,16 +1677,14 @@ public static boolean canMatchSearchAfter( | |
return true; | ||
} | ||
|
||
private static FieldDoc getSearchAfterFieldDoc(ShardSearchRequest request, QueryShardContext context) throws IOException { | ||
if (context != null && request != null && request.source() != null && request.source().sorts() != null) { | ||
final List<SortBuilder<?>> sorts = request.source().sorts(); | ||
final Object[] searchAfter = request.source().searchAfter(); | ||
final Optional<SortAndFormats> sortOpt = SortBuilder.buildSort(sorts, context); | ||
if (sortOpt.isPresent() && !CollectionUtils.isEmpty(searchAfter)) { | ||
return SearchAfterBuilder.buildFieldDoc(sortOpt.get(), searchAfter); | ||
} | ||
} | ||
return null; | ||
private static FieldDoc getPrimarySearchAfterFieldDoc( | ||
FieldSortBuilder primarySortBuilder, | ||
Object primarySearchAfter, | ||
QueryShardContext context | ||
) throws IOException { | ||
final Optional<SortAndFormats> sortOpt = SortBuilder.buildSort(List.of(primarySortBuilder), context); | ||
return sortOpt.map(sortAndFormats -> SearchAfterBuilder.buildFieldDoc(sortAndFormats, new Object[] { primarySearchAfter })) | ||
.orElse(null); | ||
Comment on lines
+1680
to
+1687
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Unable to see any optimization from this refactoring. Seems more like restructuring and not sure if the readability is improving. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The previous implementation would construct all sortFields if user request contains multi SortBuilders and search after fields, but what we need is just the primary sort search after field. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is it possible to qualitatively quantify the impact? Maybe some approximation of the worst case query earlier that would be improved with this change There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think that this is quite simple, for example, if I have following query:
Previously, we have to evaluate and build these all three sort fields, now we just need to build the primary sort field. The idea behind this change is same as |
||
} | ||
|
||
/** | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -513,10 +513,17 @@ private boolean canMatch(LeafReaderContext ctx) throws IOException { | |
} | ||
|
||
private boolean canMatchSearchAfter(LeafReaderContext ctx) throws IOException { | ||
if (searchContext.searchAfter() != null && searchContext.request() != null && searchContext.request().source() != null) { | ||
// Skipping search on shard/segment entirely can cause mismatch on total_tracking_hits, hence skip only if | ||
// track_total_hits is false. | ||
if (searchContext.searchAfter() != null | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @jainankitk The potential improvements are mostly related to short circuit. For example, we can check the conditions upfront here and avoid to build sort field or read min max values unnecessary. |
||
&& searchContext.request() != null | ||
&& searchContext.request().source() != null | ||
&& searchContext.trackTotalHitsUpTo() == SearchContext.TRACK_TOTAL_HITS_DISABLED) { | ||
// Only applied on primary sort field and primary search_after. | ||
FieldSortBuilder primarySortField = FieldSortBuilder.getPrimaryFieldSortOrNull(searchContext.request().source()); | ||
if (primarySortField != null) { | ||
// Check for sort.missing == null, since in case of missing values sort queries, if segment/shard's min/max | ||
// is out of search_after range, it still should be printed and hence we should not skip segment/shard. | ||
if (primarySortField != null && primarySortField.missing() == null) { | ||
MinAndMax<?> minMax = FieldSortBuilder.getMinMaxOrNullForSegment( | ||
this.searchContext.getQueryShardContext(), | ||
ctx, | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also, can we just not send over minMax? Should get ignored if canMatch is false anyway?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think that setting minMax as null is equivalent to not sending this value.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Then we probably should not even care what we are sending for minMax if canMatch is false? So below should also work?