Skip to content

Commit 5f68b25

Browse files
committed
ES|QL: Infer score mode from collector (#131006)
1 parent 15d5fe0 commit 5f68b25

File tree

8 files changed

+21
-23
lines changed

8 files changed

+21
-23
lines changed

x-pack/plugin/esql/compute/src/main/java/org/elasticsearch/compute/lucene/LuceneCountOperator.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ public Factory(
5858
taskConcurrency,
5959
limit,
6060
false,
61-
ScoreMode.COMPLETE_NO_SCORES
61+
shardContext -> ScoreMode.COMPLETE_NO_SCORES
6262
);
6363
this.shardRefCounters = contexts;
6464
}

x-pack/plugin/esql/compute/src/main/java/org/elasticsearch/compute/lucene/LuceneMaxFactory.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ public LuceneMaxFactory(
129129
taskConcurrency,
130130
limit,
131131
false,
132-
ScoreMode.COMPLETE_NO_SCORES
132+
shardContext -> ScoreMode.COMPLETE_NO_SCORES
133133
);
134134
this.contexts = contexts;
135135
this.fieldName = fieldName;

x-pack/plugin/esql/compute/src/main/java/org/elasticsearch/compute/lucene/LuceneMinFactory.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ public LuceneMinFactory(
130130
taskConcurrency,
131131
limit,
132132
false,
133-
ScoreMode.COMPLETE_NO_SCORES
133+
shardContext -> ScoreMode.COMPLETE_NO_SCORES
134134
);
135135
this.shardRefCounters = contexts;
136136
this.fieldName = fieldName;

x-pack/plugin/esql/compute/src/main/java/org/elasticsearch/compute/lucene/LuceneOperator.java

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,11 +112,18 @@ protected Factory(
112112
int taskConcurrency,
113113
int limit,
114114
boolean needsScore,
115-
ScoreMode scoreMode
115+
Function<ShardContext, ScoreMode> scoreModeFunction
116116
) {
117117
this.limit = limit;
118118
this.dataPartitioning = dataPartitioning;
119-
this.sliceQueue = LuceneSliceQueue.create(contexts, queryFunction, dataPartitioning, autoStrategy, taskConcurrency, scoreMode);
119+
this.sliceQueue = LuceneSliceQueue.create(
120+
contexts,
121+
queryFunction,
122+
dataPartitioning,
123+
autoStrategy,
124+
taskConcurrency,
125+
scoreModeFunction
126+
);
120127
this.taskConcurrency = Math.min(sliceQueue.totalSlices(), taskConcurrency);
121128
this.needsScore = needsScore;
122129
}

x-pack/plugin/esql/compute/src/main/java/org/elasticsearch/compute/lucene/LuceneSliceQueue.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,12 +112,14 @@ public static LuceneSliceQueue create(
112112
DataPartitioning dataPartitioning,
113113
Function<Query, PartitioningStrategy> autoStrategy,
114114
int taskConcurrency,
115-
ScoreMode scoreMode
115+
Function<ShardContext, ScoreMode> scoreModeFunction
116116
) {
117117
List<LuceneSlice> slices = new ArrayList<>();
118118
Map<String, PartitioningStrategy> partitioningStrategies = new HashMap<>(contexts.size());
119+
119120
for (ShardContext ctx : contexts) {
120121
for (QueryAndTags queryAndExtra : queryFunction.apply(ctx)) {
122+
var scoreMode = scoreModeFunction.apply(ctx);
121123
Query query = queryAndExtra.query;
122124
query = scoreMode.needsScores() ? query : new ConstantScoreQuery(query);
123125
/*

x-pack/plugin/esql/compute/src/main/java/org/elasticsearch/compute/lucene/LuceneSourceOperator.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ public Factory(
8181
taskConcurrency,
8282
limit,
8383
needsScore,
84-
needsScore ? COMPLETE : COMPLETE_NO_SCORES
84+
shardContext -> needsScore ? COMPLETE : COMPLETE_NO_SCORES
8585
);
8686
this.contexts = contexts;
8787
this.maxPageSize = maxPageSize;

x-pack/plugin/esql/compute/src/main/java/org/elasticsearch/compute/lucene/LuceneTopNSourceOperator.java

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,13 @@
1212
import org.apache.lucene.search.CollectionTerminatedException;
1313
import org.apache.lucene.search.FieldDoc;
1414
import org.apache.lucene.search.LeafCollector;
15-
import org.apache.lucene.search.Query;
1615
import org.apache.lucene.search.ScoreDoc;
16+
import org.apache.lucene.search.ScoreMode;
1717
import org.apache.lucene.search.Sort;
1818
import org.apache.lucene.search.SortField;
1919
import org.apache.lucene.search.TopDocsCollector;
2020
import org.apache.lucene.search.TopFieldCollectorManager;
2121
import org.apache.lucene.search.TopScoreDocCollectorManager;
22-
import org.apache.lucene.search.Weight;
2322
import org.elasticsearch.common.Strings;
2423
import org.elasticsearch.compute.data.BlockFactory;
2524
import org.elasticsearch.compute.data.DocBlock;
@@ -44,9 +43,6 @@
4443
import java.util.function.Function;
4544
import java.util.stream.Collectors;
4645

47-
import static org.apache.lucene.search.ScoreMode.TOP_DOCS;
48-
import static org.apache.lucene.search.ScoreMode.TOP_DOCS_WITH_SCORES;
49-
5046
/**
5147
* Source operator that builds Pages out of the output of a TopFieldCollector (aka TopN)
5248
*/
@@ -75,7 +71,7 @@ public Factory(
7571
taskConcurrency,
7672
limit,
7773
needsScore,
78-
needsScore ? TOP_DOCS_WITH_SCORES : TOP_DOCS
74+
scoreModeFunction(sorts, needsScore)
7975
);
8076
this.contexts = contexts;
8177
this.maxPageSize = maxPageSize;
@@ -331,18 +327,11 @@ static final class ScoringPerShardCollector extends PerShardCollector {
331327
}
332328
}
333329

334-
private static Function<ShardContext, Weight> weightFunction(
335-
Function<ShardContext, Query> queryFunction,
336-
List<SortBuilder<?>> sorts,
337-
boolean needsScore
338-
) {
330+
private static Function<ShardContext, ScoreMode> scoreModeFunction(List<SortBuilder<?>> sorts, boolean needsScore) {
339331
return ctx -> {
340-
final var query = queryFunction.apply(ctx);
341-
final var searcher = ctx.searcher();
342332
try {
343333
// we create a collector with a limit of 1 to determine the appropriate score mode to use.
344-
var scoreMode = newPerShardCollector(ctx, sorts, needsScore, 1).collector.scoreMode();
345-
return searcher.createWeight(searcher.rewrite(query), scoreMode, 1);
334+
return newPerShardCollector(ctx, sorts, needsScore, 1).collector.scoreMode();
346335
} catch (IOException e) {
347336
throw new UncheckedIOException(e);
348337
}

x-pack/plugin/esql/compute/src/main/java/org/elasticsearch/compute/lucene/TimeSeriesSourceOperatorFactory.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ private TimeSeriesSourceOperatorFactory(
4545
taskConcurrency,
4646
limit,
4747
false,
48-
ScoreMode.COMPLETE_NO_SCORES
48+
shardContext -> ScoreMode.COMPLETE_NO_SCORES
4949
);
5050
this.contexts = contexts;
5151
this.maxPageSize = maxPageSize;

0 commit comments

Comments
 (0)