8
8
use Illuminate \Support \Facades \DB ;
9
9
use MongoDB \Builder \Query ;
10
10
use MongoDB \Builder \Search ;
11
+ use MongoDB \Driver \Exception \ServerException ;
12
+ use MongoDB \Laravel \Schema \Builder ;
11
13
use MongoDB \Laravel \Tests \TestCase ;
12
14
13
15
use function array_map ;
14
16
use function mt_getrandmax ;
15
17
use function rand ;
16
18
use function range ;
17
19
use function srand ;
20
+ use function usleep ;
18
21
19
22
class AtlasSearchTest extends TestCase
20
23
{
@@ -28,9 +31,6 @@ protected function setUp(): void
28
31
29
32
$ moviesCollection = DB ::connection ('mongodb ' )->getCollection ('movies ' );
30
33
$ moviesCollection ->drop ();
31
- $ moviesCollection ->createSearchIndex ([
32
- 'mappings ' => ['dynamic ' => true ],
33
- ], ['name ' => 'simple_search ' ]);
34
34
35
35
Movie::insert ([
36
36
['title ' => 'Dreaming of Jakarta ' , 'year ' => 1990 ],
@@ -46,6 +46,50 @@ protected function setUp(): void
46
46
['title ' => 'C ' , 'plot ' => 'High school friends navigate love, identity, and unexpected challenges before graduating together. ' ],
47
47
['title ' => 'D ' , 'plot ' => 'Stranded on a distant planet, astronauts must repair their ship before supplies run out. ' ],
48
48
]));
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 );
49
93
}
50
94
51
95
/**
0 commit comments