Skip to content

Commit bf2063e

Browse files
authored
ES|QL: Infer score mode from collector (elastic#131006) (elastic#131083)
1 parent 5e0a8a7 commit bf2063e

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
@@ -56,7 +56,7 @@ public Factory(
5656
taskConcurrency,
5757
limit,
5858
false,
59-
ScoreMode.COMPLETE_NO_SCORES
59+
shardContext -> ScoreMode.COMPLETE_NO_SCORES
6060
);
6161
}
6262

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
@@ -128,7 +128,7 @@ public LuceneMaxFactory(
128128
taskConcurrency,
129129
limit,
130130
false,
131-
ScoreMode.COMPLETE_NO_SCORES
131+
shardContext -> ScoreMode.COMPLETE_NO_SCORES
132132
);
133133
this.fieldName = fieldName;
134134
this.numberType = numberType;

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
@@ -128,7 +128,7 @@ public LuceneMinFactory(
128128
taskConcurrency,
129129
limit,
130130
false,
131-
ScoreMode.COMPLETE_NO_SCORES
131+
shardContext -> ScoreMode.COMPLETE_NO_SCORES
132132
);
133133
this.fieldName = fieldName;
134134
this.numberType = numberType;

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
@@ -101,11 +101,18 @@ protected Factory(
101101
int taskConcurrency,
102102
int limit,
103103
boolean needsScore,
104-
ScoreMode scoreMode
104+
Function<ShardContext, ScoreMode> scoreModeFunction
105105
) {
106106
this.limit = limit;
107107
this.dataPartitioning = dataPartitioning;
108-
this.sliceQueue = LuceneSliceQueue.create(contexts, queryFunction, dataPartitioning, autoStrategy, taskConcurrency, scoreMode);
108+
this.sliceQueue = LuceneSliceQueue.create(
109+
contexts,
110+
queryFunction,
111+
dataPartitioning,
112+
autoStrategy,
113+
taskConcurrency,
114+
scoreModeFunction
115+
);
109116
this.taskConcurrency = Math.min(sliceQueue.totalSlices(), taskConcurrency);
110117
this.needsScore = needsScore;
111118
}

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
@@ -107,12 +107,14 @@ public static LuceneSliceQueue create(
107107
DataPartitioning dataPartitioning,
108108
Function<Query, PartitioningStrategy> autoStrategy,
109109
int taskConcurrency,
110-
ScoreMode scoreMode
110+
Function<ShardContext, ScoreMode> scoreModeFunction
111111
) {
112112
List<LuceneSlice> slices = new ArrayList<>();
113113
Map<String, PartitioningStrategy> partitioningStrategies = new HashMap<>(contexts.size());
114+
114115
for (ShardContext ctx : contexts) {
115116
for (QueryAndTags queryAndExtra : queryFunction.apply(ctx)) {
117+
var scoreMode = scoreModeFunction.apply(ctx);
116118
Query query = queryAndExtra.query;
117119
query = scoreMode.needsScores() ? query : new ConstantScoreQuery(query);
118120
/*

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
@@ -80,7 +80,7 @@ public Factory(
8080
taskConcurrency,
8181
limit,
8282
needsScore,
83-
needsScore ? COMPLETE : COMPLETE_NO_SCORES
83+
shardContext -> needsScore ? COMPLETE : COMPLETE_NO_SCORES
8484
);
8585
this.maxPageSize = maxPageSize;
8686
// TODO: use a single limiter for multiple stage execution

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
*/
@@ -73,7 +69,7 @@ public Factory(
7369
taskConcurrency,
7470
limit,
7571
needsScore,
76-
needsScore ? TOP_DOCS_WITH_SCORES : TOP_DOCS
72+
scoreModeFunction(sorts, needsScore)
7773
);
7874
this.maxPageSize = maxPageSize;
7975
this.sorts = sorts;
@@ -323,18 +319,11 @@ static final class ScoringPerShardCollector extends PerShardCollector {
323319
}
324320
}
325321

326-
private static Function<ShardContext, Weight> weightFunction(
327-
Function<ShardContext, Query> queryFunction,
328-
List<SortBuilder<?>> sorts,
329-
boolean needsScore
330-
) {
322+
private static Function<ShardContext, ScoreMode> scoreModeFunction(List<SortBuilder<?>> sorts, boolean needsScore) {
331323
return ctx -> {
332-
final var query = queryFunction.apply(ctx);
333-
final var searcher = ctx.searcher();
334324
try {
335325
// we create a collector with a limit of 1 to determine the appropriate score mode to use.
336-
var scoreMode = newPerShardCollector(ctx, sorts, needsScore, 1).collector.scoreMode();
337-
return searcher.createWeight(searcher.rewrite(query), scoreMode, 1);
326+
return newPerShardCollector(ctx, sorts, needsScore, 1).collector.scoreMode();
338327
} catch (IOException e) {
339328
throw new UncheckedIOException(e);
340329
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ private TimeSeriesSortedSourceOperatorFactory(
6363
taskConcurrency,
6464
limit,
6565
false,
66-
ScoreMode.COMPLETE_NO_SCORES
66+
shardContext -> ScoreMode.COMPLETE_NO_SCORES
6767
);
6868
this.maxPageSize = maxPageSize;
6969
}

0 commit comments

Comments
 (0)