2014-05-23 41 views
0

我有我的收藏mongoidb的索引全文,我想知道是否有辦法找到一個結果,即使單詞拼寫錯誤,例如:mongodb全文搜索,有拼寫檢查還是實現方法?

{id:1, 
description: "The world is yours" 
},{ 
id:2 
description: "Hello my friend"} 

,如果我搜索的單詞「 wordl'的結果是:

{id:1, 
    description: "The world is yours" 
    } 

這是可能的嗎?

回答

2

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的模糊搜索的想法。