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
@@ -502,9 +504,51 @@ public function testGetIndexes()
502
504
$ this ->assertSame ([], $ indexes );
503
505
}
504
506
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
+ ], 'vector ' );
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
+
505
548
protected function getIndex (string $ collection , string $ name )
506
549
{
507
550
$ collection = DB ::getCollection ($ collection );
551
+ assert ($ collection instanceof Collection);
508
552
509
553
foreach ($ collection ->listIndexes () as $ index ) {
510
554
if (isset ($ index ['key ' ][$ name ])) {
@@ -514,4 +558,16 @@ protected function getIndex(string $collection, string $name)
514
558
515
559
return false ;
516
560
}
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
+ }
517
573
}
0 commit comments