Skip to content

Commit 9f39ad3

Browse files
committed
Add javadoc
1 parent 3b994f6 commit 9f39ad3

File tree

6 files changed

+49
-25
lines changed

6 files changed

+49
-25
lines changed

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

Lines changed: 33 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,11 @@
2020
import org.elasticsearch.common.CheckedBiConsumer;
2121
import org.elasticsearch.compute.data.Block;
2222
import org.elasticsearch.compute.data.BlockFactory;
23-
import org.elasticsearch.compute.data.BooleanVector;
2423
import org.elasticsearch.compute.data.DocBlock;
2524
import org.elasticsearch.compute.data.DocVector;
2625
import org.elasticsearch.compute.data.IntVector;
2726
import org.elasticsearch.compute.data.Page;
2827
import org.elasticsearch.compute.data.Vector;
29-
import org.elasticsearch.compute.operator.EvalOperator;
3028
import org.elasticsearch.core.Releasable;
3129
import org.elasticsearch.core.Releasables;
3230

@@ -38,11 +36,13 @@
3836
import java.util.function.Consumer;
3937

4038
/**
41-
* {@link EvalOperator.ExpressionEvaluator} to run a Lucene {@link Query} during
42-
* the compute engine's normal execution, yielding matches/does not match into
43-
* a {@link BooleanVector}. It's much faster to push these to the
44-
* {@link LuceneSourceOperator} or the like, but sometimes this isn't possible. So
45-
* this evaluator is here to save the day.
39+
* Base class for evaluating a Lucene query at the compute engine and providing a Block as a result.
40+
* Subclasses can override methods to decide what type of {@link Block} should be returned, and how to add results to it
41+
* based on documents on the Page matching the query or not.
42+
* See {@link LuceneQueryExpressionEvaluator} for an example of how to use this class and {@link LuceneQueryScoreEvaluator} for
43+
* examples of subclasses that provide different types of scoring results for different ESQL constructs.
44+
* It's much faster to push queries to the {@link LuceneSourceOperator} or the like, but sometimes this isn't possible. So
45+
* this class is here to save the day.
4646
*/
4747
public abstract class LuceneQueryEvaluator<T extends Vector.Builder> implements Releasable {
4848

@@ -111,7 +111,7 @@ private Vector evalSingleSegmentNonDecreasing(DocVector docs) throws IOException
111111
int min = docs.docs().getInt(0);
112112
int max = docs.docs().getInt(docs.getPositionCount() - 1);
113113
int length = max - min + 1;
114-
try (T scoreBuilder = createBuilder(blockFactory, length)) {
114+
try (T scoreBuilder = createVectorBuilder(blockFactory, length)) {
115115
if (length == docs.getPositionCount() && length > 1) {
116116
return segmentState.scoreDense(scoreBuilder, min, max);
117117
}
@@ -139,7 +139,7 @@ private Vector evalSlow(DocVector docs) throws IOException {
139139
int prevShard = -1;
140140
int prevSegment = -1;
141141
SegmentState segmentState = null;
142-
try (T scoreBuilder = createBuilder(blockFactory, docs.getPositionCount())) {
142+
try (T scoreBuilder = createVectorBuilder(blockFactory, docs.getPositionCount())) {
143143
for (int i = 0; i < docs.getPositionCount(); i++) {
144144
int shard = docs.shards().getInt(docs.shards().getInt(map[i]));
145145
int segment = docs.segments().getInt(map[i]);
@@ -174,6 +174,9 @@ private ShardState shardState(int shard) throws IOException {
174174
return shardState;
175175
}
176176

177+
/**
178+
* Contains shard search related information, like the weight and index searcher
179+
*/
177180
private class ShardState {
178181
private final Weight weight;
179182
private final IndexSearcher searcher;
@@ -196,6 +199,9 @@ SegmentState segmentState(int segment) throws IOException {
196199
}
197200
}
198201

202+
/**
203+
* Contains segment search related information, like the leaf reader context and bulk scorer
204+
*/
199205
private class SegmentState {
200206
private final Weight weight;
201207
private final LeafReaderContext ctx;
@@ -369,13 +375,29 @@ public void close() {
369375
}
370376
}
371377

378+
/**
379+
* Returns the score mode to use on searches
380+
*/
372381
protected abstract ScoreMode scoreMode();
373382

383+
/**
384+
* Creates a vector where all positions correspond to elements that don't match the query
385+
*/
374386
protected abstract Vector createNoMatchVector(BlockFactory blockFactory, int size);
375387

376-
protected abstract T createBuilder(BlockFactory blockFactory, int size);
388+
/**
389+
* Creates the corresponding vector builder to store the results of evaluating the query
390+
*/
391+
protected abstract T createVectorBuilder(BlockFactory blockFactory, int size);
392+
393+
/**
394+
* Appends a matching result to a builder created by @link createVectorBuilder}
395+
*/
396+
protected abstract void appendMatch(T builder, Scorable scorer) throws IOException;
377397

398+
/**
399+
* Appends a non matching result to a builder created by @link createVectorBuilder}
400+
*/
378401
protected abstract void appendNoMatch(T builder);
379402

380-
protected abstract void appendMatch(T builder, Scorable scorer) throws IOException;
381403
}

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

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,8 @@
2323
/**
2424
* {@link EvalOperator.ExpressionEvaluator} to run a Lucene {@link Query} during
2525
* the compute engine's normal execution, yielding matches/does not match into
26-
* a {@link BooleanVector}. It's much faster to push these to the
27-
* {@link LuceneSourceOperator} or the like, but sometimes this isn't possible. So
28-
* this evaluator is here to save the day.
26+
* a {@link BooleanVector}.
27+
* @see LuceneQueryScoreEvaluator
2928
*/
3029
public class LuceneQueryExpressionEvaluator extends LuceneQueryEvaluator<BooleanVector.Builder>
3130
implements
@@ -51,7 +50,7 @@ protected Vector createNoMatchVector(BlockFactory blockFactory, int size) {
5150
}
5251

5352
@Override
54-
protected BooleanVector.Builder createBuilder(BlockFactory blockFactory, int size) {
53+
protected BooleanVector.Builder createVectorBuilder(BlockFactory blockFactory, int size) {
5554
return blockFactory.newBooleanVectorFixedBuilder(size);
5655
}
5756

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

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,23 +11,21 @@
1111
import org.apache.lucene.search.Scorable;
1212
import org.apache.lucene.search.ScoreMode;
1313
import org.elasticsearch.compute.data.BlockFactory;
14-
import org.elasticsearch.compute.data.BooleanVector;
1514
import org.elasticsearch.compute.data.DoubleBlock;
1615
import org.elasticsearch.compute.data.DoubleVector;
1716
import org.elasticsearch.compute.data.Page;
1817
import org.elasticsearch.compute.data.Vector;
1918
import org.elasticsearch.compute.operator.DriverContext;
20-
import org.elasticsearch.compute.operator.EvalOperator;
2119
import org.elasticsearch.compute.operator.ScoreOperator;
2220

2321
import java.io.IOException;
2422

2523
/**
26-
* {@link EvalOperator.ExpressionEvaluator} to run a Lucene {@link Query} during
27-
* the compute engine's normal execution, yielding matches/does not match into
28-
* a {@link BooleanVector}. It's much faster to push these to the
29-
* {@link LuceneSourceOperator} or the like, but sometimes this isn't possible. So
30-
* this evaluator is here to save the day.
24+
* {@link ScoreOperator.ExpressionScorer} to run a Lucene {@link Query} during
25+
* the compute engine's normal execution, yielding the corresponding scores into
26+
* a {@link DoubleVector}.
27+
* Elements that don't match will have a score of {@link #NO_MATCH_SCORE}.
28+
* @see LuceneQueryScoreEvaluator
3129
*/
3230
public class LuceneQueryScoreEvaluator extends LuceneQueryEvaluator<DoubleVector.Builder> implements ScoreOperator.ExpressionScorer {
3331

@@ -53,7 +51,7 @@ protected Vector createNoMatchVector(BlockFactory blockFactory, int size) {
5351
}
5452

5553
@Override
56-
protected DoubleVector.Builder createBuilder(BlockFactory blockFactory, int size) {
54+
protected DoubleVector.Builder createVectorBuilder(BlockFactory blockFactory, int size) {
5755
return blockFactory.newDoubleVectorFixedBuilder(size);
5856
}
5957

x-pack/plugin/esql/src/main/java/org/elasticsearch/xpack/esql/expression/predicate/logical/BinaryLogic.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,9 @@ public ScoreOperator.ExpressionScorer.Factory toScorer(ToScorer toScorer) {
123123
return context -> new BinaryLogicScorer(context, toScorer.toScorer(left()).get(context), toScorer.toScorer(right()).get(context));
124124
}
125125

126+
/**
127+
* Binary logic adds together scores coming from the left and right expressions, both for conjunctions and disjunctions
128+
*/
126129
private record BinaryLogicScorer(DriverContext driverContext, ScoreOperator.ExpressionScorer left, ScoreOperator.ExpressionScorer right)
127130
implements
128131
ScoreOperator.ExpressionScorer {

x-pack/plugin/esql/src/main/java/org/elasticsearch/xpack/esql/score/ExpressionScoreMapper.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,14 @@
77

88
package org.elasticsearch.xpack.esql.score;
99

10-
import org.elasticsearch.compute.operator.EvalOperator.ExpressionEvaluator;
1110
import org.elasticsearch.compute.operator.ScoreOperator.ExpressionScorer;
1211
import org.elasticsearch.xpack.esql.core.expression.Expression;
1312
import org.elasticsearch.xpack.esql.planner.EsPhysicalOperationProviders;
1413

1514
import java.util.List;
1615

1716
/**
18-
* Expressions that have a mapping to an {@link ExpressionEvaluator}.
17+
* Maps expressions that have a mapping to an {@link ExpressionScorer}. Allows for transforming expressions into their corresponding scores.
1918
*/
2019
public interface ExpressionScoreMapper {
2120
interface ToScorer {

x-pack/plugin/esql/src/main/java/org/elasticsearch/xpack/esql/score/ScoreMapper.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@
1616

1717
import java.util.List;
1818

19+
/**
20+
* Maps an expression tree into ExpressionScorer.Factory, so scores can be evaluated for an expression tree.
21+
*/
1922
public class ScoreMapper {
2023

2124
public static ScoreOperator.ExpressionScorer.Factory toScorer(Expression expression, List<ShardContext> shardContexts) {

0 commit comments

Comments
 (0)