@@ -4,7 +4,7 @@ import { Readable } from 'stream';
4
4
import { clearTimeout , setTimeout as setTimeoutCb } from 'timers' ;
5
5
import { setInterval } from 'timers/promises' ;
6
6
7
- import { type Collection , type Document , type MongoClient , ObjectId } from '../mongodb' ;
7
+ import { type Collection , type Document , type MongoClient , ObjectId , MongoServerError } from '../mongodb' ;
8
8
9
9
class TimeoutController extends AbortController {
10
10
timeoutId : NodeJS . Timeout ;
@@ -283,5 +283,157 @@ describe('Index Management Prose Tests', function () {
283
283
await collection . dropSearchIndex ( 'test-search-index' ) ;
284
284
}
285
285
) ;
286
+
287
+ it ( 'Case 7: Driver can successfully handle search index types when creating indexes' , metadata , async function ( ) {
288
+ // 01. Create a collection with the "create" command using a randomly generated name (referred to as `coll0`).
289
+ const coll0 = await client . db ( 'node-test' ) . collection ( new ObjectId ( ) . toHexString ( ) ) ;
290
+ {
291
+ // 02. Create a new search index on `coll0` with the `createSearchIndex` helper. Use the following definition:
292
+ // ```typescript
293
+ // {
294
+ // name: 'test-search-index-case7-implicit',
295
+ // definition: {
296
+ // mappings: { dynamic: false }
297
+ // }
298
+ // }
299
+ // ```
300
+ const indexName = await coll0 . createSearchIndex ( {
301
+ name : 'test-search-index-case7-implicit' ,
302
+ definition : {
303
+ mappings : { dynamic : false }
304
+ }
305
+ } )
306
+ // 03. Assert that the command returns the name of the index: `"test-search-index-case7-implicit"`.
307
+ expect ( indexName ) . to . equal ( 'test-search-index-case7-implicit' )
308
+ // 04. Run `coll0.listSearchIndexes('test-search-index-case7-implicit')` repeatedly every 5 seconds until the following
309
+ // condition is satisfied and store the value in a variable `index1`:
310
+
311
+ // - An index with the `name` of `test-search-index-case7-implicit` is present and the index has a field `queryable`
312
+ // with a value of `true`.
313
+
314
+ const [ index1 ] = await waitForIndexes ( {
315
+ predicate : indexes => indexes . every ( index => index . queryable ) ,
316
+ indexNames : 'test-search-index-case7-implicit'
317
+ } ) ;
318
+
319
+ // 05. Assert that `index1` has a property `type` whose value is `search`.
320
+ expect ( index1 ) . to . have . property ( 'type' , 'search' ) ;
321
+ }
322
+ {
323
+ // 06. Create a new search index on `coll0` with the `createSearchIndex` helper. Use the following definition:
324
+ // ```typescript
325
+ // {
326
+ // name: 'test-search-index-case7-explicit',
327
+ // type: 'search',
328
+ // definition: {
329
+ // mappings: { dynamic: false }
330
+ // }
331
+ // }
332
+ // ```
333
+ const indexName = await coll0 . createSearchIndex ( {
334
+ name : 'test-search-index-case7-explicit' ,
335
+ type : 'search' ,
336
+ definition : {
337
+ mappings : { dynamic : false }
338
+ }
339
+ }
340
+ )
341
+ // 07. Assert that the command returns the name of the index: `"test-search-index-case7-explicit"`.
342
+ expect ( indexName ) . to . equal ( 'test-search-index-case7-explicit' )
343
+ // 08. Run `coll0.listSearchIndexes('test-search-index-case7-explicit')` repeatedly every 5 seconds until the following
344
+ // condition is satisfied and store the value in a variable `index2`:
345
+
346
+ // - An index with the `name` of `test-search-index-case7-explicit` is present and the index has a field `queryable`
347
+ // with a value of `true`.
348
+
349
+ const [ index2 ] = await waitForIndexes ( {
350
+ predicate : indexes => indexes . every ( index => index . queryable ) ,
351
+ indexNames : 'test-search-index-case7-explicit'
352
+ } ) ;
353
+ // 09. Assert that `index2` has a property `type` whose value is `search`.
354
+ expect ( index2 ) . to . have . property ( 'type' , 'search' ) ;
355
+ }
356
+ {
357
+ // 10. Create a new vector search index on `coll0` with the `createSearchIndex` helper. Use the following definition:
358
+ // ```typescript
359
+ // {
360
+ // name: 'test-search-index-case7-vector',
361
+ // type: 'vectorSearch',
362
+ // definition: {
363
+ // "fields": [
364
+ // {
365
+ // "type": "vector",
366
+ // "path": "plot_embedding",
367
+ // "numDimensions": 1536,
368
+ // "similarity": "euclidean",
369
+ // },
370
+ // ]
371
+ // }
372
+ // }
373
+ // ```
374
+
375
+ const indexName = await coll0 . createSearchIndex ( {
376
+ name : 'test-search-index-case7-vector' ,
377
+ type : 'vectorSearch' ,
378
+ definition : {
379
+ "fields" : [
380
+ {
381
+ "type" : "vector" ,
382
+ "path" : "plot_embedding" ,
383
+ "numDimensions" : 1536 ,
384
+ "similarity" : "euclidean" ,
385
+ } ,
386
+ ]
387
+ }
388
+ }
389
+ )
390
+ // 11. Assert that the command returns the name of the index: `"test-search-index-case7-vector"`.
391
+ expect ( indexName ) . to . equal ( "test-search-index-case7-vector" ) ;
392
+ // 12. Run `coll0.listSearchIndexes('test-search-index-case7-vector')` repeatedly every 5 seconds until the following
393
+ // condition is satisfied and store the value in a variable `index3`:
394
+ // - An index with the `name` of `test-search-index-case7-vector` is present and the index has a field `queryable` with
395
+ // a value of `true`.
396
+ const [ index3 ] = await waitForIndexes ( {
397
+ predicate : indexes => indexes . every ( index => index . queryable ) ,
398
+ indexNames : 'test-search-index-case7-vector'
399
+ } ) ;
400
+
401
+ // 13. Assert that `index3` has a property `type` whose value is `vectorSearch`.
402
+ expect ( index3 ) . to . have . property ( 'type' , 'vectorSearch' ) ;
403
+ }
404
+ {
405
+ // 14. Create a new vector search index on `coll0` with the `createSearchIndex` helper. Use the following definition:
406
+ // ```typescript
407
+ // {
408
+ // name: 'test-search-index-case7-error',
409
+ // definition: {
410
+ // "fields": [
411
+ // {
412
+ // "type": "vector",
413
+ // "path": "plot_embedding",
414
+ // "numDimensions": 1536,
415
+ // "similarity": "euclidean",
416
+ // },
417
+ // ]
418
+ // }
419
+ // }
420
+ // ```
421
+ const error = await coll0 . createSearchIndex ( {
422
+ name : 'test-search-index-case7-error' ,
423
+ definition : {
424
+ "fields" : [
425
+ {
426
+ "type" : "vector" ,
427
+ "path" : "plot_embedding" ,
428
+ "numDimensions" : 1536 ,
429
+ "similarity" : "euclidean" ,
430
+ } ,
431
+ ]
432
+ }
433
+ } ) . catch ( e => e )
434
+ // 15. Assert that the command throws an exception due to the `mappings` field missing.
435
+ expect ( error ) . to . be . instanceOf ( MongoServerError ) . to . match ( / m a p p i n g s / i) ;
436
+ }
437
+ } )
286
438
} ) ;
287
439
} ) ;
0 commit comments