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 값에 대한 색인을 작성하는 것을 기억해야합니다. MongoDB를 가진 퍼지 검색을 수행하는 방법에 대한 아이디어를 얻을하는 데 도움이 http://php.net/soundex
희망 :
는 사운 덱스에 대해 자세히 알아보십시오.