Skip to content

Commit 033a88a

Browse files
Dummy commit
1 parent df24e4f commit 033a88a

File tree

3 files changed

+35
-7
lines changed

3 files changed

+35
-7
lines changed

rest-api-spec/src/yamlRestTest/resources/rest-api-spec/test/search.vectors/40_knn_search.yml

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -283,10 +283,28 @@ setup:
283283
- match: {hits.total.value: 0}
284284

285285
---
286-
"Test nonexistent field":
286+
"Nonexistent field returns empty query":
287287
- requires:
288-
cluster_features: "gte_v8.4.0"
289-
reason: 'kNN added to search endpoint in 8.4'
288+
cluster_features: "gte_v8.16.0" // what will happen if we go to 9.0 from 8.15
289+
reason: 'allow unmapped field is true'
290+
- do:
291+
search:
292+
index: test
293+
body:
294+
fields: [ "name" ]
295+
knn:
296+
field: nonexistent
297+
query_vector: [ -0.5, 90.0, -10, 14.8, -156.0 ]
298+
k: 2
299+
num_candidates: 3
300+
301+
- length: {hits.hits: 0}
302+
303+
---
304+
"Nonexistent field throws error":
305+
- requires:
306+
cluster_features: "gte_v8.16.0"
307+
reason: 'allow unmapped field is false'
290308
- do:
291309
catch: bad_request
292310
search:
@@ -298,7 +316,7 @@ setup:
298316
query_vector: [ -0.5, 90.0, -10, 14.8, -156.0 ]
299317
k: 2
300318
num_candidates: 3
301-
- match: { error.root_cause.0.type: "query_shard_exception" }
319+
- match: { error.root_cause.0.type: "query_shard_exception" } // how does it have query shard exception where as the unit test had IllegalArgumentException
302320
- match: { error.root_cause.0.reason: "failed to create query: field [nonexistent] does not exist in the mapping" }
303321

304322
---

server/src/main/java/org/elasticsearch/search/vectors/KnnVectorQueryBuilder.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
import org.apache.lucene.search.BooleanClause;
1212
import org.apache.lucene.search.BooleanQuery;
13+
import org.apache.lucene.search.MatchNoDocsQuery;
1314
import org.apache.lucene.search.Query;
1415
import org.apache.lucene.search.join.BitSetProducer;
1516
import org.apache.lucene.search.join.ToChildBlockJoinQuery;
@@ -436,7 +437,7 @@ protected Query doToQuery(SearchExecutionContext context) throws IOException {
436437
? Math.round(Math.min(NUM_CANDS_MULTIPLICATIVE_FACTOR * requestSize, NUM_CANDS_LIMIT))
437438
: numCands;
438439
if (fieldType == null) {
439-
throw new IllegalArgumentException("field [" + fieldName + "] does not exist in the mapping");
440+
return new MatchNoDocsQuery();
440441
}
441442

442443
if (fieldType instanceof DenseVectorFieldType == false) {

server/src/test/java/org/elasticsearch/search/vectors/AbstractKnnVectorQueryBuilderTestCase.java

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
import org.apache.lucene.search.BooleanClause;
1212
import org.apache.lucene.search.BooleanQuery;
13+
import org.apache.lucene.search.MatchNoDocsQuery;
1314
import org.apache.lucene.search.Query;
1415
import org.elasticsearch.TransportVersion;
1516
import org.elasticsearch.TransportVersions;
@@ -25,6 +26,7 @@
2526
import org.elasticsearch.index.query.QueryBuilder;
2627
import org.elasticsearch.index.query.QueryBuilders;
2728
import org.elasticsearch.index.query.QueryRewriteContext;
29+
import org.elasticsearch.index.query.QueryShardException;
2830
import org.elasticsearch.index.query.Rewriteable;
2931
import org.elasticsearch.index.query.SearchExecutionContext;
3032
import org.elasticsearch.index.query.TermQueryBuilder;
@@ -154,13 +156,20 @@ public void testWrongDimension() {
154156
);
155157
}
156158

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

167+
public void testNonexistentFieldDoesNotThrowsErrorWhenAllowMappingIsTrue() throws IOException {
168+
SearchExecutionContext context = createSearchExecutionContext();
169+
KnnVectorQueryBuilder query = new KnnVectorQueryBuilder("nonexistent", new float[] { 1.0f, 1.0f, 1.0f }, 5, 10, null);
170+
assertThat(query.doToQuery(context), instanceOf(MatchNoDocsQuery.class));
171+
}
172+
164173
public void testWrongFieldType() {
165174
SearchExecutionContext context = createSearchExecutionContext();
166175
KnnVectorQueryBuilder query = new KnnVectorQueryBuilder(

0 commit comments

Comments
 (0)