WebSocket server + Search index + indexable content parser
This commit is contained in:
		| @@ -183,7 +183,7 @@ module.exports = { | ||||
| 	 * @param      {String}  entryPath  The entry path | ||||
| 	 * @return     {String}  Text-only version | ||||
| 	 */ | ||||
| 	fetchTextVersion(entryPath) { | ||||
| 	fetchIndexableVersion(entryPath) { | ||||
|  | ||||
| 		let self = this; | ||||
|  | ||||
| @@ -192,11 +192,13 @@ module.exports = { | ||||
| 			parseMeta: true, | ||||
| 			parseTree: false, | ||||
| 			includeMarkdown: true, | ||||
| 			includeParentInfo: false, | ||||
| 			includeParentInfo: true, | ||||
| 			cache: false | ||||
| 		}).then((pageData) => { | ||||
| 			return { | ||||
| 				entryPath, | ||||
| 				meta: pageData.meta, | ||||
| 				parent: pageData.parent || {}, | ||||
| 				text: mark.removeMarkdown(pageData.markdown) | ||||
| 			}; | ||||
| 		}); | ||||
|   | ||||
| @@ -160,7 +160,7 @@ module.exports = { | ||||
|  | ||||
| 				} else { | ||||
|  | ||||
| 					winston.info('[GIT] Repository is already in sync.'); | ||||
| 					winston.info('[GIT] Push skipped. Repository is already in sync.'); | ||||
|  | ||||
| 				} | ||||
|  | ||||
|   | ||||
| @@ -3,7 +3,7 @@ | ||||
| var Promise = require('bluebird'), | ||||
| 	_ = require('lodash'), | ||||
| 	path = require('path'), | ||||
| 	searchIndex = Promise.promisifyAll(require('search-index')), | ||||
| 	searchIndex = require('search-index'), | ||||
| 	stopWord = require('stopword'); | ||||
|  | ||||
| /** | ||||
| @@ -21,9 +21,10 @@ module.exports = { | ||||
| 	 */ | ||||
| 	init(appconfig) { | ||||
|  | ||||
| 		let self = this; | ||||
| 		let dbPath = path.resolve(ROOTPATH, appconfig.datadir.db, 'search-index'); | ||||
|  | ||||
| 		this._si = searchIndex({ | ||||
| 		searchIndex({ | ||||
| 			deletable: true, | ||||
| 			fieldedSearch: true, | ||||
| 			indexPath: dbPath, | ||||
| @@ -32,11 +33,86 @@ module.exports = { | ||||
| 		}, (err, si) => { | ||||
| 			if(err) { | ||||
| 				winston.error('Failed to initialize search-index.', err); | ||||
| 			} else { | ||||
| 				self._si = Promise.promisifyAll(si); | ||||
| 			} | ||||
| 		}); | ||||
|  | ||||
| 		return self; | ||||
|  | ||||
| 	}, | ||||
|  | ||||
| 	find(terms) { | ||||
|  | ||||
| 		let self = this; | ||||
| 		terms = _.chain(terms) | ||||
| 							.deburr() | ||||
| 							.toLower() | ||||
| 							.trim() | ||||
| 							.replace(/[^a-z0-9 ]/g, '') | ||||
| 							.split(' ') | ||||
| 							.filter((f) => { return !_.isEmpty(f); }) | ||||
| 							.value(); | ||||
|  | ||||
| 		return self._si.searchAsync({ | ||||
| 			query: { | ||||
| 				AND: [{ '*': terms }] | ||||
| 			}, | ||||
| 			pageSize: 10 | ||||
| 		}).get('hits'); | ||||
|  | ||||
| 	}, | ||||
|  | ||||
| 	/** | ||||
| 	 * Add a document to the index | ||||
| 	 * | ||||
| 	 * @param      {Object}   content  Document content | ||||
| 	 * @return     {Promise}  Promise of the add operation | ||||
| 	 */ | ||||
| 	add(content) { | ||||
|  | ||||
| 		let self = this; | ||||
|  | ||||
| 		return self._si.addAsync({ | ||||
| 			entryPath: content.entryPath, | ||||
| 			title: content.meta.title, | ||||
| 			subtitle: content.meta.subtitle || '', | ||||
| 			parent: content.parent.title || '', | ||||
| 			content: content.text || '' | ||||
| 		}, { | ||||
| 			fieldOptions: [{ | ||||
| 				fieldName: 'entryPath', | ||||
| 				searchable: true, | ||||
| 				weight: 2 | ||||
| 			}, | ||||
| 			{ | ||||
| 				fieldName: 'title', | ||||
| 				nGramLength: [1, 2], | ||||
| 				searchable: true, | ||||
| 				weight: 3 | ||||
| 			}, | ||||
| 			{ | ||||
| 				fieldName: 'subtitle', | ||||
| 				searchable: true, | ||||
| 				weight: 1, | ||||
| 				store: false | ||||
| 			}, | ||||
| 			{ | ||||
| 				fieldName: 'subtitle', | ||||
| 				searchable: false, | ||||
| 			}, | ||||
| 			{ | ||||
| 				fieldName: 'content', | ||||
| 				searchable: true, | ||||
| 				weight: 0, | ||||
| 				store: false | ||||
| 			}] | ||||
| 		}).then(() => { | ||||
| 			winston.info('Entry ' + content.entryPath + ' added to index.'); | ||||
| 		}).catch((err) => { | ||||
| 			winston.error(err); | ||||
| 		}); | ||||
|  | ||||
| 	} | ||||
|  | ||||
|  | ||||
|  | ||||
| }; | ||||
		Reference in New Issue
	
	Block a user