Skip to content

[8.19] Fix knn search error when dimensions are not set (#131081) #131120

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
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions docs/changelog/131081.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
pr: 131081
summary: Fix knn search error when dimensions are not set
area: Vector Search
type: bug
issues:
- 129550
Original file line number Diff line number Diff line change
Expand Up @@ -621,3 +621,36 @@ setup:
properties:
embedding:
type: dense_vector


---
"Searching with no data dimensions specified":
- requires:
cluster_features: "search.vectors.no_dimensions_bugfix"
reason: "Search with no dimensions bugfix"

- do:
indices.create:
index: empty-test
body:
mappings:
properties:
vector:
type: dense_vector
index: true

- do:
search:
index: empty-test
body:
fields: [ "name" ]
knn:
field: vector
query_vector: [ -0.5, 90.0, -10, 14.8, -156.0 ]
k: 3
num_candidates: 3
rescore_vector:
oversample: 1.5
similarity: 0.1

- match: { hits.total.value: 0 }
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import org.apache.lucene.index.VectorEncoding;
import org.apache.lucene.index.VectorSimilarityFunction;
import org.apache.lucene.search.FieldExistsQuery;
import org.apache.lucene.search.MatchNoDocsQuery;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.join.BitSetProducer;
import org.apache.lucene.util.BitUtil;
Expand Down Expand Up @@ -2216,6 +2217,9 @@ public Query createKnnQuery(
"to perform knn search on field [" + name() + "], its mapping must have [index] set to [true]"
);
}
if (dims == null) {
return new MatchNoDocsQuery("No data has been indexed for field [" + name() + "]");
}
return switch (getElementType()) {
case BYTE -> createKnnByteQuery(queryVector.asByteVector(), k, numCands, filter, similarityThreshold, parentFilter);
case FLOAT -> createKnnFloatQuery(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,11 @@ public Set<NodeFeature> getFeatures() {
public static final NodeFeature COMPLETION_FIELD_SUPPORTS_DUPLICATE_SUGGESTIONS = new NodeFeature(
"search.completion_field.duplicate.support"
);
public static final NodeFeature RESCORER_MISSING_FIELD_BAD_REQUEST = new NodeFeature("search.rescorer.missing.field.bad.request");
public static final NodeFeature INT_SORT_FOR_INT_SHORT_BYTE_FIELDS = new NodeFeature("search.sort.int_sort_for_int_short_byte_fields");
static final NodeFeature MULTI_MATCH_CHECKS_POSITIONS = new NodeFeature("search.multi.match.checks.positions");
private static final NodeFeature KNN_QUERY_BUGFIX_130254 = new NodeFeature("search.knn.query.bugfix.130254", true);
public static final NodeFeature SEARCH_WITH_NO_DIMENSIONS_BUGFIX = new NodeFeature("search.vectors.no_dimensions_bugfix");

@Override
public Set<NodeFeature> getTestFeatures() {
Expand All @@ -36,7 +38,8 @@ public Set<NodeFeature> getTestFeatures() {
COMPLETION_FIELD_SUPPORTS_DUPLICATE_SUGGESTIONS,
INT_SORT_FOR_INT_SHORT_BYTE_FIELDS,
MULTI_MATCH_CHECKS_POSITIONS,
KNN_QUERY_BUGFIX_130254
KNN_QUERY_BUGFIX_130254,
SEARCH_WITH_NO_DIMENSIONS_BUGFIX
);
}
}