123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133 |
- "use strict";
- const Promise = require('bluebird'),
- _ = require('lodash'),
- path = require('path');
- /**
- * Search Model
- */
- module.exports = {
- _si: null,
- /**
- * Initialize Search model
- *
- * @param {Object} appconfig The application config
- * @return {Object} Search model instance
- */
- init(appconfig) {
- let self = this;
- return self;
- },
- find(terms) {
- let self = this;
- terms = _.chain(terms)
- .deburr()
- .toLower()
- .trim()
- .replace(/[^a-z0-9 ]/g, '')
- .split(' ')
- .filter((f) => { return !_.isEmpty(f); })
- .join(' ')
- .value();
- return db.Entry.find(
- { $text: { $search: terms } },
- { score: { $meta: "textScore" }, title: 1 }
- )
- .sort({ score: { $meta: "textScore" } })
- .limit(10)
- .exec()
- .then((hits) => {
- /*if(hits.length < 5) {
- return self._si.matchAsync({
- beginsWith: terms,
- threshold: 3,
- limit: 5,
- type: 'simple'
- }).then((matches) => {
- return {
- match: hits,
- suggest: matches
- };
- });
- } else {*/
- return {
- match: hits,
- suggest: []
- };
- //}
- }).catch((err) => {
- if(err.type === 'NotFoundError') {
- return {
- match: [],
- suggest: []
- };
- } else {
- winston.error(err);
- }
- });
- },
- /**
- * Delete an entry from the index
- *
- * @param {String} The entry path
- * @return {Promise} Promise of the operation
- */
- delete(entryPath) {
- let self = this;
- /*let hasResults = false;
- return new Promise((resolve, reject) => {
- self._si.search({
- query: {
- AND: { 'entryPath': [entryPath] }
- }
- }).on('data', (results) => {
- hasResults = true;
- if(results.totalHits > 0) {
- let delIds = _.map(results.hits, 'id');
- self._si.del(delIds).on('end', () => { return resolve(true); });
- } else {
- resolve(true);
- }
- }).on('error', (err) => {
- if(err.type === 'NotFoundError') {
- resolve(true);
- } else {
- winston.error(err);
- reject(err);
- }
- }).on('end', () => {
- if(!hasResults) {
- resolve(true);
- }
- });
- });*/
- }
- };
|