8
8
use Illuminate \Support \Facades \Schema ;
9
9
use MongoDB \BSON \Binary ;
10
10
use MongoDB \BSON \UTCDateTime ;
11
+ use MongoDB \Collection ;
12
+ use MongoDB \Database ;
11
13
use MongoDB \Laravel \Schema \Blueprint ;
12
14
15
+ use function assert ;
13
16
use function collect ;
14
17
use function count ;
15
18
16
19
class SchemaTest extends TestCase
17
20
{
18
21
public function tearDown (): void
19
22
{
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 ' );
22
27
}
23
28
24
29
public function testCreate (): void
@@ -474,6 +479,7 @@ public function testGetColumns()
474
479
$ this ->assertSame ([], $ columns );
475
480
}
476
481
482
+ /** @see AtlasSearchTest::testGetIndexes() */
477
483
public function testGetIndexes ()
478
484
{
479
485
Schema::create ('newcollection ' , function (Blueprint $ collection ) {
@@ -523,9 +529,54 @@ public function testGetIndexes()
523
529
$ this ->assertSame ([], $ indexes );
524
530
}
525
531
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
+
526
576
protected function getIndex (string $ collection , string $ name )
527
577
{
528
578
$ collection = DB ::getCollection ($ collection );
579
+ assert ($ collection instanceof Collection);
529
580
530
581
foreach ($ collection ->listIndexes () as $ index ) {
531
582
if (isset ($ index ['key ' ][$ name ])) {
@@ -535,4 +586,16 @@ protected function getIndex(string $collection, string $name)
535
586
536
587
return false ;
537
588
}
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
+ }
538
601
}
0 commit comments