3737import java .util .Map ;
3838
3939import static java .util .Collections .emptyList ;
40+ import static org .elasticsearch .index .mapper .vectors .DenseVectorFieldMapper .IVF_FORMAT ;
4041import static org .elasticsearch .search .RandomSearchRequestGenerator .randomSearchSourceBuilder ;
4142import static org .hamcrest .Matchers .containsString ;
4243
@@ -111,7 +112,7 @@ public void testParseSourceString() throws IOException {
111112 .field (KnnSearch .FIELD_FIELD .getPreferredName (), knnSearch .field )
112113 .field (KnnSearch .K_FIELD .getPreferredName (), knnSearch .k )
113114 .field (KnnSearch .NUM_CANDS_FIELD .getPreferredName (), knnSearch .numCands );
114- if (knnSearch .visitPercentage != null ) {
115+ if (IVF_FORMAT . isEnabled () && knnSearch .visitPercentage != null ) {
115116 builder .field (KnnSearch .VISIT_PERCENTAGE_FIELD .getPreferredName (), knnSearch .visitPercentage );
116117 }
117118 builder .field (KnnSearch .QUERY_VECTOR_FIELD .getPreferredName (), knnSearch .queryVector );
@@ -140,7 +141,7 @@ public void testParseSourceArray() throws IOException {
140141 .field (KnnSearch .FIELD_FIELD .getPreferredName (), knnSearch .field )
141142 .field (KnnSearch .K_FIELD .getPreferredName (), knnSearch .k )
142143 .field (KnnSearch .NUM_CANDS_FIELD .getPreferredName (), knnSearch .numCands );
143- if (knnSearch .visitPercentage != null ) {
144+ if (IVF_FORMAT . isEnabled () && knnSearch .visitPercentage != null ) {
144145 builder .field (KnnSearch .VISIT_PERCENTAGE_FIELD .getPreferredName (), knnSearch .visitPercentage );
145146 }
146147 builder .field (KnnSearch .QUERY_VECTOR_FIELD .getPreferredName (), knnSearch .queryVector );
@@ -176,11 +177,11 @@ public void testNumCandsLessThanK() throws IOException {
176177 .startObject (KnnSearchRequestParser .KNN_SECTION_FIELD .getPreferredName ())
177178 .field (KnnSearch .FIELD_FIELD .getPreferredName (), "field" )
178179 .field (KnnSearch .K_FIELD .getPreferredName (), 100 )
179- .field (KnnSearch .NUM_CANDS_FIELD .getPreferredName (), 80 )
180- . field ( KnnSearch . VISIT_PERCENTAGE_FIELD . getPreferredName (), 100.0f )
181- .field (KnnSearch .QUERY_VECTOR_FIELD .getPreferredName (), new float [] { 1 .0f, 2.0f , 3.0f })
182- . endObject ()
183- .endObject ();
180+ .field (KnnSearch .NUM_CANDS_FIELD .getPreferredName (), 80 );
181+ if ( IVF_FORMAT . isEnabled ()) {
182+ builder .field (KnnSearch .VISIT_PERCENTAGE_FIELD .getPreferredName (), 100 .0f);
183+ }
184+ builder . field ( KnnSearch . QUERY_VECTOR_FIELD . getPreferredName (), new float [] { 1.0f , 2.0f , 3.0f }). endObject () .endObject ();
184185
185186 IllegalArgumentException e = expectThrows (IllegalArgumentException .class , () -> parseSearchRequest (builder ));
186187 assertThat (e .getMessage (), containsString ("[num_candidates] cannot be less than [k]" ));
@@ -193,17 +194,18 @@ public void testNumCandsExceedsLimit() throws IOException {
193194 .startObject (KnnSearchRequestParser .KNN_SECTION_FIELD .getPreferredName ())
194195 .field (KnnSearch .FIELD_FIELD .getPreferredName (), "field" )
195196 .field (KnnSearch .K_FIELD .getPreferredName (), 100 )
196- .field (KnnSearch .NUM_CANDS_FIELD .getPreferredName (), 10002 )
197- . field ( KnnSearch . VISIT_PERCENTAGE_FIELD . getPreferredName (), 100.0f )
198- .field (KnnSearch .QUERY_VECTOR_FIELD .getPreferredName (), new float [] { 1 .0f, 2.0f , 3.0f })
199- . endObject ()
200- .endObject ();
197+ .field (KnnSearch .NUM_CANDS_FIELD .getPreferredName (), 10002 );
198+ if ( IVF_FORMAT . isEnabled ()) {
199+ builder .field (KnnSearch .VISIT_PERCENTAGE_FIELD .getPreferredName (), 100 .0f);
200+ }
201+ builder . field ( KnnSearch . QUERY_VECTOR_FIELD . getPreferredName (), new float [] { 1.0f , 2.0f , 3.0f }). endObject () .endObject ();
201202
202203 IllegalArgumentException e = expectThrows (IllegalArgumentException .class , () -> parseSearchRequest (builder ));
203204 assertThat (e .getMessage (), containsString ("[num_candidates] cannot exceed [10000]" ));
204205 }
205206
206207 public void testVisitPercnetageLessThan0 () throws IOException {
208+ assumeTrue ("visit_percentage is only supported if IVF format is enabled" , IVF_FORMAT .isEnabled ());
207209 XContentType xContentType = randomFrom (XContentType .values ());
208210 XContentBuilder builder = XContentBuilder .builder (xContentType .xContent ())
209211 .startObject ()
@@ -220,7 +222,8 @@ public void testVisitPercnetageLessThan0() throws IOException {
220222 assertThat (e .getMessage (), containsString ("[visit_percentage] must be between 0 and 100" ));
221223 }
222224
223- public void testVisitPercnetageGreaterThan100 () throws IOException {
225+ public void testVisitPercentageGreaterThan100 () throws IOException {
226+ assumeTrue ("visit_percentage is only supported if IVF format is enabled" , IVF_FORMAT .isEnabled ());
224227 XContentType xContentType = randomFrom (XContentType .values ());
225228 XContentBuilder builder = XContentBuilder .builder (xContentType .xContent ())
226229 .startObject ()
@@ -244,11 +247,11 @@ public void testInvalidK() throws IOException {
244247 .startObject (KnnSearchRequestParser .KNN_SECTION_FIELD .getPreferredName ())
245248 .field (KnnSearch .FIELD_FIELD .getPreferredName (), "field" )
246249 .field (KnnSearch .K_FIELD .getPreferredName (), 0 )
247- .field (KnnSearch .NUM_CANDS_FIELD .getPreferredName (), 10 )
248- . field ( KnnSearch . VISIT_PERCENTAGE_FIELD . getPreferredName (), 100.0f )
249- .field (KnnSearch .QUERY_VECTOR_FIELD .getPreferredName (), new float [] { 1 .0f, 2.0f , 3.0f })
250- . endObject ()
251- .endObject ();
250+ .field (KnnSearch .NUM_CANDS_FIELD .getPreferredName (), 10 );
251+ if ( IVF_FORMAT . isEnabled ()) {
252+ builder .field (KnnSearch .VISIT_PERCENTAGE_FIELD .getPreferredName (), 100 .0f);
253+ }
254+ builder . field ( KnnSearch . QUERY_VECTOR_FIELD . getPreferredName (), new float [] { 1.0f , 2.0f , 3.0f }). endObject () .endObject ();
252255
253256 IllegalArgumentException e = expectThrows (IllegalArgumentException .class , () -> parseSearchRequest (builder ));
254257 assertThat (e .getMessage (), containsString ("[k] must be greater than 0" ));
@@ -281,7 +284,7 @@ private KnnSearch randomKnnSearch() {
281284
282285 int k = randomIntBetween (1 , 100 );
283286 int numCands = randomIntBetween (k , 1000 );
284- Float visitPercentage = randomBoolean () ? null : randomFloatBetween (0.0f , 100.0f , true );
287+ Float visitPercentage = ( IVF_FORMAT . isEnabled () == false && randomBoolean () ) ? null : randomFloatBetween (0.0f , 100.0f , true );
285288 return new KnnSearch (field , vector , k , numCands , visitPercentage );
286289 }
287290
0 commit comments