Skip to content

[PoC] ESQL - Add scoring for full text functions disjunctions#121153

Closed
carlosdelest wants to merge 44 commits intoelastic:mainfrom
carlosdelest:non-issue/esql-scoring-full-text-functions-disjunctions
Closed

[PoC] ESQL - Add scoring for full text functions disjunctions#121153
carlosdelest wants to merge 44 commits intoelastic:mainfrom
carlosdelest:non-issue/esql-scoring-full-text-functions-disjunctions

Conversation

@carlosdelest
Copy link
Copy Markdown
Member

@carlosdelest carlosdelest commented Jan 29, 2025

See another approach in #121322

#120291 added support for using full text functions in disjunctions, by using the LuceneQueryExpressionEvaluator to evaluate queries that couldn't be pushed down to Lucene at the compute engine level.

The LuceneQueryExpressionEvaluator runs the query and outputs a BooleanVector with true or false depending on whether the docs received as input have been identified as a result of the query.

This provides no support for scoring, but we could adapt the LuceneQueryExpressionEvaluator behaviour when scoring is used, so the query run in collects the scores and the evaluator could return a DoubleVector with the scores:

LuceneQueryExpressionEvaluator and scoring

A special negative value is returned for docs that do not match the query, so they can be later identified even if we apply boosting to the query.

The FilterOperator will then receive a DoubleVector instead of a BooleanVector. It can check whether scoring is being used, and thus filter a DoubleVector for non-negative scores.

It also will change the score block that was retrieved from Lucene, so it will use the scores retrieved from the query as the new score block. This way the _score metadata will be updated.

LuceneQueryExpressionEvaluator and scoring (3)

The problem is how to combine the result of the LuceneQueryExpressionEvaluator with other conditions via boolean operators like AND, OR, NOT.

There are new versions of operator evaluators that work with DoubleVector, so boolean operations can combine scores:

  • AND sums the scores when both scores are positive, otherwise returns a negative score
  • OR sums the scores when one of them is positive, otherwise returns a negative score
  • NOT returns 0 when score is positive, a negative score otherwise

As there will be non-scoring boolean expressions, we need to convert a BooleanVector to a DoubleVector so results can be correctly computed. A BooleanToScoringExpressionEvaluator class is used to wrap evaluators used as input on conditional expressions, so they convert booleans to constant scores. They are a passthrough in case a DoubleVector of scores is already provided.

As logic for boolean logic was inlined into EvalMapper, two classes have been created to run scoring and non-scoring logic: BooleanLogicExpressionEvaluator and BooleanScoringLogicExpressionEvaluator.

Untitled drawing

This allows combining both scored queries with boolean conditions, that will be evaluated as constant queries.

carlosdelest and others added 30 commits January 16, 2025 09:10
…unctions-disjunctions' into non-issue/esql-full-text-functions-disjunctions
# Conflicts:
#	x-pack/plugin/esql/src/main/java/org/elasticsearch/xpack/esql/expression/function/fulltext/FullTextFunction.java
#	x-pack/plugin/esql/src/main/java/org/elasticsearch/xpack/esql/optimizer/rules/physical/local/PushFiltersToSource.java
…unctions-disjunctions' into non-issue/esql-full-text-functions-disjunctions
…unctions-disjunctions' into non-issue/esql-full-text-functions-disjunctions
@github-actions
Copy link
Copy Markdown
Contributor

Documentation preview:

@carlosdelest carlosdelest added >enhancement :Analytics/ES|QL AKA ESQL Team:Search Relevance Meta label for the Search Relevance team in Elasticsearch labels Jan 29, 2025
@elasticsearchmachine
Copy link
Copy Markdown
Collaborator

Hi @carlosdelest, I've created a changelog YAML for you.

carlosdelest and others added 5 commits January 29, 2025 12:18
…g-full-text-functions-disjunctions

# Conflicts:
#	docs/reference/esql/esql-limitations.asciidoc
#	x-pack/plugin/build.gradle
#	x-pack/plugin/esql/compute/src/main/java/org/elasticsearch/compute/lucene/LuceneQueryExpressionEvaluator.java
#	x-pack/plugin/esql/compute/src/test/java/org/elasticsearch/compute/lucene/LuceneQueryExpressionEvaluatorTests.java
#	x-pack/plugin/esql/qa/testFixtures/src/main/resources/match-function.csv-spec
#	x-pack/plugin/esql/src/main/java/org/elasticsearch/xpack/esql/action/EsqlCapabilities.java
#	x-pack/plugin/esql/src/main/java/org/elasticsearch/xpack/esql/evaluator/EvalMapper.java
#	x-pack/plugin/esql/src/main/java/org/elasticsearch/xpack/esql/evaluator/mapper/EvaluatorMapper.java
#	x-pack/plugin/esql/src/main/java/org/elasticsearch/xpack/esql/evaluator/mapper/ExpressionMapper.java
#	x-pack/plugin/esql/src/main/java/org/elasticsearch/xpack/esql/expression/function/fulltext/FullTextFunction.java
#	x-pack/plugin/esql/src/main/java/org/elasticsearch/xpack/esql/expression/predicate/operator/comparison/InsensitiveEqualsMapper.java
#	x-pack/plugin/esql/src/main/java/org/elasticsearch/xpack/esql/planner/LocalExecutionPlanner.java
#	x-pack/plugin/esql/src/test/java/org/elasticsearch/xpack/esql/optimizer/LocalPhysicalPlanOptimizerTests.java
…l-text-functions-disjunctions' into non-issue/esql-scoring-full-text-functions-disjunctions

static class BooleanLogic extends ExpressionMapper<BinaryLogic> {
@Override
public ExpressionEvaluator.Factory map(FoldContext foldCtx, BinaryLogic bc, Layout layout, List<ShardContext> shardContexts) {
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Refactored this logic to BooleanLogicExpressionEvaluator

@carlosdelest carlosdelest changed the title ESQL - Add scoring for full text functions disjunctions [PoC] ESQL - Add scoring for full text functions disjunctions Jan 31, 2025
@carlosdelest
Copy link
Copy Markdown
Member Author

Closing this in favour of #121551

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

:Analytics/ES|QL AKA ESQL >enhancement Team:Search Relevance Meta label for the Search Relevance team in Elasticsearch v9.1.0

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants