基于NodeJS和Redis的轻量级搜索引擎Reds

jopen 9年前

介绍:

Reds 是一个轻量的基于NodeJS和Redis的搜索引擎,由TJ Holowaychuk 开发,这个模块原本是为优化 Kue 搜索能力而开发,但是它也非常适合作为轻量的通用搜索库而加入到Blog、文档系统中去。

安装:

  1. $ npm install reds

例子:

首先你可能想做的事情就是创建一个可以通过关键字来进行搜索的搜索实例,你可以使用Redis的命名空间以支持在同一个DB里进行多重搜索。

  1. var search = reds.createSearch(pets);

Reds严格区分任意的数字和字符串ids,所以你可以用这个库来做任何原本想做的事情,甚至是结构化数据存储,下面的例子仅仅是一个包括了一些字符串数据库数组,我们调用Search#index()来将它加入到Reds.

  1. var strs = [];
  2. strs.push(Tobi wants four dollars);
  3. strs.push(Tobi only wants $4);
  4. strs.push(Loki is really fat);
  5. strs.push(Loki, Jane, and Tobi are ferrets);
  6. strs.push(Manny is a cat);
  7. strs.push(Luna is a cat);
  8. strs.push(Mustachio is a cat);
  9.  
  10. strs.forEach(function(str, i){ search.index(str, i); });

执行一个操作以防止Redsg来作为字符串调用Search#query(),然后通过回调函数来获取一个执行完成之后包含ids或者空数组的返回结果。

  1. search
  2.   .query(query = Tobi dollars)
  3.   .end(function(err, ids){
  4.     if (err) throw err;
  5.     console.log(Search results for "%s":, query);
  6.     ids.forEach(function(id){
  7.       console.log(  – %s, strs[id]);
  8.     });
  9.     process.exit();
  10.   });

当Reds匹配到搜索的关键词,将输出如下的结果:

  1. Search results for "Tobi dollars":
  2.   – Tobi wants four dollars

我们通过reds.search()也可以用Reds在回调函数里进行联合查询。

  1. search
  2.   .query(query = tobi dollars)
  3.   .end(function(err, ids){
  4.     if (err) throw err;
  5.     console.log(Search results for "%s":, query);
  6.     ids.forEach(function(id){
  7.       console.log(  – %s, strs[id]);
  8.     });
  9.     process.exit();
  10.   }, or);

如果匹配到包含:”Tobi”和”dollars”关键词的语句时,将返回如下结果:

  1. Search results for "tobi dollars":
  2.   – Tobi wants four dollars
  3.   – Tobi only wants $4
  4.   – Loki, Jane, and Tobi are ferrets

API

  1. reds.createSearch(key)
  2. Search#index(text, id[, fn])
  3. Search#remove(id[, fn]);
  4. Search#query(text, fn[, type]);

例如:

  1. var search = reds.createSearch(misc);
  2. search.index(Foo bar baz, abc);
  3. search.index(Foo bar, bcd);
  4. search.remove(bcd);
  5. search.query(foo bar).end(function(err, ids){});

Reds的项目主页是:https://github.com/visionmedia/reds