2014-02-28 75 views

回答

2

https://atmospherejs.com/perak/fuzzy-search它使用Levenshtein Distance算法。自述採取的一個例子:

// If we have a collection named "Drinks" which contains "beer", "juice" and "milk" 

var searchString = "bear"; // user typed "bear" instead of "beer" 

// search "Drinks" collection for string "bear" 
var someCursor = Drinks.find({ drink_name: searchString }); 

// "bear" is not found, so we want to find most similar word to give user suggestion (Did you mean...) 
if(someCursor.count() == 0) 
{ 
    // expose entire collection 
    var tempCursor = Drinks.find({ }, { drink_name: true }); 

    // find most similar string 
    var bestWord = mostSimilarString(tempCursor, "drink_name", searchString, -1, false); 

    // in this example, bestWord is "beer", show user a suggestion: "Did you mean beer?" 
    // ... 
} 

也有其他替代品,如https://atmospherejs.com/matteodem/easy-search它採用Elastic Searchhttps://github.com/Crenshinibon/spomet這也是非常強大的,但看起來很難在第一次使用。