88use Illuminate \Support \Facades \Schema ;
99use MongoDB \BSON \Binary ;
1010use MongoDB \BSON \UTCDateTime ;
11+ use MongoDB \Collection ;
12+ use MongoDB \Database ;
1113use MongoDB \Laravel \Schema \Blueprint ;
1214
15+ use function assert ;
1316use function collect ;
1417use function count ;
1518
1619class SchemaTest extends TestCase
1720{
1821 public function tearDown (): void
1922 {
20- Schema::drop ('newcollection ' );
21- Schema::drop ('newcollection_two ' );
23+ $ database = $ this ->getConnection ('mongodb ' )->getMongoDB ();
24+ assert ($ database instanceof Database);
25+ $ database ->dropCollection ('newcollection ' );
26+ $ database ->dropCollection ('newcollection_two ' );
2227 }
2328
2429 public function testCreate (): void
@@ -474,6 +479,7 @@ public function testGetColumns()
474479 $ this ->assertSame ([], $ columns );
475480 }
476481
482+ /** @see AtlasSearchTest::testGetIndexes() */
477483 public function testGetIndexes ()
478484 {
479485 Schema::create ('newcollection ' , function (Blueprint $ collection ) {
@@ -523,9 +529,54 @@ public function testGetIndexes()
523529 $ this ->assertSame ([], $ indexes );
524530 }
525531
532+ public function testSearchIndex (): void
533+ {
534+ $ this ->skipIfSearchIndexManagementIsNotSupported ();
535+
536+ Schema::create ('newcollection ' , function (Blueprint $ collection ) {
537+ $ collection ->searchIndex ([
538+ 'mappings ' => [
539+ 'dynamic ' => false ,
540+ 'fields ' => [
541+ 'foo ' => ['type ' => 'string ' , 'analyzer ' => 'lucene.whitespace ' ],
542+ ],
543+ ],
544+ ]);
545+ });
546+
547+ $ index = $ this ->getSearchIndex ('newcollection ' , 'default ' );
548+ self ::assertNotNull ($ index );
549+
550+ self ::assertSame ('default ' , $ index ['name ' ]);
551+ self ::assertSame ('search ' , $ index ['type ' ]);
552+ self ::assertFalse ($ index ['latestDefinition ' ]['mappings ' ]['dynamic ' ]);
553+ self ::assertSame ('lucene.whitespace ' , $ index ['latestDefinition ' ]['mappings ' ]['fields ' ]['foo ' ]['analyzer ' ]);
554+ }
555+
556+ public function testVectorSearchIndex ()
557+ {
558+ $ this ->skipIfSearchIndexManagementIsNotSupported ();
559+
560+ Schema::create ('newcollection ' , function (Blueprint $ collection ) {
561+ $ collection ->vectorSearchIndex ([
562+ 'fields ' => [
563+ ['type ' => 'vector ' , 'path ' => 'foo ' , 'numDimensions ' => 128 , 'similarity ' => 'euclidean ' , 'quantization ' => 'none ' ],
564+ ],
565+ ], 'vector ' );
566+ });
567+
568+ $ index = $ this ->getSearchIndex ('newcollection ' , 'vector ' );
569+ self ::assertNotNull ($ index );
570+
571+ self ::assertSame ('vector ' , $ index ['name ' ]);
572+ self ::assertSame ('vectorSearch ' , $ index ['type ' ]);
573+ self ::assertSame ('vector ' , $ index ['latestDefinition ' ]['fields ' ][0 ]['type ' ]);
574+ }
575+
526576 protected function getIndex (string $ collection , string $ name )
527577 {
528578 $ collection = DB ::getCollection ($ collection );
579+ assert ($ collection instanceof Collection);
529580
530581 foreach ($ collection ->listIndexes () as $ index ) {
531582 if (isset ($ index ['key ' ][$ name ])) {
@@ -535,4 +586,16 @@ protected function getIndex(string $collection, string $name)
535586
536587 return false ;
537588 }
589+
590+ protected function getSearchIndex (string $ collection , string $ name ): ?array
591+ {
592+ $ collection = DB ::getCollection ($ collection );
593+ assert ($ collection instanceof Collection);
594+
595+ foreach ($ collection ->listSearchIndexes (['name ' => $ name , 'typeMap ' => ['root ' => 'array ' , 'array ' => 'array ' , 'document ' => 'array ' ]]) as $ index ) {
596+ return $ index ;
597+ }
598+
599+ return null ;
600+ }
538601}
0 commit comments