Last active
December 16, 2015 00:08
-
-
Save mcuadros/5344879 to your computer and use it in GitHub Desktop.
Basic example in PHP of Mongo 2.4 Text Search feature.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$m = new MongoClient(); // connect | |
$db = $collection = $m->foo; // get the database named "foo" | |
$collection = $db->bar; // get the collection "bar" from database named "foo" | |
$collection->ensureIndex( | |
array( | |
'title' => 'text', | |
'desc' => 'text', | |
), | |
array( | |
'name' => 'ExampleTextIndex', | |
'weights' => array( | |
'title' => 100, | |
'desc' => 30, | |
) | |
) | |
); | |
$result = $db->command( | |
array( | |
'text' => 'bar', //this is the name of the collection where we are searching | |
'search' => 'hotel', //the string to search | |
'limit' => 5, //the number of results, by default is 1000 | |
'project' => Array( //the fields to retrieve from db | |
'title' => 1 | |
) | |
) | |
); | |
print_r($result); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment