Reds:一个Redis加Node.js的全文搜索引擎

2011-08-02 21:46

Reds:一个Redis加Node.js的全文搜索引擎

by nosqlfan

at 2011-08-02 13:46:40

original http://item.feedsky.com/~feedsky/nosqlfan/~8149226/543387930/6253001/1/item.html

Reds是由LearnBoost公司的TJ Holowaychuk开发的一个基于Redis的Node.js全文搜索引擎,其代码加上注释也只有300行。不得不说又是一个Redis的最佳实践,它的主要原理是通过Redis的sets数据结构将分词后的词语碎片进行存储。这里的分词仅仅是对英文按空格进行切分(中文分词就不要想了~)。

例子:
先添加几个句子到搜索引擎中建立索引

var strs = [];
strs.push('Tobi wants four dollars');
strs.push('Tobi only wants $4');
strs.push('Loki is really fat');
strs.push('Loki, Jane, and Tobi are ferrets');
strs.push('Manny is a cat');
strs.push('Luna is a cat');
strs.push('Mustachio is a cat');

strs.forEach(function(str, i){ search.index(str, i); });

然后在Tobi dollars这个组合进行搜索

search.query(query = 'Tobi dollars', function(err, ids){
  if (err) throw err;
  console.log('Search results for "%s":', query);
  ids.forEach(function(id){
    console.log('  - %s', strs[id]);
  });
  process.exit();
});

下面是其搜索结果

Search results for "Tobi dollars":
  - Tobi wants four dollars

更多相关信息及源码请上其在GitHub上的项目地址:https://github.com/visionmedia/reds

同时感谢@xu_lele的分享,分享NoSQL相关技术与新闻,只需要在twitter,新浪微博,腾讯微博上@nosqlfan 即可。

技术传播,需要你我共同努力!    

相关文章:

Redis运维之道

Instagram的实时图片Demo:Node.js, Redis 加 Web Sockets

lodis:一个JavaScript实现的本地Redis存储

Redis作者详谈2.4版本改进

*nix、node.js、MongoDB 下一代的LAMP
无觅