Skip to content

Commit c4618ec

Browse files
committed
Create search example
1 parent 4edad3c commit c4618ec

File tree

1 file changed

+129
-0
lines changed

1 file changed

+129
-0
lines changed

examples/atlas-search.php

Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
<?php
2+
3+
/**
4+
* This example demonstrates how to create a search index and perform a text search.
5+
* It requires a MongoDB Atlas M10+ cluster with Sample Dataset loaded.
6+
*/
7+
8+
declare(strict_types=1);
9+
10+
namespace MongoDB\Examples;
11+
12+
use Closure;
13+
use MongoDB\Client;
14+
use RuntimeException;
15+
16+
use function define;
17+
use function getenv;
18+
use function hrtime;
19+
use function iterator_to_array;
20+
use function preg_match;
21+
use function printf;
22+
use function sleep;
23+
24+
require __DIR__ . '/../vendor/autoload.php';
25+
26+
$uri = getenv('MONGODB_URI');
27+
if (! $uri || ! preg_match('/\.(mongodb\.net|mongodb-dev\.net)/', $uri)) {
28+
printf("This example requires a MongoDB Atlas cluster.\n");
29+
printf("Make sure you set the MONGODB_URI environment variable.\n");
30+
exit(1);
31+
}
32+
33+
define('WAIT_TIMEOUT_SEC', getenv('MONGODB_WAIT_TIMEOUT_SEC') ?: 300);
34+
define('INDEX_NAME', 'default');
35+
36+
$client = new Client($uri);
37+
$collection = $client->sample_airbnb->listingsAndReviews;
38+
39+
$count = $collection->estimatedDocumentCount();
40+
if ($count === 0) {
41+
printf("This example requires the sample_airbnb database with the listingsAndReviews collection.\n");
42+
printf("Load the sample dataset in your MongoDB Atlas cluster before running this example:\n");
43+
printf(" https://www.mongodb.com/docs/atlas/sample-data/\n");
44+
exit(1);
45+
}
46+
47+
// Delete the index if it already exists.
48+
$indexes = iterator_to_array($collection->listSearchIndexes());
49+
foreach ($indexes as $index) {
50+
if ($index->name === 'default') {
51+
printf("\nThe index already exists. Dropping it.\n");
52+
$collection->dropSearchIndex($index->name);
53+
54+
// Wait for the index to be deleted.
55+
wait(function () use ($collection) {
56+
foreach ($collection->listSearchIndexes() as $index) {
57+
if ($index->name === 'default') {
58+
printf("Waiting for the index to be deleted...\n");
59+
60+
return false;
61+
}
62+
}
63+
64+
return true;
65+
});
66+
}
67+
}
68+
69+
// Create the search index
70+
printf("\nCreating the index.\n");
71+
$collection->createSearchIndex(
72+
// Index definition
73+
// See: https://www.mongodb.com/docs/atlas/atlas-search/define-field-mappings/
74+
['mappings' => ['dynamic' => true]],
75+
// "default" is the default index name, this config can be omitted.
76+
['name' => 'default'],
77+
);
78+
79+
// Wait for the index to be ready.
80+
wait(function () use ($collection) {
81+
foreach ($collection->listSearchIndexes() as $index) {
82+
if ($index->name === 'default') {
83+
printf("Waiting for the index to be ready...\n");
84+
85+
return $index->queryable;
86+
}
87+
}
88+
89+
return false;
90+
});
91+
92+
// Perform a text search.
93+
printf("\nPerforming a text search...\n");
94+
$results = $collection->aggregate([
95+
[
96+
'$search' => [
97+
'index' => 'default',
98+
'text' => [
99+
'query' => 'view beach ocean',
100+
'path' => ['name'],
101+
],
102+
],
103+
],
104+
['$project' => ['name' => 1, 'description' => 1]],
105+
['$limit' => 10],
106+
])->toArray();
107+
108+
foreach ($results as $document) {
109+
printf(" - %s\n", $document['name']);
110+
}
111+
112+
printf("\nEnjoy MongoDB Atlas Search!\n\n");
113+
114+
/**
115+
* Wait until the callback returns true or the timeout is reached.
116+
*/
117+
function wait(Closure $callback): void
118+
{
119+
$timeout = hrtime()[0] + WAIT_TIMEOUT_SEC;
120+
while (hrtime()[0] < $timeout) {
121+
if ($callback()) {
122+
return;
123+
}
124+
125+
sleep(5);
126+
}
127+
128+
throw new RuntimeException('Time out');
129+
}

0 commit comments

Comments
 (0)