|
39 | 39 | import org.opensearch.plugins.SearchPlugin;
|
40 | 40 |
|
41 | 41 | import java.io.IOException;
|
| 42 | +import java.util.ArrayList; |
42 | 43 | import java.util.List;
|
43 | 44 | import java.util.Optional;
|
44 | 45 |
|
@@ -149,6 +150,70 @@ public void testFromXcontent_WithFilter_UnsupportedClusterVersion() throws Excep
|
149 | 150 | expectThrows(IllegalArgumentException.class, () -> KNNQueryBuilder.fromXContent(contentParser));
|
150 | 151 | }
|
151 | 152 |
|
| 153 | + public void testFromXContent_invalidQueryVectorType() throws Exception { |
| 154 | + final ClusterService clusterService = mockClusterService(Version.CURRENT); |
| 155 | + |
| 156 | + final KNNClusterUtil knnClusterUtil = KNNClusterUtil.instance(); |
| 157 | + knnClusterUtil.initialize(clusterService); |
| 158 | + |
| 159 | + List<Object> invalidTypeQueryVector = new ArrayList<>(); |
| 160 | + invalidTypeQueryVector.add(1.5); |
| 161 | + invalidTypeQueryVector.add(2.5); |
| 162 | + invalidTypeQueryVector.add("a"); |
| 163 | + invalidTypeQueryVector.add(null); |
| 164 | + |
| 165 | + XContentBuilder builder = XContentFactory.jsonBuilder(); |
| 166 | + builder.startObject(); |
| 167 | + builder.startObject(FIELD_NAME); |
| 168 | + builder.field(KNNQueryBuilder.VECTOR_FIELD.getPreferredName(), invalidTypeQueryVector); |
| 169 | + builder.field(KNNQueryBuilder.K_FIELD.getPreferredName(), K); |
| 170 | + builder.endObject(); |
| 171 | + builder.endObject(); |
| 172 | + XContentParser contentParser = createParser(builder); |
| 173 | + contentParser.nextToken(); |
| 174 | + IllegalArgumentException exception = expectThrows( |
| 175 | + IllegalArgumentException.class, |
| 176 | + () -> KNNQueryBuilder.fromXContent(contentParser) |
| 177 | + ); |
| 178 | + assertTrue(exception.getMessage().contains("[knn] field 'vector' requires to be an array of numbers")); |
| 179 | + } |
| 180 | + |
| 181 | + public void testFromXContent_missingQueryVector() throws Exception { |
| 182 | + final ClusterService clusterService = mockClusterService(Version.CURRENT); |
| 183 | + |
| 184 | + final KNNClusterUtil knnClusterUtil = KNNClusterUtil.instance(); |
| 185 | + knnClusterUtil.initialize(clusterService); |
| 186 | + |
| 187 | + // Test without vector field |
| 188 | + XContentBuilder builderWithoutVectorField = XContentFactory.jsonBuilder(); |
| 189 | + builderWithoutVectorField.startObject(); |
| 190 | + builderWithoutVectorField.startObject(FIELD_NAME); |
| 191 | + builderWithoutVectorField.field(KNNQueryBuilder.K_FIELD.getPreferredName(), K); |
| 192 | + builderWithoutVectorField.endObject(); |
| 193 | + builderWithoutVectorField.endObject(); |
| 194 | + XContentParser contentParserWithoutVectorField = createParser(builderWithoutVectorField); |
| 195 | + contentParserWithoutVectorField.nextToken(); |
| 196 | + IllegalArgumentException exception = expectThrows( |
| 197 | + IllegalArgumentException.class, |
| 198 | + () -> KNNQueryBuilder.fromXContent(contentParserWithoutVectorField) |
| 199 | + ); |
| 200 | + assertTrue(exception.getMessage().contains("[knn] field 'vector' requires to be non-null and non-empty")); |
| 201 | + |
| 202 | + // Test empty vector field |
| 203 | + List<Object> emptyQueryVector = new ArrayList<>(); |
| 204 | + XContentBuilder builderWithEmptyVector = XContentFactory.jsonBuilder(); |
| 205 | + builderWithEmptyVector.startObject(); |
| 206 | + builderWithEmptyVector.startObject(FIELD_NAME); |
| 207 | + builderWithEmptyVector.field(KNNQueryBuilder.VECTOR_FIELD.getPreferredName(), emptyQueryVector); |
| 208 | + builderWithEmptyVector.field(KNNQueryBuilder.K_FIELD.getPreferredName(), K); |
| 209 | + builderWithEmptyVector.endObject(); |
| 210 | + builderWithEmptyVector.endObject(); |
| 211 | + XContentParser contentParserWithEmptyVector = createParser(builderWithEmptyVector); |
| 212 | + contentParserWithEmptyVector.nextToken(); |
| 213 | + exception = expectThrows(IllegalArgumentException.class, () -> KNNQueryBuilder.fromXContent(contentParserWithEmptyVector)); |
| 214 | + assertTrue(exception.getMessage().contains("[knn] field 'vector' requires to be non-null and non-empty")); |
| 215 | + } |
| 216 | + |
152 | 217 | @Override
|
153 | 218 | protected NamedXContentRegistry xContentRegistry() {
|
154 | 219 | List<NamedXContentRegistry.Entry> list = ClusterModule.getNamedXWriteables();
|
|
0 commit comments