0
Fork 0
mirror of https://github.com/verdaccio/verdaccio.git synced 2024-12-16 21:56:25 -05:00
verdaccio/lib/search.js

46 lines
920 B
JavaScript
Raw Normal View History

2014-05-07 10:29:47 -05:00
var lunr = require('lunr')
var Search = function() {
2014-05-07 10:29:47 -05:00
this.index = lunr(function () {
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) {
this.index.add({
id: package.name,
name: package.name,
description: package.description,
2014-08-07 20:58:25 -05:00
author: package._npmUser ? 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]);
}
});
},
configureStorage: function(storage) {
this.storage = storage;
this.reindex();
2014-05-07 10:29:47 -05:00
}
};
2014-08-07 20:58:25 -05:00
module.exports = new Search();