|
| 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 sleep; |
| 22 | + |
| 23 | +require __DIR__ . '/../vendor/autoload.php'; |
| 24 | + |
| 25 | +$uri = getenv('MONGODB_URI'); |
| 26 | +if (! $uri || ! preg_match('/\.(mongodb\.net|mongodb-dev\.net)/', $uri)) { |
| 27 | + echo "This example requires a MongoDB Atlas cluster.\n"; |
| 28 | + echo "Make sure you set the MONGODB_URI environment variable.\n"; |
| 29 | + exit(1); |
| 30 | +} |
| 31 | + |
| 32 | +define('WAIT_TIMEOUT_SEC', (int) getenv('MONGODB_WAIT_TIMEOUT_SEC') ?: 300); |
| 33 | +define('INDEX_NAME', 'default'); |
| 34 | + |
| 35 | +$client = new Client($uri); |
| 36 | +$collection = $client->selectCollection('sample_airbnb', 'listingsAndReviews'); |
| 37 | + |
| 38 | +$count = $collection->estimatedDocumentCount(); |
| 39 | +if ($count === 0) { |
| 40 | + echo "This example requires the sample_airbnb database with the listingsAndReviews collection.\n"; |
| 41 | + echo "Load the sample dataset in your MongoDB Atlas cluster before running this example:\n"; |
| 42 | + echo " https://www.mongodb.com/docs/atlas/sample-data/\n"; |
| 43 | + exit(1); |
| 44 | +} |
| 45 | + |
| 46 | +// Delete the index if it already exists. |
| 47 | +$indexes = iterator_to_array($collection->listSearchIndexes()); |
| 48 | +foreach ($indexes as $index) { |
| 49 | + if ($index->name === 'default') { |
| 50 | + echo "\nThe index already exists. Dropping it.\n"; |
| 51 | + $collection->dropSearchIndex($index->name); |
| 52 | + |
| 53 | + // Wait for the index to be deleted. |
| 54 | + wait(function () use ($collection) { |
| 55 | + foreach ($collection->listSearchIndexes() as $index) { |
| 56 | + if ($index->name === 'default') { |
| 57 | + echo '.'; |
| 58 | + |
| 59 | + return false; |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + echo "\n"; |
| 64 | + |
| 65 | + return true; |
| 66 | + }); |
| 67 | + } |
| 68 | +} |
| 69 | + |
| 70 | +// Create the search index |
| 71 | +echo "\nCreating the index.\n"; |
| 72 | +$collection->createSearchIndex( |
| 73 | + // Index definition |
| 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 | + foreach ($collection->listSearchIndexes() as $index) { |
| 83 | + if ($index->name === 'default') { |
| 84 | + echo '.'; |
| 85 | + |
| 86 | + return $index->queryable; |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + return false; |
| 91 | +}); |
| 92 | + |
| 93 | +// Perform a text search. |
| 94 | +echo "\nPerforming a text search...\n"; |
| 95 | +$results = $collection->aggregate([ |
| 96 | + [ |
| 97 | + '$search' => [ |
| 98 | + 'index' => 'default', |
| 99 | + 'text' => [ |
| 100 | + 'query' => 'view beach ocean', |
| 101 | + 'path' => ['name'], |
| 102 | + ], |
| 103 | + ], |
| 104 | + ], |
| 105 | + ['$project' => ['name' => 1, 'description' => 1]], |
| 106 | + ['$limit' => 10], |
| 107 | +])->toArray(); |
| 108 | + |
| 109 | +foreach ($results as $document) { |
| 110 | + echo ' -', $document['name'], "\n"; |
| 111 | +} |
| 112 | + |
| 113 | +echo "\nEnjoy MongoDB Atlas Search!\n\n"; |
| 114 | + |
| 115 | +/** |
| 116 | + * Wait until the callback returns true or the timeout is reached. |
| 117 | + */ |
| 118 | +function wait(Closure $callback): void |
| 119 | +{ |
| 120 | + $timeout = hrtime()[0] + WAIT_TIMEOUT_SEC; |
| 121 | + while (hrtime()[0] < $timeout) { |
| 122 | + if ($callback()) { |
| 123 | + return; |
| 124 | + } |
| 125 | + |
| 126 | + sleep(5); |
| 127 | + } |
| 128 | + |
| 129 | + throw new RuntimeException('Time out'); |
| 130 | +} |
0 commit comments