Reds是由LearBoost公司的TJHolowaychuk开发的一个基于Redis的Node.js全文搜索引擎,其代码加上注释也只有300行。不得不说又是一个Redis的最佳实践,它的主要原理是通过Redis的sets数据结构将分词后的词语碎片进行存储。这里的分词仅仅是对英文按空格进行切分(中文分词就不要想了~)。
例子:先添加几个句子到搜索引擎中建立索引
varstrs=[];strs.push('Tobiwatsfourdollars');strs.push('Tobiolywats$4');strs.push('Lokiisreallyfat');strs.push('Loki,Jae,adTobiareferrets');strs.push('Mayisacat');strs.push('Luaisacat');strs.push('Mustachioisacat');strs.forEach(fuctio(str,i){search.idex(str,i);});然后在Tobidollars这个组合进行搜索
search.query(query='Tobidollars',fuctio(err,ids){if(err)throwerr;cosole.log('Searchresultsfor"%s":',query);ids.forEach(fuctio(id){cosole.log('-%s',strs[id]);});process.exit();});下面是其搜索结果
Searchresultsfor"Tobidollars":-Tobiwatsfourdollars介绍内容来自:https://blog.osqlfa.com/html/2676.html
评论