8
8
use Illuminate \Support \Facades \Schema ;
9
9
use MongoDB \BSON \Binary ;
10
10
use MongoDB \BSON \UTCDateTime ;
11
+ use MongoDB \Collection ;
11
12
use MongoDB \Laravel \Schema \Blueprint ;
12
13
14
+ use function assert ;
13
15
use function collect ;
14
16
use function count ;
15
17
@@ -523,9 +525,51 @@ public function testGetIndexes()
523
525
$ this ->assertSame ([], $ indexes );
524
526
}
525
527
528
+ /** @todo requires SearchIndex support */
529
+ public function testSearchIndex (): void
530
+ {
531
+ Schema::create ('newcollection ' , function (Blueprint $ collection ) {
532
+ $ collection ->searchIndex ([
533
+ 'mappings ' => [
534
+ 'dynamic ' => false ,
535
+ 'fields ' => [
536
+ 'foo ' => ['type ' => 'string ' , 'analyzer ' => 'lucene.whitespace ' ],
537
+ ],
538
+ ],
539
+ ]);
540
+ });
541
+
542
+ $ index = $ this ->getSearchIndex ('newcollection ' , 'default ' );
543
+ self ::assertNotFalse ($ index );
544
+
545
+ self ::assertSame ('default ' , $ index ['name ' ]);
546
+ self ::assertSame ('search ' , $ index ['type ' ]);
547
+ self ::assertFalse ($ index ['latestDefinition ' ]['mappings ' ]['dynamic ' ]);
548
+ self ::assertSame ('lucene.whitespace ' , $ index ['latestDefinition ' ]['mappings ' ]['fields ' ]['foo ' ]['analyzer ' ]);
549
+ }
550
+
551
+ public function testVectorSearchIndex ()
552
+ {
553
+ Schema::create ('newcollection ' , function (Blueprint $ collection ) {
554
+ $ collection ->vectorSearchIndex ([
555
+ 'fields ' => [
556
+ ['type ' => 'vector ' , 'path ' => 'foo ' , 'numDimensions ' => 128 , 'similarity ' => 'euclidean ' , 'quantization ' => 'none ' ],
557
+ ],
558
+ ], 'vector ' );
559
+ });
560
+
561
+ $ index = $ this ->getSearchIndex ('newcollection ' , 'vector ' );
562
+ self ::assertNotFalse ($ index );
563
+
564
+ self ::assertSame ('vector ' , $ index ['name ' ]);
565
+ self ::assertSame ('vectorSearch ' , $ index ['type ' ]);
566
+ self ::assertSame ('vector ' , $ index ['latestDefinition ' ]['fields ' ][0 ]['type ' ]);
567
+ }
568
+
526
569
protected function getIndex (string $ collection , string $ name )
527
570
{
528
571
$ collection = DB ::getCollection ($ collection );
572
+ assert ($ collection instanceof Collection);
529
573
530
574
foreach ($ collection ->listIndexes () as $ index ) {
531
575
if (isset ($ index ['key ' ][$ name ])) {
@@ -535,4 +579,16 @@ protected function getIndex(string $collection, string $name)
535
579
536
580
return false ;
537
581
}
582
+
583
+ protected function getSearchIndex (string $ collection , string $ name )
584
+ {
585
+ $ collection = DB ::getCollection ($ collection );
586
+ assert ($ collection instanceof Collection);
587
+
588
+ foreach ($ collection ->listSearchIndexes (['name ' => $ name ]) as $ index ) {
589
+ return $ index ;
590
+ }
591
+
592
+ return false ;
593
+ }
538
594
}
0 commit comments