2424import org .elasticsearch .compute .data .DocVector ;
2525import org .elasticsearch .compute .data .IntVector ;
2626import org .elasticsearch .compute .data .Page ;
27- import org .elasticsearch .compute .data .Vector ;
2827import org .elasticsearch .core .Releasable ;
2928import org .elasticsearch .core .Releasables ;
3029
4443 * It's much faster to push queries to the {@link LuceneSourceOperator} or the like, but sometimes this isn't possible. So
4544 * this class is here to save the day.
4645 */
47- public abstract class LuceneQueryEvaluator <T extends Vector .Builder > implements Releasable {
46+ public abstract class LuceneQueryEvaluator <T extends Block .Builder > implements Releasable {
4847
4948 public record ShardConfig (Query query , IndexSearcher searcher ) {}
5049
5150 private final BlockFactory blockFactory ;
52- private final ShardConfig [] shards ;
51+ protected final ShardConfig [] shards ;
5352
5453 private final List <ShardState > perShardState ;
5554
@@ -67,9 +66,9 @@ public Block executeQuery(Page page) {
6766 DocVector docs = (DocVector ) block .asVector ();
6867 try {
6968 if (docs .singleSegmentNonDecreasing ()) {
70- return evalSingleSegmentNonDecreasing (docs ). asBlock () ;
69+ return evalSingleSegmentNonDecreasing (docs );
7170 } else {
72- return evalSlow (docs ). asBlock () ;
71+ return evalSlow (docs );
7372 }
7473 } catch (IOException e ) {
7574 throw new UncheckedIOException (e );
@@ -106,15 +105,15 @@ public Block executeQuery(Page page) {
106105 * common.
107106 * </p>
108107 */
109- private Vector evalSingleSegmentNonDecreasing (DocVector docs ) throws IOException {
108+ private Block evalSingleSegmentNonDecreasing (DocVector docs ) throws IOException {
110109 ShardState shardState = shardState (docs .shards ().getInt (0 ));
111110 SegmentState segmentState = shardState .segmentState (docs .segments ().getInt (0 ));
112111 int min = docs .docs ().getInt (0 );
113112 int max = docs .docs ().getInt (docs .getPositionCount () - 1 );
114113 int length = max - min + 1 ;
115- try (T scoreBuilder = createVectorBuilder (blockFactory , docs .getPositionCount ())) {
114+ try (T scoreBuilder = createBlockBuilder (blockFactory , docs .getPositionCount ())) {
116115 if (length == docs .getPositionCount () && length > 1 ) {
117- return segmentState .scoreDense (scoreBuilder , min , max );
116+ return segmentState .scoreDense (scoreBuilder , min , max , docs . getPositionCount () );
118117 }
119118 return segmentState .scoreSparse (scoreBuilder , docs .docs ());
120119 }
@@ -134,13 +133,13 @@ private Vector evalSingleSegmentNonDecreasing(DocVector docs) throws IOException
134133 * the order that the {@link DocVector} came in.
135134 * </p>
136135 */
137- private Vector evalSlow (DocVector docs ) throws IOException {
136+ private Block evalSlow (DocVector docs ) throws IOException {
138137 int [] map = docs .shardSegmentDocMapForwards ();
139138 // Clear any state flags from the previous run
140139 int prevShard = -1 ;
141140 int prevSegment = -1 ;
142141 SegmentState segmentState = null ;
143- try (T scoreBuilder = createVectorBuilder (blockFactory , docs .getPositionCount ())) {
142+ try (T scoreBuilder = createBlockBuilder (blockFactory , docs .getPositionCount ())) {
144143 for (int i = 0 ; i < docs .getPositionCount (); i ++) {
145144 int shard = docs .shards ().getInt (docs .shards ().getInt (map [i ]));
146145 int segment = docs .segments ().getInt (map [i ]);
@@ -156,7 +155,7 @@ private Vector evalSlow(DocVector docs) throws IOException {
156155 segmentState .scoreSingleDocWithScorer (scoreBuilder , docs .docs ().getInt (map [i ]));
157156 }
158157 }
159- try (Vector outOfOrder = scoreBuilder .build ()) {
158+ try (Block outOfOrder = scoreBuilder .build ()) {
160159 return outOfOrder .filter (docs .shardSegmentDocMapBackwards ());
161160 }
162161 }
@@ -247,9 +246,9 @@ private SegmentState(Weight weight, LeafReaderContext ctx) {
247246 * Score a range using the {@link BulkScorer}. This should be faster
248247 * than using {@link #scoreSparse} for dense doc ids.
249248 */
250- Vector scoreDense (T scoreBuilder , int min , int max ) throws IOException {
249+ Block scoreDense (T scoreBuilder , int min , int max , int positionCount ) throws IOException {
251250 if (noMatch ) {
252- return createNoMatchVector (blockFactory , max - min + 1 );
251+ return createNoMatchBlock (blockFactory , max - min + 1 );
253252 }
254253 if (bulkScorer == null || // The bulkScorer wasn't initialized
255254 Thread .currentThread () != bulkScorerThread // The bulkScorer was initialized on a different thread
@@ -258,19 +257,22 @@ Vector scoreDense(T scoreBuilder, int min, int max) throws IOException {
258257 bulkScorer = weight .bulkScorer (ctx );
259258 if (bulkScorer == null ) {
260259 noMatch = true ;
261- return createNoMatchVector (blockFactory , max - min + 1 );
260+ return createNoMatchBlock (blockFactory , positionCount );
262261 }
263262 }
264263 try (
265264 DenseCollector <T > collector = new DenseCollector <>(
266265 min ,
267266 max ,
268267 scoreBuilder ,
268+ ctx ,
269269 LuceneQueryEvaluator .this ::appendNoMatch ,
270- LuceneQueryEvaluator .this ::appendMatch
270+ LuceneQueryEvaluator .this ::appendMatch ,
271+ weight .getQuery ()
271272 )
272273 ) {
273274 bulkScorer .score (collector , ctx .reader ().getLiveDocs (), min , max + 1 );
275+ collector .finish ();
274276 return collector .build ();
275277 }
276278 }
@@ -279,10 +281,10 @@ Vector scoreDense(T scoreBuilder, int min, int max) throws IOException {
279281 * Score a vector of doc ids using {@link Scorer}. If you have a dense range of
280282 * doc ids it'd be faster to use {@link #scoreDense}.
281283 */
282- Vector scoreSparse (T scoreBuilder , IntVector docs ) throws IOException {
284+ Block scoreSparse (T scoreBuilder , IntVector docs ) throws IOException {
283285 initScorer (docs .getInt (0 ));
284286 if (noMatch ) {
285- return createNoMatchVector (blockFactory , docs .getPositionCount ());
287+ return createNoMatchBlock (blockFactory , docs .getPositionCount ());
286288 }
287289 for (int i = 0 ; i < docs .getPositionCount (); i ++) {
288290 scoreSingleDocWithScorer (scoreBuilder , docs .getInt (i ));
@@ -326,11 +328,13 @@ private void scoreSingleDocWithScorer(T builder, int doc) throws IOException {
326328 * doc ids are sent to {@link LeafCollector#collect(int)} in ascending order
327329 * which isn't documented, but @jpountz swears is true.
328330 */
329- static class DenseCollector <U extends Vector .Builder > implements LeafCollector , Releasable {
331+ static class DenseCollector <U extends Block .Builder > implements LeafCollector , Releasable {
330332 private final U scoreBuilder ;
331333 private final int max ;
334+ private final LeafReaderContext leafReaderContext ;
332335 private final Consumer <U > appendNoMatch ;
333336 private final CheckedBiConsumer <U , Scorable , IOException > appendMatch ;
337+ private final Query query ;
334338
335339 private Scorable scorer ;
336340 int next ;
@@ -339,14 +343,18 @@ static class DenseCollector<U extends Vector.Builder> implements LeafCollector,
339343 int min ,
340344 int max ,
341345 U scoreBuilder ,
346+ LeafReaderContext leafReaderContext ,
342347 Consumer <U > appendNoMatch ,
343- CheckedBiConsumer <U , Scorable , IOException > appendMatch
348+ CheckedBiConsumer <U , Scorable , IOException > appendMatch ,
349+ Query query
344350 ) {
345351 this .scoreBuilder = scoreBuilder ;
346352 this .max = max ;
347353 next = min ;
354+ this .leafReaderContext = leafReaderContext ;
348355 this .appendNoMatch = appendNoMatch ;
349356 this .appendMatch = appendMatch ;
357+ this .query = query ;
350358 }
351359
352360 @ Override
@@ -362,7 +370,7 @@ public void collect(int doc) throws IOException {
362370 appendMatch .accept (scoreBuilder , scorer );
363371 }
364372
365- public Vector build () {
373+ public Block build () {
366374 return scoreBuilder .build ();
367375 }
368376
@@ -387,12 +395,12 @@ public void close() {
387395 /**
388396 * Creates a vector where all positions correspond to elements that don't match the query
389397 */
390- protected abstract Vector createNoMatchVector (BlockFactory blockFactory , int size );
398+ protected abstract Block createNoMatchBlock (BlockFactory blockFactory , int size );
391399
392400 /**
393401 * Creates the corresponding vector builder to store the results of evaluating the query
394402 */
395- protected abstract T createVectorBuilder (BlockFactory blockFactory , int size );
403+ protected abstract T createBlockBuilder (BlockFactory blockFactory , int size );
396404
397405 /**
398406 * Appends a matching result to a builder created by @link createVectorBuilder}
0 commit comments