Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
Thomas Fritz committed Apr 17, 2012
1 parent e3ff8bc commit 348d721
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 31 deletions.
54 changes: 30 additions & 24 deletions lib/index.js
Expand Up @@ -7,33 +7,39 @@ module.exports = function (document, callback) {
var text = document.text;
var id = document.id;

if (!text || !id) {
callback("invalid document", null);
return false;
}

var normalizedTokens = normalize(text);

// Start a Multi Bulk Command
// So for each document only one redis command will be executed
var multi = client.multi();
normalizedTokens.forEach(function (tokenParts) {
tokenParts.forEach(function (part) {
// for each token part we add a set with the given doc id
multi.sadd('W:' + part, id);
});
});

// execute all buffered commands
multi.exec(function (err, replies) {
if (err) {
client.incr('G:NEXTSCORE', function (err, nextScore) {
if(err) {
callback(err, null);
return false;
}
else {
callback(null, normalizedTokens);
return true;

if (!text || !id) {
callback("invalid document", null);
return false;
}
});

var normalizedTokens = normalize(text);

// Start a Multi Bulk Command
// So for each document only one redis command will be executed
var multi = client.multi();
normalizedTokens.forEach(function (tokenParts) {
tokenParts.forEach(function (part) {
// for each token part we add a set with the given doc id
multi.zadd('W:' + part, nextScore, id);
});
});

// execute all buffered commands
multi.exec(function (err, replies) {
if (err) {
callback(err, null);
return false;
}
else {
callback(null, normalizedTokens);
return true;
}
});
})
}
2 changes: 0 additions & 2 deletions lib/normalize.js
Expand Up @@ -5,7 +5,6 @@ var stopwords = require('./stopwords.js').words;
var stemmer = natural.LancasterStemmer;
var nounInflector = new natural.NounInflector();
var _ = require('underscore');
var util = require('util');

/**
* Stemmed Parts is an array with a length of 3
Expand Down Expand Up @@ -43,7 +42,6 @@ module.exports = function (text) {
parts.push(singular.toUpperCase());

parts.push(token);
console.log(util.inspect(parts, false, 4, true));
// push the token parts to the
normalizedTokens.push(parts);
}
Expand Down
23 changes: 20 additions & 3 deletions lib/search.js
@@ -1,6 +1,7 @@
var redis = require('redis');
var client = redis.createClient('run/redis.sock');
var normalize = require('./normalize.js');
var util = require('util');

module.exports = function (query, callback) {
var queryTokens = normalize(query);
Expand All @@ -11,7 +12,23 @@ module.exports = function (query, callback) {
});
});

var args = keys;
args.push(callback);
client.sinter.apply(client, args);
var args = [];
// Create key for the stored search
var searchKey = 'S:' + keys.join('|');
args.push(searchKey);
// 2nd argument is the number of keys we want to "UNION"
args.push(keys.length);
// we merge the keys array into args array
// keys beginning with index 2
args = args.concat(keys);
// last argument is the callback
args.push(getStoreCallback(searchKey, callback));
client.zunionstore.apply(client, args);
}

function getStoreCallback(searchKey, callback) {
return function (err, replies) {
if (err) return callback(err, null);
client.zrange(searchKey, 0, -1, callback);
}
}
5 changes: 3 additions & 2 deletions test-search.js
@@ -1,10 +1,11 @@
var util = require('util');
var search = require('./lib/search');
var query = "detail";

var query = "comparing";

search(query, function(error, replies) {
if(error) return console.error(error);
else console.log(util.inspect(replies, false, 4, true));
else console.log("TESTSEARCH", util.inspect(replies, false, 4, true));
});


0 comments on commit 348d721

Please sign in to comment.