Skip to content

Conversation

benwtrent
Copy link
Member

Since dfs kNN searches aren't in the query phase, we don't get their search stats for free in query stats.

This adds their stats specifically during knn search in dfs.

closes: #128098

@elasticsearchmachine elasticsearchmachine added the Team:Search Relevance Meta label for the Search Relevance team in Elasticsearch label Aug 7, 2025
@elasticsearchmachine
Copy link
Collaborator

Pinging @elastic/es-search-relevance (Team:Search Relevance)

@elasticsearchmachine
Copy link
Collaborator

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

…rent/elasticsearch into bugfix/knn-account-for-query-stats
Copy link
Member

@carlosdelest carlosdelest left a comment

Choose a reason for hiding this comment

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

The change LGTM 👍

A query with both knn section and a query will have multiple onPreQueryPhase / onQueryPhase calls. I wonder if we should have a different listener method for it, so we can tell apart the knn section from other queries?

@benwtrent
Copy link
Member Author

@carlosdelest I went and checked the logic for the onPreQueryPhase

    public void onPreQueryPhase(SearchContext searchContext) {
        computeStats(
            searchContext,
            searchContext.hasOnlySuggest() ? statsHolder -> statsHolder.suggestCurrent.inc() : statsHolder -> statsHolder.queryCurrent.inc()
        );
    }

It looks like (for knn) it would only call statsHolder.queryCurrent.inc() which is then adjusted later in onQueryPhase to decrement the query being done.

However, I did notice that if there was a failure, we might not actually decrement this correctly as we don't catch and then call onFailedQueryPhase (which also decrements the counter)

I will add a try/finally here to make sure we decrement.

@benwtrent benwtrent added the auto-merge-without-approval Automatically merge pull request when CI checks pass (NB doesn't wait for reviews!) label Aug 8, 2025
@carlosdelest
Copy link
Member

I will add a try/finally here to make sure we decrement.

Looks good! 👍

@elasticsearchmachine elasticsearchmachine merged commit c19dc0e into elastic:main Aug 8, 2025
33 checks passed
@benwtrent benwtrent deleted the bugfix/knn-account-for-query-stats branch August 8, 2025 15:28
@stures
Copy link

stures commented Sep 4, 2025

Sorry for waking this up, I see this is labeled 9.2.0, is this or will fix this be added to a minor fix of the 8.x major version? Or do we need to upgrade to 9 to get this fix?

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

Labels

auto-merge-without-approval Automatically merge pull request when CI checks pass (NB doesn't wait for reviews!) >bug :Search Relevance/Vectors Vector search Team:Search Relevance Meta label for the Search Relevance team in Elasticsearch v9.2.0

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Index stat metrics are not including queries using knn level

4 participants