|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * This example demonstrates how to create an Atlas Search index and perform a search query. |
| 5 | + * It requires a MongoDB Atlas M10+ cluster with Sample Dataset loaded. |
| 6 | + * |
| 7 | + * Use the MONGODB_URI environment variable to specify the connection string from the Atlas UI. |
| 8 | + */ |
| 9 | + |
| 10 | +declare(strict_types=1); |
| 11 | + |
| 12 | +namespace MongoDB\Examples; |
| 13 | + |
| 14 | +use Closure; |
| 15 | +use MongoDB\Client; |
| 16 | +use RuntimeException; |
| 17 | + |
| 18 | +use function define; |
| 19 | +use function getenv; |
| 20 | +use function hrtime; |
| 21 | +use function iterator_to_array; |
| 22 | +use function sleep; |
| 23 | +use function str_contains; |
| 24 | + |
| 25 | +require __DIR__ . '/../vendor/autoload.php'; |
| 26 | + |
| 27 | +$uri = getenv('MONGODB_URI'); |
| 28 | +if (! $uri || ! str_contains($uri, '.mongodb.net')) { |
| 29 | + echo 'This example requires a MongoDB Atlas cluster.', "\n"; |
| 30 | + echo 'Make sure you set the MONGODB_URI environment variable.', "\n"; |
| 31 | + exit(1); |
| 32 | +} |
| 33 | + |
| 34 | +// Atlas Search index management operations are asynchronous. |
| 35 | +// They usually take less than 5 minutes to complete. |
| 36 | +define('WAIT_TIMEOUT_SEC', 300); |
| 37 | + |
| 38 | +$client = new Client($uri); |
| 39 | +$collection = $client->selectCollection('sample_airbnb', 'listingsAndReviews'); |
| 40 | + |
| 41 | +$count = $collection->estimatedDocumentCount(); |
| 42 | +if ($count === 0) { |
| 43 | + echo 'This example requires the sample_airbnb database with the listingsAndReviews collection.', "\n"; |
| 44 | + echo 'Load the sample dataset in your MongoDB Atlas cluster before running this example:', "\n"; |
| 45 | + echo ' https://www.mongodb.com/docs/atlas/sample-data/', "\n"; |
| 46 | + exit(1); |
| 47 | +} |
| 48 | + |
| 49 | +// Delete the index if it already exists |
| 50 | +$indexes = iterator_to_array($collection->listSearchIndexes()); |
| 51 | +foreach ($indexes as $index) { |
| 52 | + if ($index->name === 'default') { |
| 53 | + echo "The index already exists. Dropping it.\n"; |
| 54 | + $collection->dropSearchIndex($index->name); |
| 55 | + |
| 56 | + // Wait for the index to be deleted. |
| 57 | + wait(function () use ($collection) { |
| 58 | + echo '.'; |
| 59 | + foreach ($collection->listSearchIndexes() as $index) { |
| 60 | + if ($index->name === 'default') { |
| 61 | + return false; |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + return true; |
| 66 | + }); |
| 67 | + } |
| 68 | +} |
| 69 | + |
| 70 | +// Create the search index |
| 71 | +echo "\nCreating the index.\n"; |
| 72 | +$collection->createSearchIndex( |
| 73 | + /* The index definition requires a mapping |
| 74 | + * See: https://www.mongodb.com/docs/atlas/atlas-search/define-field-mappings/ */ |
| 75 | + ['mappings' => ['dynamic' => true]], |
| 76 | + // "default" is the default index name, this config can be omitted. |
| 77 | + ['name' => 'default'], |
| 78 | +); |
| 79 | + |
| 80 | +// Wait for the index to be ready. |
| 81 | +wait(function () use ($collection) { |
| 82 | + echo '.'; |
| 83 | + foreach ($collection->listSearchIndexes() as $index) { |
| 84 | + if ($index->name === 'default') { |
| 85 | + return $index->queryable; |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + return false; |
| 90 | +}); |
| 91 | + |
| 92 | +// Perform a text search |
| 93 | +echo "\n", 'Performing 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 | + echo ' - ', $document['name'], "\n"; |
| 110 | +} |
| 111 | + |
| 112 | +echo "\n", 'Enjoy MongoDB Atlas Search!', "\n\n"; |
| 113 | + |
| 114 | +/** |
| 115 | + * This function waits 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