MongoDB目前不支持模糊搜索。一種方法是使用字符串/探測相似度算法,如soundex。
我創建了一個PHP一個簡單的例子來說明如何用同音做到這一點:
$dbName = 'soundex';
$client = new MongoClient("mongodb://127.0.0.1", array('db' => $db));
$db = $client->$dbName;
$phrases = array(
'This, is the last of earth. I am content.',
'Take a step forward, lads. It will be easier that way.',
'Adieu, mes amis. Je vais à la gloire. (Farewell, my friends. I go to glory.)',
'I will die like a true-blue rebel. Don\'t waste any time in mourning - organize.'
);
// just for the example, so we can reuse the script several times
$db->phrases->drop();
foreach ($phrases as $phrase) {
// remove all non characters/whitespaces
$phrase = preg_replace('/[^a-z\s]/i', ' ', $phrase);
// remove multiple whitespaces and whitespaces at the beginning/end of the phrase
$phrase = preg_replace('/\s\s+/', ' ', trim($phrase));
// split the phrase into unique words
$words = array_unique(explode(' ', $phrase));
$soundex = array();
foreach ($words as $word) {
$soundex[] = soundex($word);
}
$soundex = array_unique($soundex);
$db->phrases->insert(array(
'phrase' => $phrase,
'soundex' => $soundex
));
}
// search for some words
$searches = array(
'earht', // earth, phrase 1
'eaasierr', // easier, phrase 2
'faerwel', // farewell, phrase 3
'reebell' // rebel, phrase 4
);
foreach ($searches as $search) {
$cursor = $db->phrases->find(array(
'soundex' => array(
'$in' => array(soundex($search))
)
));
if ($cursor->count()) {
foreach ($cursor as $doc) {
echo "Search result for '$search':\n";
echo $doc['phrase'] . "\n\n";
}
} else {
echo "No results for '$search'\n\n";
echo soundex($search);
}
}
這個例子將輸出:
Search result for 'earht':
This is the last of earth I am content
Search result for 'eaasierr':
Take a step forward lads It will be easier that way
Search result for 'faerwel':
Adieu mes amis Je vais la gloire Farewell my friends I go to glory
Search result for 'reebell':
I will die like a true blue rebel Don t waste any time in mourning organize
這只是一個簡單的例子,而不移除停止字。您還必須記住爲soundex值創建索引。
瞭解更多關於同音:http://php.net/soundex
希望這有助於獲得如何做MongoDB的模糊搜索的想法。