Skip to content

Commit 30f3bb1

Browse files
authored
Merge pull request #301 from canjs/search-speed
Improve the speed of searching for can-*
2 parents 08cf10b + bf406e5 commit 30f3bb1

File tree

2 files changed

+32
-19
lines changed

2 files changed

+32
-19
lines changed

static/search.js

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -350,32 +350,35 @@ var Search = Control.extend({
350350
var searchTerm = value.toLowerCase();
351351
var self = this;
352352
return this.searchEnginePromise.then(function(searchEngine) {
353-
return searchEngine
354-
//run the search
355-
.query(function(q) {
356353

357-
if (searchTerm.indexOf('can-') > -1) {// If the search term includes “can-”
354+
//run the search
355+
var queryResults = searchEngine.query(function(q) {
358356

359-
// look for an exact match and apply a large positive boost
360-
q.term(searchTerm, { usePipeline: true, boost: 375 });
357+
if (searchTerm.indexOf('can-') > -1) {// If the search term includes “can-”
361358

362-
} else {
363-
// add “can-”, look for an exact match in the title field, and apply a positive boost
364-
q.term('can-' + searchTerm, { usePipeline: false, fields: ['title'], boost: 12 });
365-
}
359+
// look for an exact match and apply a large positive boost
360+
q.term(searchTerm, { usePipeline: true, boost: 375 });
361+
362+
} else {
363+
// add “can-”, look for an exact match in the title field, and apply a positive boost
364+
q.term('can-' + searchTerm, { usePipeline: false, fields: ['title'], boost: 12 });
366365

367366
// look for terms that match the beginning or end of this query
368367
q.term('*' + searchTerm + '*', { usePipeline: false });
368+
}
369+
370+
// look for matches in any of the fields and apply a medium positive boost
371+
var split = searchTerm.split(lunr.tokenizer.separator);
372+
split.forEach(function(term) {
373+
q.term(term, { usePipeline: false, fields: q.allFields, boost: 10 });
374+
q.term(term + '*', { usePipeline: false, fields: q.allFields });
375+
});
376+
});
377+
378+
//convert the results into a searchMap subset
379+
var mappedResults = queryResults.map(function(result){ return self.searchMap[result.ref] });
369380

370-
// look for matches in any of the fields and apply a medium positive boost
371-
var split = searchTerm.split(lunr.tokenizer.separator);
372-
split.forEach(function(term) {
373-
q.term(term, { usePipeline: false, fields: q.allFields, boost: 10 });
374-
q.term(term + '*', { usePipeline: false, fields: q.allFields });
375-
});
376-
})
377-
//convert the results into a searchMap subset
378-
.map(function(result){ return self.searchMap[result.ref] });
381+
return mappedResults;
379382
});
380383
},
381384

test/search.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,3 +94,13 @@ QUnit.test('Search for “%special”', function(assert) {
9494
done();
9595
});
9696
});
97+
98+
QUnit.test('Speed while searching for can-*', function(assert) {
99+
var done = assert.async();
100+
var startTime = new Date();
101+
search.searchEngineSearch('can-zone').then(function() {
102+
var totalTime = new Date() - startTime;
103+
assert.equal(totalTime < 300, true, 'less than 300 milliseconds');
104+
done();
105+
});
106+
});

0 commit comments

Comments
 (0)