Skip to content

Commit

Permalink
Create search example
Browse files Browse the repository at this point in the history
  • Loading branch information
GromNaN committed Aug 31, 2023
1 parent f6ce211 commit 5c8a818
Show file tree
Hide file tree
Showing 2 changed files with 156 additions and 0 deletions.
135 changes: 135 additions & 0 deletions examples/atlas-search.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
<?php

/**
* This example demonstrates how to create an Atlas Search index and perform a search query.
* It requires a MongoDB Atlas M10+ cluster with Sample Dataset loaded.
*
* Use the MONGODB_URI environment variable to specify the connection string from the Atlas UI.
*/

declare(strict_types=1);

namespace MongoDB\Examples;

use Closure;
use MongoDB\Client;
use RuntimeException;

use function define;
use function getenv;
use function hrtime;
use function iterator_to_array;
use function sleep;
use function str_contains;

require __DIR__ . '/../vendor/autoload.php';

$uri = getenv('MONGODB_URI');
if (! $uri || ! str_contains($uri, '.mongodb.net')) {
echo 'This example requires a MongoDB Atlas cluster.', "\n";
echo 'Make sure you set the MONGODB_URI environment variable.', "\n";
exit(1);
}

// Atlas Search index management operations are asynchronous.
// They usually take less than 5 minutes to complete.
define('WAIT_TIMEOUT_SEC', 300);

$client = new Client($uri);

Check failure on line 38 in examples/atlas-search.php

View workflow job for this annotation

GitHub Actions / Psalm

PossiblyFalseArgument

examples/atlas-search.php:38:22: PossiblyFalseArgument: Argument 1 of MongoDB\Client::__construct cannot be false, possibly null|string value expected (see https://psalm.dev/104)
$collection = $client->selectCollection('sample_airbnb', 'listingsAndReviews');

$count = $collection->estimatedDocumentCount();
if ($count === 0) {
echo 'This example requires the sample_airbnb database with the listingsAndReviews collection.', "\n";
echo 'Load the sample dataset in your MongoDB Atlas cluster before running this example:', "\n";
echo ' https://www.mongodb.com/docs/atlas/sample-data/', "\n";
exit(1);
}

// Delete the index if it already exists
$indexes = iterator_to_array($collection->listSearchIndexes());
foreach ($indexes as $index) {
if ($index->name === 'default') {
echo "\nThe index already exists. Dropping it.\n";
$collection->dropSearchIndex($index->name);

// Wait for the index to be deleted.
wait(function () use ($collection) {
foreach ($collection->listSearchIndexes() as $index) {
if ($index->name === 'default') {
echo '.';

return false;
}
}

echo "\n";

return true;
});
}
}

// Create the search index
echo "\nCreating the index.\n";
$collection->createSearchIndex(
/* The index definition requires a mapping
* See: https://www.mongodb.com/docs/atlas/atlas-search/define-field-mappings/ */
['mappings' => ['dynamic' => true]],
// "default" is the default index name, this config can be omitted.
['name' => 'default'],
);

// Wait for the index to be ready.
wait(function () use ($collection) {

Check failure on line 84 in examples/atlas-search.php

View workflow job for this annotation

GitHub Actions / Psalm

MixedArgumentTypeCoercion

examples/atlas-search.php:84:6: MixedArgumentTypeCoercion: Argument 1 of MongoDB\Examples\wait expects Closure():bool, but parent type impure-Closure():(false|mixed) provided (see https://psalm.dev/194)
foreach ($collection->listSearchIndexes() as $index) {
if ($index->name === 'default') {
echo '.';

return $index->queryable;
}
}

return false;
});

// Perform a text search
echo "\n", 'Performing a text search...', "\n";
$results = $collection->aggregate([
[
'$search' => [
'index' => 'default',
'text' => [
'query' => 'view beach ocean',
'path' => ['name'],
],
],
],
['$project' => ['name' => 1, 'description' => 1]],
['$limit' => 10],
])->toArray();

foreach ($results as $document) {
echo ' -', $document['name'], "\n";
}

echo "\n", 'Enjoy MongoDB Atlas Search!', "\n\n";

/**
* This function waits until the callback returns true or the timeout is reached.
*
* @param Closure():bool $callback
*/
function wait(Closure $callback): void
{
$timeout = hrtime()[0] + WAIT_TIMEOUT_SEC;
while (hrtime()[0] < $timeout) {
if ($callback()) {
return;
}

sleep(5);
}

throw new RuntimeException('Time out');
}
21 changes: 21 additions & 0 deletions psalm-baseline.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,27 @@
<code><![CDATA[$clientEncryption->decrypt($document->encryptedField)]]></code>
</MixedArgument>
</file>
<file src="examples/atlas-search.php">
<MixedArgument>
<code><![CDATA[$document['name']]]></code>
<code><![CDATA[$index->name]]></code>
</MixedArgument>
<MixedArrayAccess>
<code><![CDATA[$document['name']]]></code>
</MixedArrayAccess>
<MixedAssignment>
<code>$document</code>
<code>$index</code>
<code>$index</code>
<code>$index</code>
</MixedAssignment>
<MixedPropertyFetch>
<code><![CDATA[$index->name]]></code>
<code><![CDATA[$index->name]]></code>
<code><![CDATA[$index->name]]></code>
<code><![CDATA[$index->queryable]]></code>
</MixedPropertyFetch>
</file>
<file src="src/ChangeStream.php">
<DeprecatedConstant>
<code>self::CURSOR_NOT_FOUND</code>
Expand Down

0 comments on commit 5c8a818

Please sign in to comment.