Skip to content
Closed
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
Original file line number Diff line number Diff line change
Expand Up @@ -283,10 +283,28 @@ setup:
- match: {hits.total.value: 0}

---
"Test nonexistent field":
"Nonexistent field returns empty query":
- requires:
cluster_features: "gte_v8.4.0"
reason: 'kNN added to search endpoint in 8.4'
cluster_features: "gte_v8.16.0"
reason: 'allow unmapped field is true'
- do:
search:
index: test
body:
fields: [ "name" ]
knn:
field: nonexistent
query_vector: [ -0.5, 90.0, -10, 14.8, -156.0 ]
k: 2
num_candidates: 3

- length: {hits.hits: 0}

---
"Nonexistent field throws error":
- requires:
cluster_features: "gte_v8.16.0"
reason: 'allow unmapped field is false'
- do:
catch: bad_request
search:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

import org.apache.lucene.search.BooleanClause;
import org.apache.lucene.search.BooleanQuery;
import org.apache.lucene.search.MatchNoDocsQuery;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.join.BitSetProducer;
import org.apache.lucene.search.join.ToChildBlockJoinQuery;
Expand Down Expand Up @@ -436,7 +437,7 @@ protected Query doToQuery(SearchExecutionContext context) throws IOException {
? Math.round(Math.min(NUM_CANDS_MULTIPLICATIVE_FACTOR * requestSize, NUM_CANDS_LIMIT))
: numCands;
if (fieldType == null) {
throw new IllegalArgumentException("field [" + fieldName + "] does not exist in the mapping");
return new MatchNoDocsQuery();
}

if (fieldType instanceof DenseVectorFieldType == false) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

import org.apache.lucene.search.BooleanClause;
import org.apache.lucene.search.BooleanQuery;
import org.apache.lucene.search.MatchNoDocsQuery;
import org.apache.lucene.search.Query;
import org.elasticsearch.TransportVersion;
import org.elasticsearch.TransportVersions;
Expand All @@ -25,6 +26,7 @@
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.index.query.QueryRewriteContext;
import org.elasticsearch.index.query.QueryShardException;
import org.elasticsearch.index.query.Rewriteable;
import org.elasticsearch.index.query.SearchExecutionContext;
import org.elasticsearch.index.query.TermQueryBuilder;
Expand Down Expand Up @@ -154,13 +156,20 @@ public void testWrongDimension() {
);
}

public void testNonexistentField() {
public void testNonexistentFieldThrowsErrorWhenAllowMappingIsFalse() {
SearchExecutionContext context = createSearchExecutionContext();
context.setAllowUnmappedFields(false);
KnnVectorQueryBuilder query = new KnnVectorQueryBuilder("nonexistent", new float[] { 1.0f, 1.0f, 1.0f }, 5, 10, null);
IllegalArgumentException e = expectThrows(IllegalArgumentException.class, () -> query.doToQuery(context));
QueryShardException e = expectThrows(QueryShardException.class, () -> query.doToQuery(context));
assertThat(e.getMessage(), containsString("field [nonexistent] does not exist in the mapping"));
}

public void testNonexistentFieldDoesNotThrowsErrorWhenAllowMappingIsTrue() throws IOException {
SearchExecutionContext context = createSearchExecutionContext();
KnnVectorQueryBuilder query = new KnnVectorQueryBuilder("nonexistent", new float[] { 1.0f, 1.0f, 1.0f }, 5, 10, null);
assertThat(query.doToQuery(context), instanceOf(MatchNoDocsQuery.class));
}

public void testWrongFieldType() {
SearchExecutionContext context = createSearchExecutionContext();
KnnVectorQueryBuilder query = new KnnVectorQueryBuilder(
Expand Down