diff --git a/static/search.js b/static/search.js index effb4e0a..127a8f5a 100644 --- a/static/search.js +++ b/static/search.js @@ -350,32 +350,35 @@ var Search = Control.extend({ var searchTerm = value.toLowerCase(); var self = this; return this.searchEnginePromise.then(function(searchEngine) { - return searchEngine - //run the search - .query(function(q) { - if (searchTerm.indexOf('can-') > -1) {// If the search term includes “can-” + //run the search + var queryResults = searchEngine.query(function(q) { - // look for an exact match and apply a large positive boost - q.term(searchTerm, { usePipeline: true, boost: 375 }); + if (searchTerm.indexOf('can-') > -1) {// If the search term includes “can-” - } else { - // add “can-”, look for an exact match in the title field, and apply a positive boost - q.term('can-' + searchTerm, { usePipeline: false, fields: ['title'], boost: 12 }); - } + // look for an exact match and apply a large positive boost + q.term(searchTerm, { usePipeline: true, boost: 375 }); + + } else { + // add “can-”, look for an exact match in the title field, and apply a positive boost + q.term('can-' + searchTerm, { usePipeline: false, fields: ['title'], boost: 12 }); // look for terms that match the beginning or end of this query q.term('*' + searchTerm + '*', { usePipeline: false }); + } + + // look for matches in any of the fields and apply a medium positive boost + var split = searchTerm.split(lunr.tokenizer.separator); + split.forEach(function(term) { + q.term(term, { usePipeline: false, fields: q.allFields, boost: 10 }); + q.term(term + '*', { usePipeline: false, fields: q.allFields }); + }); + }); + + //convert the results into a searchMap subset + var mappedResults = queryResults.map(function(result){ return self.searchMap[result.ref] }); - // look for matches in any of the fields and apply a medium positive boost - var split = searchTerm.split(lunr.tokenizer.separator); - split.forEach(function(term) { - q.term(term, { usePipeline: false, fields: q.allFields, boost: 10 }); - q.term(term + '*', { usePipeline: false, fields: q.allFields }); - }); - }) - //convert the results into a searchMap subset - .map(function(result){ return self.searchMap[result.ref] }); + return mappedResults; }); }, diff --git a/test/search.js b/test/search.js index a80acbd5..f2baca61 100644 --- a/test/search.js +++ b/test/search.js @@ -94,3 +94,13 @@ QUnit.test('Search for “%special”', function(assert) { done(); }); }); + +QUnit.test('Speed while searching for can-*', function(assert) { + var done = assert.async(); + var startTime = new Date(); + search.searchEngineSearch('can-zone').then(function() { + var totalTime = new Date() - startTime; + assert.equal(totalTime < 300, true, 'less than 300 milliseconds'); + done(); + }); +});