3 var Promise = require('bluebird');
4 var _ = require('lodash');
5 var languageDetector = require('./language-detector');
6 var languageAnalyzerMapper = require('./language-analyzer-mapper');
7 var chatsForRoomSearch = require('gitter-web-elasticsearch/lib/chats-for-room-search');
8 var debug = require('debug')('gitter:app:chat-search-service');
10 var parseQuery = Promise.method(function(textQuery, userLang) {
11 /* Horrible hack - rip a from:xyz field out of the textQuery */
13 textQuery = textQuery.replace(/\bfrom:('[^']*'|"[^"]*"|[^'"]\w*)/g, function(
25 analyzers: ['default']
29 return languageDetector(textQuery).then(function(detectedLanguage) {
30 var analyzers = { default: true };
33 analyzers[languageAnalyzerMapper(userLang)] = true;
36 if (detectedLanguage) {
37 analyzers[languageAnalyzerMapper(detectedLanguage)] = true;
41 queryString: textQuery,
43 analyzers: Object.keys(analyzers)
49 * Search for messages in a room using a full-text index.
51 * Returns promise messages
53 exports.searchChatMessagesForRoom = function(troupeId, textQuery, options) {
54 if (!options) options = {};
56 options = _.defaults(options, {
61 return parseQuery(textQuery, options.lang).then(function(parsedQuery) {
62 debug('Search: %j', parsedQuery);
64 // TODO: deal with limit and skip
65 var searchResults = chatsForRoomSearch.searchRoom(troupeId, parsedQuery, options);