88use Illuminate \Support \Facades \DB ;
99use MongoDB \Builder \Query ;
1010use MongoDB \Builder \Search ;
11+ use MongoDB \Driver \Exception \ServerException ;
12+ use MongoDB \Laravel \Schema \Builder ;
1113use MongoDB \Laravel \Tests \TestCase ;
1214
1315use function array_map ;
1416use function mt_getrandmax ;
1517use function rand ;
1618use function range ;
1719use function srand ;
20+ use function usleep ;
1821
1922class AtlasSearchTest extends TestCase
2023{
@@ -28,9 +31,6 @@ protected function setUp(): void
2831
2932 $ moviesCollection = DB ::connection ('mongodb ' )->getCollection ('movies ' );
3033 $ moviesCollection ->drop ();
31- $ moviesCollection ->createSearchIndex ([
32- 'mappings ' => ['dynamic ' => true ],
33- ], ['name ' => 'simple_search ' ]);
3434
3535 Movie::insert ([
3636 ['title ' => 'Dreaming of Jakarta ' , 'year ' => 1990 ],
@@ -46,6 +46,50 @@ protected function setUp(): void
4646 ['title ' => 'C ' , 'plot ' => 'High school friends navigate love, identity, and unexpected challenges before graduating together. ' ],
4747 ['title ' => 'D ' , 'plot ' => 'Stranded on a distant planet, astronauts must repair their ship before supplies run out. ' ],
4848 ]));
49+
50+ $ moviesCollection = DB ::connection ('mongodb ' )->getCollection ('movies ' );
51+
52+ try {
53+ $ moviesCollection ->createSearchIndex ([
54+ 'mappings ' => [
55+ 'fields ' => [
56+ 'title ' => [
57+ ['type ' => 'string ' , 'analyzer ' => 'lucene.english ' ],
58+ ['type ' => 'autocomplete ' , 'analyzer ' => 'lucene.english ' ],
59+ ['type ' => 'token ' ],
60+ ],
61+ ],
62+ ],
63+ ]);
64+
65+ $ moviesCollection ->createSearchIndex ([
66+ 'mappings ' => ['dynamic ' => true ],
67+ ], ['name ' => 'dynamic_search ' ]);
68+
69+ $ moviesCollection ->createSearchIndex ([
70+ 'fields ' => [
71+ ['type ' => 'vector ' , 'numDimensions ' => 4 , 'path ' => 'vector4 ' , 'similarity ' => 'cosine ' ],
72+ ['type ' => 'filter ' , 'path ' => 'title ' ],
73+ ],
74+ ], ['name ' => 'vector ' , 'type ' => 'vectorSearch ' ]);
75+ } catch (ServerException $ e ) {
76+ if (Builder::isAtlasSearchNotSupportedException ($ e )) {
77+ self ::markTestSkipped ('Atlas Search not supported. ' . $ e ->getMessage ());
78+ }
79+
80+ throw $ e ;
81+ }
82+
83+ // Wait for the index to be ready
84+ do {
85+ $ ready = true ;
86+ usleep (10_000 );
87+ foreach ($ collection ->listSearchIndexes () as $ index ) {
88+ if ($ index ['status ' ] !== 'READY ' ) {
89+ $ ready = false ;
90+ }
91+ }
92+ } while (! $ ready );
4993 }
5094
5195 /**
0 commit comments