88use Illuminate \Support \Facades \Schema ;
99use MongoDB \BSON \Binary ;
1010use MongoDB \BSON \UTCDateTime ;
11+ use MongoDB \Collection ;
1112use MongoDB \Laravel \Schema \Blueprint ;
1213
14+ use function assert ;
1315use function collect ;
1416use function count ;
1517
@@ -502,9 +504,51 @@ public function testGetIndexes()
502504 $ this ->assertSame ([], $ indexes );
503505 }
504506
507+ /** @todo requires SearchIndex support */
508+ public function testSearchIndex (): void
509+ {
510+ Schema::create ('newcollection ' , function (Blueprint $ collection ) {
511+ $ collection ->searchIndex ([
512+ 'mappings ' => [
513+ 'dynamic ' => false ,
514+ 'fields ' => [
515+ 'foo ' => ['type ' => 'string ' , 'analyzer ' => 'lucene.whitespace ' ],
516+ ],
517+ ],
518+ ]);
519+ });
520+
521+ $ index = $ this ->getSearchIndex ('newcollection ' , 'default ' );
522+ self ::assertNotFalse ($ index );
523+
524+ self ::assertSame ('default ' , $ index ['name ' ]);
525+ self ::assertSame ('search ' , $ index ['type ' ]);
526+ self ::assertFalse ($ index ['latestDefinition ' ]['mappings ' ]['dynamic ' ]);
527+ self ::assertSame ('lucene.whitespace ' , $ index ['latestDefinition ' ]['mappings ' ]['fields ' ]['foo ' ]['analyzer ' ]);
528+ }
529+
530+ public function testVectorSearchIndex ()
531+ {
532+ Schema::create ('newcollection ' , function (Blueprint $ collection ) {
533+ $ collection ->vectorSearchIndex ([
534+ 'fields ' => [
535+ ['type ' => 'vector ' , 'path ' => 'foo ' , 'numDimensions ' => 128 , 'similarity ' => 'euclidean ' , 'quantization ' => 'none ' ],
536+ ],
537+ ]);
538+ });
539+
540+ $ index = $ this ->getSearchIndex ('newcollection ' , 'vector ' );
541+ self ::assertNotFalse ($ index );
542+
543+ self ::assertSame ('vector ' , $ index ['name ' ]);
544+ self ::assertSame ('vectorSearch ' , $ index ['type ' ]);
545+ self ::assertSame ('vector ' , $ index ['latestDefinition ' ]['fields ' ][0 ]['type ' ]);
546+ }
547+
505548 protected function getIndex (string $ collection , string $ name )
506549 {
507550 $ collection = DB ::getCollection ($ collection );
551+ assert ($ collection instanceof Collection);
508552
509553 foreach ($ collection ->listIndexes () as $ index ) {
510554 if (isset ($ index ['key ' ][$ name ])) {
@@ -514,4 +558,16 @@ protected function getIndex(string $collection, string $name)
514558
515559 return false ;
516560 }
561+
562+ protected function getSearchIndex (string $ collection , string $ name )
563+ {
564+ $ collection = DB ::getCollection ($ collection );
565+ assert ($ collection instanceof Collection);
566+
567+ foreach ($ collection ->listSearchIndexes (['name ' => $ name ]) as $ index ) {
568+ return $ index ;
569+ }
570+
571+ return false ;
572+ }
517573}
0 commit comments