Skip to content

Fix knn search error when dimensions are not set #131081

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged

Conversation

carlosdelest
Copy link
Member

A KNN search that uses similarity when no dimensions are set for a dense_vector field caused a NPE when calculating similarity:

            if (similarityThreshold != null) {
                knnQuery = new VectorSimilarityQuery(
                    knnQuery,
                    similarityThreshold,
                    similarity.score(similarityThreshold, elementType, dims)
                );
            }

Having no dimensions set on a dense_vector field means that no data has been ingested yet, so we can shortcut to use a MatchNoDocsQuery in this case.

Closes #129550

@carlosdelest carlosdelest added >bug auto-backport Automatically create backport pull requests when merged :Search Relevance/Vectors Vector search v8.9.1 v8.9.2 Team:Search Relevance Meta label for the Search Relevance team in Elasticsearch v8.19.0 v8.18.5 labels Jul 11, 2025
@elasticsearchmachine
Copy link
Collaborator

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

@carlosdelest carlosdelest marked this pull request as ready for review July 11, 2025 13:25
@elasticsearchmachine
Copy link
Collaborator

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

@carlosdelest carlosdelest requested a review from a team July 11, 2025 14:05
Copy link
Contributor

@john-wagster john-wagster left a comment

Choose a reason for hiding this comment

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

lgtm

@carlosdelest carlosdelest merged commit bbc4cef into elastic:main Jul 11, 2025
33 checks passed
@elasticsearchmachine
Copy link
Collaborator

💔 Backport failed

Status Branch Result
8.9 Commit could not be cherrypicked due to conflicts
8.19 Commit could not be cherrypicked due to conflicts
8.18 Commit could not be cherrypicked due to conflicts

You can use sqren/backport to manually backport by running backport --upstream elastic/elasticsearch --pr 131081

carlosdelest added a commit to carlosdelest/elasticsearch that referenced this pull request Jul 11, 2025
* Unmute tests muted in elastic#129550 as they were fixed in elastic#127322

* Return no match when no dimensions have been set

* Add tests

* Update docs/changelog/131081.yaml

(cherry picked from commit bbc4cef)

# Conflicts:
#	server/src/main/java/org/elasticsearch/index/mapper/vectors/DenseVectorFieldMapper.java
#	x-pack/plugin/esql/qa/testFixtures/src/main/resources/knn-function.csv-spec
carlosdelest added a commit to carlosdelest/elasticsearch that referenced this pull request Jul 11, 2025
* Unmute tests muted in elastic#129550 as they were fixed in elastic#127322

* Return no match when no dimensions have been set

* Add tests

* Update docs/changelog/131081.yaml

(cherry picked from commit bbc4cef)

# Conflicts:
#	server/src/main/java/org/elasticsearch/index/mapper/vectors/DenseVectorFieldMapper.java
#	server/src/main/java/org/elasticsearch/search/SearchFeatures.java
#	x-pack/plugin/esql/qa/testFixtures/src/main/resources/knn-function.csv-spec
carlosdelest added a commit to carlosdelest/elasticsearch that referenced this pull request Jul 11, 2025
* Unmute tests muted in elastic#129550 as they were fixed in elastic#127322

* Return no match when no dimensions have been set

* Add tests

* Update docs/changelog/131081.yaml

(cherry picked from commit bbc4cef)

# Conflicts:
#	server/src/main/java/org/elasticsearch/index/mapper/vectors/DenseVectorFieldMapper.java
#	server/src/main/java/org/elasticsearch/search/SearchFeatures.java
#	x-pack/plugin/esql/qa/testFixtures/src/main/resources/knn-function.csv-spec
@carlosdelest
Copy link
Member Author

💚 All backports created successfully

Status Branch Result
9.1
9.0
8.19
8.18

Questions ?

Please refer to the Backport tool documentation

carlosdelest added a commit to carlosdelest/elasticsearch that referenced this pull request Jul 11, 2025
* Unmute tests muted in elastic#129550 as they were fixed in elastic#127322

* Return no match when no dimensions have been set

* Add tests

* Update docs/changelog/131081.yaml

(cherry picked from commit bbc4cef)

# Conflicts:
#	server/src/main/java/org/elasticsearch/index/mapper/vectors/DenseVectorFieldMapper.java
#	server/src/main/java/org/elasticsearch/search/SearchFeatures.java
#	x-pack/plugin/esql/qa/testFixtures/src/main/resources/knn-function.csv-spec
elasticsearchmachine pushed a commit that referenced this pull request Jul 14, 2025
* Unmute tests muted in #129550 as they were fixed in #127322

* Return no match when no dimensions have been set

* Add tests

* Update docs/changelog/131081.yaml

(cherry picked from commit bbc4cef)

# Conflicts:
#	server/src/main/java/org/elasticsearch/index/mapper/vectors/DenseVectorFieldMapper.java
#	x-pack/plugin/esql/qa/testFixtures/src/main/resources/knn-function.csv-spec
elasticsearchmachine pushed a commit that referenced this pull request Jul 14, 2025
* Unmute tests muted in #129550 as they were fixed in #127322

* Return no match when no dimensions have been set

* Add tests

* Update docs/changelog/131081.yaml

(cherry picked from commit bbc4cef)

# Conflicts:
#	server/src/main/java/org/elasticsearch/index/mapper/vectors/DenseVectorFieldMapper.java
#	server/src/main/java/org/elasticsearch/search/SearchFeatures.java
#	x-pack/plugin/esql/qa/testFixtures/src/main/resources/knn-function.csv-spec
mridula-s109 pushed a commit to mridula-s109/elasticsearch that referenced this pull request Jul 17, 2025
* Unmute tests muted in elastic#129550 as they were fixed in elastic#127322

* Return no match when no dimensions have been set

* Add tests

* Update docs/changelog/131081.yaml
mridula-s109 pushed a commit to mridula-s109/elasticsearch that referenced this pull request Jul 17, 2025
* Unmute tests muted in elastic#129550 as they were fixed in elastic#127322

* Return no match when no dimensions have been set

* Add tests

* Update docs/changelog/131081.yaml
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
auto-backport Automatically create backport pull requests when merged backport pending >bug :Search Relevance/Vectors Vector search Team:Search Relevance Meta label for the Search Relevance team in Elasticsearch v8.9.1 v8.9.2 v8.18.5 v8.19.0 v9.2.0
Projects
None yet
Development

Successfully merging this pull request may close these issues.

ESQL: partial failure in few KNN tests
4 participants