2014-05-07 10:29:47 -05:00
|
|
|
var lunr = require('lunr')
|
|
|
|
, localList = require('./local-list');
|
|
|
|
|
2014-05-08 11:13:39 -05:00
|
|
|
var Search = function() {
|
2014-05-07 10:29:47 -05:00
|
|
|
this.index = lunr(function () {
|
2014-05-08 11:13:39 -05:00
|
|
|
this.field('name', {boost: 10});
|
|
|
|
this.field('description', {boost: 4});
|
|
|
|
this.field('author', {boost: 6});
|
|
|
|
this.field('readme');
|
2014-05-07 10:29:47 -05:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
Search.prototype = {
|
|
|
|
query: function(q) {
|
2014-05-07 13:08:29 -05:00
|
|
|
return this.index.search(q);
|
2014-05-07 10:29:47 -05:00
|
|
|
},
|
2014-05-07 13:08:29 -05:00
|
|
|
add: function(package) {
|
2014-05-08 16:34:16 -05:00
|
|
|
this.index.add({
|
|
|
|
id: package.name,
|
|
|
|
name: package.name,
|
|
|
|
description: package.description,
|
|
|
|
author: package._npmUser.name
|
2014-05-07 10:29:47 -05:00
|
|
|
});
|
2014-05-07 13:08:29 -05:00
|
|
|
},
|
2014-05-08 16:48:15 -05:00
|
|
|
remove: function(name) {
|
|
|
|
this.index.remove({
|
|
|
|
id: name
|
|
|
|
});
|
|
|
|
},
|
2014-05-07 13:08:29 -05:00
|
|
|
reindex: function() {
|
|
|
|
var self = this;
|
|
|
|
this.storage.get_local(function(err, packages) {
|
|
|
|
var i = packages.length;
|
|
|
|
|
|
|
|
while(i--) {
|
|
|
|
self.add(packages[i]);
|
|
|
|
}
|
|
|
|
});
|
2014-05-08 11:13:39 -05:00
|
|
|
},
|
|
|
|
configureStorage: function(storage) {
|
|
|
|
this.storage = storage;
|
|
|
|
this.reindex();
|
2014-05-07 10:29:47 -05:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2014-05-08 11:13:39 -05:00
|
|
|
module.exports = new Search();
|