@@ -311,4 +311,151 @@ func TestSearchIndexProse(t *testing.T) {
311311 actual := doc .Lookup ("latestDefinition" ).Value
312312 assert .Equal (mt , expected , actual , "unmatched definition" )
313313 })
314+
315+ case7CollName , err := uuid .New ()
316+ assert .NoError (mt , err , "failed to create random collection name for case #7" )
317+
318+ mt .RunOpts ("case 7: Driver can successfully handle search index types when creating indexes" ,
319+ mtest .NewOptions ().CollectionName (case7CollName .String ()),
320+ func (mt * mtest.T ) {
321+ ctx := context .Background ()
322+
323+ _ , err := mt .Coll .InsertOne (ctx , bson.D {})
324+ require .NoError (mt , err , "failed to insert" )
325+
326+ view := mt .Coll .SearchIndexes ()
327+
328+ definition := bson.D {{"mappings" , bson.D {{"dynamic" , false }}}}
329+ indexName := "test-search-index-case7-implicit"
330+ opts := options .SearchIndexes ().SetName (indexName )
331+ index , err := view .CreateOne (ctx , mongo.SearchIndexModel {
332+ Definition : definition ,
333+ Options : opts ,
334+ })
335+ require .NoError (mt , err , "failed to create index" )
336+ require .Equal (mt , indexName , index , "unmatched name" )
337+ var doc bson.Raw
338+ for doc == nil {
339+ cursor , err := view .List (ctx , opts )
340+ require .NoError (mt , err , "failed to list" )
341+
342+ if ! cursor .Next (ctx ) {
343+ break
344+ }
345+ name := cursor .Current .Lookup ("name" ).StringValue ()
346+ queryable := cursor .Current .Lookup ("queryable" ).Boolean ()
347+ indexType := cursor .Current .Lookup ("type" ).StringValue ()
348+ if name == indexName && queryable {
349+ doc = cursor .Current
350+ assert .Equal (mt , indexType , "search" )
351+ } else {
352+ t .Logf ("cursor: %s, sleep 5 seconds..." , cursor .Current .String ())
353+ time .Sleep (5 * time .Second )
354+ }
355+ }
356+
357+ indexName = "test-search-index-case7-explicit"
358+ opts = options .SearchIndexes ().SetName (indexName ).SetType ("search" )
359+ index , err = view .CreateOne (ctx , mongo.SearchIndexModel {
360+ Definition : definition ,
361+ Options : opts ,
362+ })
363+ require .NoError (mt , err , "failed to create index" )
364+ require .Equal (mt , indexName , index , "unmatched name" )
365+ doc = nil
366+ for doc == nil {
367+ cursor , err := view .List (ctx , opts )
368+ require .NoError (mt , err , "failed to list" )
369+
370+ if ! cursor .Next (ctx ) {
371+ break
372+ }
373+ name := cursor .Current .Lookup ("name" ).StringValue ()
374+ queryable := cursor .Current .Lookup ("queryable" ).Boolean ()
375+ indexType := cursor .Current .Lookup ("type" ).StringValue ()
376+ if name == indexName && queryable {
377+ doc = cursor .Current
378+ assert .Equal (mt , indexType , "search" )
379+ } else {
380+ t .Logf ("cursor: %s, sleep 5 seconds..." , cursor .Current .String ())
381+ time .Sleep (5 * time .Second )
382+ }
383+ }
384+
385+ indexName = "test-search-index-case7-vector"
386+ type vectorDefinitionField struct {
387+ Type string `bson:"type"`
388+ Path string `bson:"path"`
389+ NumDimensions int `bson:"numDimensions"`
390+ Similarity string `bson:"similarity"`
391+ }
392+
393+ type vectorDefinition struct {
394+ Fields []vectorDefinitionField `bson:"fields"`
395+ }
396+
397+ opts = options .SearchIndexes ().SetName (indexName ).SetType ("vectorSearch" )
398+ index , err = view .CreateOne (ctx , mongo.SearchIndexModel {
399+ Definition : vectorDefinition {
400+ Fields : []vectorDefinitionField {{"vector" , "path" , 1536 , "euclidean" }},
401+ },
402+ Options : opts ,
403+ })
404+ require .NoError (mt , err , "failed to create index" )
405+ require .Equal (mt , indexName , index , "unmatched name" )
406+ doc = nil
407+ for doc == nil {
408+ cursor , err := view .List (ctx , opts )
409+ require .NoError (mt , err , "failed to list" )
410+
411+ if ! cursor .Next (ctx ) {
412+ break
413+ }
414+ name := cursor .Current .Lookup ("name" ).StringValue ()
415+ queryable := cursor .Current .Lookup ("queryable" ).Boolean ()
416+ indexType := cursor .Current .Lookup ("type" ).StringValue ()
417+ if name == indexName && queryable {
418+ doc = cursor .Current
419+ assert .Equal (mt , indexType , "vectorSearch" )
420+ } else {
421+ t .Logf ("cursor: %s, sleep 5 seconds..." , cursor .Current .String ())
422+ time .Sleep (5 * time .Second )
423+ }
424+ }
425+ })
426+
427+ case8CollName , err := uuid .New ()
428+ assert .NoError (mt , err , "failed to create random collection name for case #8" )
429+
430+ mt .RunOpts ("case 8: Driver requires explicit type to create a vector search index" ,
431+ mtest .NewOptions ().CollectionName (case8CollName .String ()),
432+ func (mt * mtest.T ) {
433+ ctx := context .Background ()
434+
435+ _ , err := mt .Coll .InsertOne (ctx , bson.D {})
436+ require .NoError (mt , err , "failed to insert" )
437+
438+ view := mt .Coll .SearchIndexes ()
439+
440+ type vectorDefinitionField struct {
441+ Type string `bson:"type"`
442+ Path string `bson:"path"`
443+ NumDimensions int `bson:"numDimensions"`
444+ Similarity string `bson:"similarity"`
445+ }
446+
447+ type vectorDefinition struct {
448+ Fields []vectorDefinitionField `bson:"fields"`
449+ }
450+
451+ const indexName = "test-search-index-case7-vector"
452+ opts := options .SearchIndexes ().SetName (indexName )
453+ _ , err = view .CreateOne (ctx , mongo.SearchIndexModel {
454+ Definition : vectorDefinition {
455+ Fields : []vectorDefinitionField {{"vector" , "plot_embedding" , 1536 , "euclidean" }},
456+ },
457+ Options : opts ,
458+ })
459+ assert .ErrorContains (mt , err , "Attribute mappings missing" )
460+ })
314461}
0 commit comments