Skip to content

Commit 0c52834

Browse files
committed
wip
1 parent fe84cee commit 0c52834

File tree

1 file changed

+47
-3
lines changed

1 file changed

+47
-3
lines changed

docs/includes/fundamentals/as-avs/AtlasSearchTest.php

Lines changed: 47 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,16 @@
88
use Illuminate\Support\Facades\DB;
99
use MongoDB\Builder\Query;
1010
use MongoDB\Builder\Search;
11+
use MongoDB\Driver\Exception\ServerException;
12+
use MongoDB\Laravel\Schema\Builder;
1113
use MongoDB\Laravel\Tests\TestCase;
1214

1315
use function array_map;
1416
use function mt_getrandmax;
1517
use function rand;
1618
use function range;
1719
use function srand;
20+
use function usleep;
1821

1922
class 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

Comments
 (0)