search.js 789 B

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. "use strict";
  2. var Promise = require('bluebird'),
  3. _ = require('lodash'),
  4. path = require('path'),
  5. searchIndex = Promise.promisifyAll(require('search-index')),
  6. stopWord = require('stopword');
  7. /**
  8. * Search Model
  9. */
  10. module.exports = {
  11. _si: null,
  12. /**
  13. * Initialize Search model
  14. *
  15. * @param {Object} appconfig The application config
  16. * @return {Object} Search model instance
  17. */
  18. init(appconfig) {
  19. let dbPath = path.resolve(ROOTPATH, appconfig.datadir.db, 'search-index');
  20. this._si = searchIndex({
  21. deletable: true,
  22. fieldedSearch: true,
  23. indexPath: dbPath,
  24. logLevel: 'error',
  25. stopwords: stopWord.getStopwords(appconfig.lang).sort()
  26. }, (err, si) => {
  27. if(err) {
  28. winston.error('Failed to initialize search-index.', err);
  29. }
  30. });
  31. }
  32. };