Skip to content

Commit 9d5c474

Browse files
authored
Avoid populating rank docs metadata if explain is not specified (#120536) (#120766)
1 parent 819ec9e commit 9d5c474

File tree

22 files changed

+226
-77
lines changed

22 files changed

+226
-77
lines changed

server/src/main/java/org/elasticsearch/TransportVersions.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,7 @@ static TransportVersion def(int id) {
170170
public static final TransportVersion ML_ROLLOVER_LEGACY_INDICES = def(8_830_00_0);
171171
public static final TransportVersion ADD_INCLUDE_FAILURE_INDICES_OPTION = def(8_831_00_0);
172172
public static final TransportVersion ESQL_RESPONSE_PARTIAL = def(8_832_00_0);
173+
public static final TransportVersion RANK_DOC_OPTIONAL_METADATA_FOR_EXPLAIN = def(8_833_00_0);
173174

174175
/*
175176
* STOP! READ THIS FIRST! No, really,

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -507,6 +507,7 @@ void executeRequest(
507507
});
508508

509509
final SearchSourceBuilder source = original.source();
510+
final boolean isExplain = source != null && source.explain() != null && source.explain();
510511
if (shouldOpenPIT(source)) {
511512
// disabling shard reordering for request
512513
original.setPreFilterShardSize(Integer.MAX_VALUE);
@@ -536,7 +537,12 @@ public void onFailure(Exception e) {
536537
} else {
537538
Rewriteable.rewriteAndFetch(
538539
original,
539-
searchService.getRewriteContext(timeProvider::absoluteStartMillis, resolvedIndices, original.pointInTimeBuilder()),
540+
searchService.getRewriteContext(
541+
timeProvider::absoluteStartMillis,
542+
resolvedIndices,
543+
original.pointInTimeBuilder(),
544+
isExplain
545+
),
540546
rewriteListener
541547
);
542548
}

server/src/main/java/org/elasticsearch/index/IndexService.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -807,7 +807,8 @@ public QueryRewriteContext newQueryRewriteContext(
807807
scriptService,
808808
null,
809809
null,
810-
null
810+
null,
811+
false
811812
);
812813
}
813814

server/src/main/java/org/elasticsearch/index/query/CoordinatorRewriteContext.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,8 @@ public CoordinatorRewriteContext(
105105
null,
106106
null,
107107
null,
108-
null
108+
null,
109+
false
109110
);
110111
this.dateFieldRangeInfo = dateFieldRangeInfo;
111112
this.tier = tier;

server/src/main/java/org/elasticsearch/index/query/QueryRewriteContext.java

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ public class QueryRewriteContext {
7272
private final ResolvedIndices resolvedIndices;
7373
private final PointInTimeBuilder pit;
7474
private QueryRewriteInterceptor queryRewriteInterceptor;
75+
private final boolean isExplain;
7576

7677
public QueryRewriteContext(
7778
final XContentParserConfiguration parserConfiguration,
@@ -89,7 +90,8 @@ public QueryRewriteContext(
8990
final ScriptCompiler scriptService,
9091
final ResolvedIndices resolvedIndices,
9192
final PointInTimeBuilder pit,
92-
final QueryRewriteInterceptor queryRewriteInterceptor
93+
final QueryRewriteInterceptor queryRewriteInterceptor,
94+
final boolean isExplain
9395
) {
9496

9597
this.parserConfiguration = parserConfiguration;
@@ -109,6 +111,7 @@ public QueryRewriteContext(
109111
this.resolvedIndices = resolvedIndices;
110112
this.pit = pit;
111113
this.queryRewriteInterceptor = queryRewriteInterceptor;
114+
this.isExplain = isExplain;
112115
}
113116

114117
public QueryRewriteContext(final XContentParserConfiguration parserConfiguration, final Client client, final LongSupplier nowInMillis) {
@@ -128,7 +131,8 @@ public QueryRewriteContext(final XContentParserConfiguration parserConfiguration
128131
null,
129132
null,
130133
null,
131-
null
134+
null,
135+
false
132136
);
133137
}
134138

@@ -139,6 +143,18 @@ public QueryRewriteContext(
139143
final ResolvedIndices resolvedIndices,
140144
final PointInTimeBuilder pit,
141145
final QueryRewriteInterceptor queryRewriteInterceptor
146+
) {
147+
this(parserConfiguration, client, nowInMillis, resolvedIndices, pit, queryRewriteInterceptor, false);
148+
}
149+
150+
public QueryRewriteContext(
151+
final XContentParserConfiguration parserConfiguration,
152+
final Client client,
153+
final LongSupplier nowInMillis,
154+
final ResolvedIndices resolvedIndices,
155+
final PointInTimeBuilder pit,
156+
final QueryRewriteInterceptor queryRewriteInterceptor,
157+
final boolean isExplain
142158
) {
143159
this(
144160
parserConfiguration,
@@ -156,7 +172,8 @@ public QueryRewriteContext(
156172
null,
157173
resolvedIndices,
158174
pit,
159-
queryRewriteInterceptor
175+
queryRewriteInterceptor,
176+
isExplain
160177
);
161178
}
162179

@@ -262,6 +279,10 @@ public void setMapUnmappedFieldAsString(boolean mapUnmappedFieldAsString) {
262279
this.mapUnmappedFieldAsString = mapUnmappedFieldAsString;
263280
}
264281

282+
public boolean isExplain() {
283+
return this.isExplain;
284+
}
285+
265286
public NamedWriteableRegistry getWriteableRegistry() {
266287
return writeableRegistry;
267288
}

server/src/main/java/org/elasticsearch/index/query/SearchExecutionContext.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -272,7 +272,8 @@ private SearchExecutionContext(
272272
scriptService,
273273
null,
274274
null,
275-
null
275+
null,
276+
false
276277
);
277278
this.shardId = shardId;
278279
this.shardRequestIndex = shardRequestIndex;

server/src/main/java/org/elasticsearch/indices/IndicesService.java

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1745,7 +1745,19 @@ public AliasFilter buildAliasFilter(ClusterState state, String index, Set<Resolv
17451745
* Returns a new {@link QueryRewriteContext} with the given {@code now} provider
17461746
*/
17471747
public QueryRewriteContext getRewriteContext(LongSupplier nowInMillis, ResolvedIndices resolvedIndices, PointInTimeBuilder pit) {
1748-
return new QueryRewriteContext(parserConfig, client, nowInMillis, resolvedIndices, pit, queryRewriteInterceptor);
1748+
return getRewriteContext(nowInMillis, resolvedIndices, pit, false);
1749+
}
1750+
1751+
/**
1752+
* Returns a new {@link QueryRewriteContext} with the given {@code now} provider
1753+
*/
1754+
public QueryRewriteContext getRewriteContext(
1755+
LongSupplier nowInMillis,
1756+
ResolvedIndices resolvedIndices,
1757+
PointInTimeBuilder pit,
1758+
final boolean isExplain
1759+
) {
1760+
return new QueryRewriteContext(parserConfig, client, nowInMillis, resolvedIndices, pit, queryRewriteInterceptor, isExplain);
17491761
}
17501762

17511763
public DataRewriteContext getDataRewriteContext(LongSupplier nowInMillis) {

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

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1950,7 +1950,19 @@ private void rewriteAndFetchShardRequest(IndexShard shard, ShardSearchRequest re
19501950
* Returns a new {@link QueryRewriteContext} with the given {@code now} provider
19511951
*/
19521952
public QueryRewriteContext getRewriteContext(LongSupplier nowInMillis, ResolvedIndices resolvedIndices, PointInTimeBuilder pit) {
1953-
return indicesService.getRewriteContext(nowInMillis, resolvedIndices, pit);
1953+
return getRewriteContext(nowInMillis, resolvedIndices, pit, false);
1954+
}
1955+
1956+
/**
1957+
* Returns a new {@link QueryRewriteContext} with the given {@code now} provider
1958+
*/
1959+
public QueryRewriteContext getRewriteContext(
1960+
LongSupplier nowInMillis,
1961+
ResolvedIndices resolvedIndices,
1962+
PointInTimeBuilder pit,
1963+
final boolean isExplain
1964+
) {
1965+
return indicesService.getRewriteContext(nowInMillis, resolvedIndices, pit, isExplain);
19541966
}
19551967

19561968
public CoordinatorRewriteContextProvider getCoordinatorRewriteContextProvider(LongSupplier nowInMillis) {

server/src/main/java/org/elasticsearch/search/retriever/CompoundRetrieverBuilder.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ public T addChild(RetrieverBuilder retrieverBuilder) {
7878
/**
7979
* Combines the provided {@code rankResults} to return the final top documents.
8080
*/
81-
protected abstract RankDoc[] combineInnerRetrieverResults(List<ScoreDoc[]> rankResults);
81+
protected abstract RankDoc[] combineInnerRetrieverResults(List<ScoreDoc[]> rankResults, boolean explain);
8282

8383
@Override
8484
public final boolean isCompound() {
@@ -181,7 +181,7 @@ public void onResponse(MultiSearchResponse items) {
181181
failures.forEach(ex::addSuppressed);
182182
listener.onFailure(ex);
183183
} else {
184-
results.set(combineInnerRetrieverResults(topDocs));
184+
results.set(combineInnerRetrieverResults(topDocs, ctx.isExplain()));
185185
listener.onResponse(null);
186186
}
187187
}

server/src/main/java/org/elasticsearch/search/retriever/RescorerRetrieverBuilder.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ protected RescorerRetrieverBuilder clone(List<RetrieverSource> newChildRetriever
148148
}
149149

150150
@Override
151-
protected RankDoc[] combineInnerRetrieverResults(List<ScoreDoc[]> rankResults) {
151+
protected RankDoc[] combineInnerRetrieverResults(List<ScoreDoc[]> rankResults, boolean explain) {
152152
assert rankResults.size() == 1;
153153
ScoreDoc[] scoreDocs = rankResults.get(0);
154154
RankDoc[] rankDocs = new RankDoc[scoreDocs.length];

0 commit comments

Comments
 (0)