Skip to content

Conversation

astefan
Copy link
Contributor

@astefan astefan commented Mar 4, 2025

Fixes #123967 by using a must statement (vs. a filter one) when scoring is needed in a query (enabled with metadata _score).

astefan added 2 commits March 4, 2025 16:14
AND when scoring is needed; wrap the request filter with a bool filter in case the query itself doesn't need scoring.
@elasticsearchmachine elasticsearchmachine added needs:triage Requires assignment of a team area label v9.1.0 labels Mar 4, 2025
tsid = attr;
} else if (name.equals(MetadataAttribute.TIMESTAMP_FIELD)) {
timestamp = attr;

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Just refactoring to eliminate double iteration over plan.output() attributes.

Query queryDSL = TRANSLATOR_HANDLER.asQuery(Predicates.combineAnd(newPushable));
QueryBuilder planQuery = queryDSL.asBuilder();
var query = Queries.combine(Queries.Clause.FILTER, asList(queryExec.query(), planQuery));
Queries.Clause combiningQueryClauseType;
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is the actual fix for the bug. Need to add tests.

assert esQueryExec.estimatedRowSize() != null : "estimated row size not initialized";
int rowEstimatedSize = esQueryExec.estimatedRowSize();
int limit = esQueryExec.limit() != null ? (Integer) esQueryExec.limit().fold(context.foldCtx()) : NO_LIMIT;
boolean scoring = esQueryExec.attrs()
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Small refactoring.

@kingherc kingherc added the :Analytics/ES|QL AKA ESQL label Mar 4, 2025
@elasticsearchmachine elasticsearchmachine added Team:Analytics Meta label for analytical engine team (ESQL/Aggs/Geo) and removed needs:triage Requires assignment of a team area label labels Mar 4, 2025
@elasticsearchmachine
Copy link
Collaborator

Pinging @elastic/es-analytical-engine (Team:Analytics)

@astefan astefan added the >bug label Mar 4, 2025
@elasticsearchmachine
Copy link
Collaborator

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

Copy link
Member

@costin costin left a comment

Choose a reason for hiding this comment

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

LGTM.
Since ES optimizes must queries into filters, a simpler fix would be to just always use the MUST clause instead and let ES handle it accordingly.
I'm thinking of the latter since it eliminates code on our side AND in time, it might evaluate if scoring is needed then we do (by searching for the score attribute which might not be sufficient in the future).

QueryBuilder planQuery = queryDSL.asBuilder();
var query = Queries.combine(Queries.Clause.FILTER, asList(queryExec.query(), planQuery));
Queries.Clause combiningQueryClauseType;
if (queryExec.hasScoring()) {
Copy link
Member

Choose a reason for hiding this comment

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

clauseType = queryExec.hasScoring() ? MUST : FILTER

}

public boolean hasScoring() {
return attrs().stream().anyMatch(a -> a instanceof MetadataAttribute && a.name().equals(MetadataAttribute.SCORE));
Copy link
Member

Choose a reason for hiding this comment

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

Expressions.anyMatch(a -> a ..) -> not just a one liner which avoids the stream stack pollution but it also handles the expression tree navigation.
Not needed here but doesn't hurt.

Copy link
Member

@costin costin left a comment

Choose a reason for hiding this comment

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

LGTM

@astefan astefan added v8.18.1 v8.19.0 v9.0.1 v8.17.4 auto-backport Automatically create backport pull requests when merged and removed v8.17.4 labels Mar 5, 2025
@astefan astefan merged commit ecb3d21 into elastic:main Mar 5, 2025
19 checks passed
@astefan astefan deleted the 123967_fix branch March 5, 2025 16:35
@elasticsearchmachine
Copy link
Collaborator

💚 Backport successful

Status Branch Result
8.18
8.x
9.0
astefan added a commit to astefan/elasticsearch that referenced this pull request Mar 5, 2025
…coring is also needed (elastic#124001) * Use a "must" instead of "filter" when building the pushed down filter AND when scoring is needed
astefan added a commit to astefan/elasticsearch that referenced this pull request Mar 5, 2025
…coring is also needed (elastic#124001) * Use a "must" instead of "filter" when building the pushed down filter AND when scoring is needed
astefan added a commit to astefan/elasticsearch that referenced this pull request Mar 5, 2025
…coring is also needed (elastic#124001) * Use a "must" instead of "filter" when building the pushed down filter AND when scoring is needed
elasticsearchmachine pushed a commit that referenced this pull request Mar 6, 2025
…coring is also needed (#124001) (#124121) * Use a "must" instead of "filter" when building the pushed down filter AND when scoring is needed
elasticsearchmachine pushed a commit that referenced this pull request Mar 6, 2025
…coring is also needed (#124001) (#124120) * Use a "must" instead of "filter" when building the pushed down filter AND when scoring is needed
elasticsearchmachine pushed a commit that referenced this pull request Mar 6, 2025
…when scoring is also needed (#124001) (#124122) * ESQL: Use a must boolean statement when pushing down to Lucene when scoring is also needed (#124001) * Use a "must" instead of "filter" when building the pushed down filter AND when scoring is needed * Make the test deterministic
georgewallace pushed a commit to georgewallace/elasticsearch that referenced this pull request Mar 11, 2025
…coring is also needed (elastic#124001) * Use a "must" instead of "filter" when building the pushed down filter AND when scoring is needed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

:Analytics/ES|QL AKA ESQL auto-backport Automatically create backport pull requests when merged >bug Team:Analytics Meta label for analytical engine team (ESQL/Aggs/Geo) v8.18.1 v8.19.0 v9.0.1 v9.1.0

4 participants