Skip to content

Commit 65e16b7

Browse files
committed
Add comments and javadocs
1 parent 9674073 commit 65e16b7

File tree

8 files changed

+54
-77
lines changed

8 files changed

+54
-77
lines changed

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

Lines changed: 36 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ private Vector evalSlow(DocVector docs) throws IOException {
149149
prevSegment = segment;
150150
}
151151
if (segmentState.noMatch) {
152-
docScorerVectorProvider.scoreNoHit();
152+
docScorerVectorProvider.addNoMatchingDoc();
153153
} else {
154154
segmentState.scoreSingleDocWithScorer(docs.docs().getInt(map[i]));
155155
}
@@ -291,14 +291,14 @@ private void initScorer(int minDocId) throws IOException {
291291

292292
private void scoreSingleDocWithScorer(int doc) throws IOException {
293293
if (scorer.iterator().docID() == doc) {
294-
docScorerVectorProvider.scoreHit(scorer);
294+
docScorerVectorProvider.addNatchingDoc(scorer);
295295
} else if (scorer.iterator().docID() > doc) {
296-
docScorerVectorProvider.scoreNoHit();
296+
docScorerVectorProvider.addNoMatchingDoc();
297297
} else {
298298
if (scorer.iterator().advance(doc) == doc) {
299-
docScorerVectorProvider.scoreHit(scorer);
299+
docScorerVectorProvider.addNatchingDoc(scorer);
300300
} else {
301-
docScorerVectorProvider.scoreNoHit();
301+
docScorerVectorProvider.addNoMatchingDoc();
302302
}
303303
}
304304
}
@@ -334,9 +334,9 @@ public void setScorer(Scorable scorable) {
334334
@Override
335335
public void collect(int doc) throws IOException {
336336
while (next++ < doc) {
337-
docScorerVectorProvider.scoreNoHit();
337+
docScorerVectorProvider.addNoMatchingDoc();
338338
}
339-
docScorerVectorProvider.scoreHit(scorable);
339+
docScorerVectorProvider.addNatchingDoc(scorable);
340340
}
341341

342342
public Vector build() {
@@ -346,7 +346,7 @@ public Vector build() {
346346
@Override
347347
public void finish() {
348348
while (next++ <= max) {
349-
docScorerVectorProvider.scoreNoHit();
349+
docScorerVectorProvider.addNoMatchingDoc();
350350
}
351351
}
352352

@@ -356,18 +356,40 @@ public void close() {
356356
}
357357
}
358358

359+
/**
360+
* Encapsulates the logic for scoring documents, depending on whether we need a boolean vector for filtering
361+
* or a double vector for scoring
362+
*/
359363
private interface DocScorerVectorProvider extends Releasable {
360364

365+
/**
366+
* Returns the vector to use when no documents match the query
367+
*/
361368
Vector noMatchVector(int docs);
362369

370+
/**
371+
* Initializes the vector to build. Needed to be called before scoring any documents
372+
*/
363373
void init(int numDocs);
364374

365-
void scoreHit(Scorable scorable) throws IOException;
375+
/**
376+
* Adds a document that matches the query
377+
*/
378+
void addNatchingDoc(Scorable scorable) throws IOException;
366379

367-
void scoreNoHit();
380+
/**
381+
* Adds a document that does not match the query
382+
*/
383+
void addNoMatchingDoc();
368384

385+
/**
386+
* Builds the resulting vector after adding all docs
387+
*/
369388
Vector build();
370389

390+
/**
391+
* Retrieves the score mode for the query
392+
*/
371393
ScoreMode scoreMode();
372394
}
373395

@@ -396,13 +418,13 @@ public void init(int numDocs) {
396418
}
397419

398420
@Override
399-
public void scoreHit(Scorable scorable) {
421+
public void addNatchingDoc(Scorable scorable) {
400422
assert builder != null : "init must be called before scoring";
401423
builder.appendBoolean(true);
402424
}
403425

404426
@Override
405-
public void scoreNoHit() {
427+
public void addNoMatchingDoc() {
406428
assert builder != null : "init must be called before scoring";
407429
builder.appendBoolean(false);
408430
}
@@ -444,13 +466,13 @@ public void init(int numDocs) {
444466
}
445467

446468
@Override
447-
public void scoreHit(Scorable scorable) throws IOException {
469+
public void addNatchingDoc(Scorable scorable) throws IOException {
448470
assert builder != null : "init must be called before scoring";
449471
builder.appendDouble(scorable.score());
450472
}
451473

452474
@Override
453-
public void scoreNoHit() {
475+
public void addNoMatchingDoc() {
454476
assert builder != null : "init must be called before scoring";
455477
builder.appendDouble(SCORE_FOR_FALSE);
456478
}

x-pack/plugin/esql/src/main/java/org/elasticsearch/xpack/esql/evaluator/mapper/BooleanToScoringExpressionEvaluator.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,11 @@
1717

1818
import static org.elasticsearch.compute.lucene.LuceneQueryExpressionEvaluator.SCORE_FOR_FALSE;
1919

20+
/**
21+
* Converts a {@link BooleanBlock} to a {@link DoubleBlock} if needed. This is needed to convert boolean expressions
22+
* into scores that can be mixed together with other scoring expressions, or just for boolean expressions to be scored.
23+
* Scores are converted using 1.0 for true and {* @link LuceneQueryExpressionEvaluator#SCORE_FOR_FALSE} for false.
24+
*/
2025
public class BooleanToScoringExpressionEvaluator implements EvalOperator.ExpressionEvaluator {
2126

2227
private final EvalOperator.ExpressionEvaluator inner;

x-pack/plugin/esql/src/main/java/org/elasticsearch/xpack/esql/evaluator/predicate/operator/logical/NotScoringEvaluator.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@
2020
import static org.elasticsearch.compute.lucene.LuceneQueryExpressionEvaluator.SCORE_FOR_FALSE;
2121

2222
/**
23-
* {@link EvalOperator.ExpressionEvaluator} implementation for {@link Not}.
24-
* This class is generated. Do not edit it.
23+
* {@link EvalOperator.ExpressionEvaluator} implementation for {@link Not} when scores are used.
24+
* It returns 0.0 for false and {@link org.elasticsearch.compute.lucene.LuceneQueryExpressionEvaluator#SCORE_FOR_FALSE} for true.
2525
*/
2626
public final class NotScoringEvaluator implements EvalOperator.ExpressionEvaluator {
2727
private final Source source;

x-pack/plugin/esql/src/main/java/org/elasticsearch/xpack/esql/expression/function/fulltext/FullTextFunction.java

Lines changed: 0 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,9 @@
2323
import org.elasticsearch.xpack.esql.core.querydsl.query.Query;
2424
import org.elasticsearch.xpack.esql.core.tree.Source;
2525
import org.elasticsearch.xpack.esql.core.type.DataType;
26-
import org.elasticsearch.xpack.esql.core.util.Holder;
2726
import org.elasticsearch.xpack.esql.evaluator.mapper.EvaluatorMapper;
2827
import org.elasticsearch.xpack.esql.expression.predicate.logical.BinaryLogic;
2928
import org.elasticsearch.xpack.esql.expression.predicate.logical.Not;
30-
import org.elasticsearch.xpack.esql.expression.predicate.logical.Or;
3129
import org.elasticsearch.xpack.esql.optimizer.rules.physical.local.LucenePushdownPredicates;
3230
import org.elasticsearch.xpack.esql.plan.logical.Aggregate;
3331
import org.elasticsearch.xpack.esql.plan.logical.EsRelation;
@@ -214,65 +212,6 @@ private static void checkFullTextQueryFunctions(LogicalPlan plan, Failures failu
214212
}
215213
}
216214

217-
/**
218-
* Checks whether a condition contains a disjunction with a full text search.
219-
* If it does, check that every element of the disjunction is a full text search or combinations (AND, OR, NOT) of them.
220-
* If not, add a failure to the failures collection.
221-
*
222-
* @param condition condition to check for disjunctions of full text searches
223-
* @param failures failures collection to add to
224-
*/
225-
private static void checkFullTextSearchDisjunctions(Expression condition, Failures failures) {
226-
Holder<Boolean> isInvalid = new Holder<>(false);
227-
condition.forEachDown(Or.class, or -> {
228-
if (isInvalid.get()) {
229-
// Exit early if we already have a failures
230-
return;
231-
}
232-
if (checkDisjunctionPushable(or) == false) {
233-
isInvalid.set(true);
234-
failures.add(
235-
fail(
236-
or,
237-
"Invalid condition when using METADATA _score [{}]. Full text functions can be used in an OR condition, "
238-
+ "but only if just full text functions are used in the OR condition",
239-
or.sourceText()
240-
)
241-
);
242-
}
243-
});
244-
}
245-
246-
/**
247-
* Checks if a disjunction is pushable from the point of view of FullTextFunctions. Either it has no FullTextFunctions disjunction
248-
* all it contains are FullTextFunctions.
249-
*
250-
* @param disjunction disjunction to check
251-
* @return true if the disjunction is pushable, false otherwise
252-
*/
253-
public static boolean checkDisjunctionPushable(Expression disjunction) {
254-
boolean hasFullText = disjunction.anyMatch(FullTextFunction.class::isInstance);
255-
return hasFullText == false || onlyFullTextFunctionsInExpression(disjunction);
256-
}
257-
258-
/**
259-
* Checks whether an expression contains just full text functions or negations (NOT) and combinations (AND, OR) of full text functions
260-
*
261-
* @param expression expression to check
262-
* @return true if all children are full text functions or negations of full text functions, false otherwise
263-
*/
264-
private static boolean onlyFullTextFunctionsInExpression(Expression expression) {
265-
if (expression instanceof FullTextFunction) {
266-
return true;
267-
} else if (expression instanceof Not) {
268-
return onlyFullTextFunctionsInExpression(expression.children().get(0));
269-
} else if (expression instanceof BinaryLogic binaryLogic) {
270-
return onlyFullTextFunctionsInExpression(binaryLogic.left()) && onlyFullTextFunctionsInExpression(binaryLogic.right());
271-
}
272-
273-
return false;
274-
}
275-
276215
/**
277216
* Checks all commands that exist before a specific type satisfy conditions.
278217
*

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434

3535
public abstract class BinaryLogic extends BinaryOperator<Boolean, Boolean, Boolean, BinaryLogicOperation> implements TranslationAware {
3636

37+
// Provides a way to score the result of the logical operation in case scoring is used
3738
private final BinaryScoringLogicOperation scoringFunction;
3839

3940
protected BinaryLogic(

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

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

1414
import static org.elasticsearch.compute.lucene.LuceneQueryExpressionEvaluator.SCORE_FOR_FALSE;
1515

16+
/**
17+
* Implementation of binary logic operations when scores are used
18+
*/
1619
public enum BinaryScoringLogicOperation implements PredicateBiFunction<Double, Double, Double> {
1720

1821
AND((l, r) -> {

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

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

1919
import java.util.Objects;
2020

21+
/**
22+
* Expression evaluator for boolean logic operations. To be used when scoring is not used
23+
*/
2124
public class BooleanLogicExpressionEvaluator implements EvalOperator.ExpressionEvaluator {
2225
private final BinaryLogicOperation bl;
2326
private final EvalOperator.ExpressionEvaluator leftEval;

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@
1919

2020
import java.util.Objects;
2121

22+
/**
23+
* Expression evaluator for boolean logic operations when scoring is used
24+
* TODO May merge with {@link BooleanLogicExpressionEvaluator} instead of separating into a new class
25+
*/
2226
public class BooleanScoringLogicExpressionEvaluator implements EvalOperator.ExpressionEvaluator {
2327
private final BinaryScoringLogicOperation bsl;
2428
private final EvalOperator.ExpressionEvaluator leftEval;

0 commit comments

Comments
 (0)