Skip to content

Improve the speed of searching for can-* #301

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 5, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 22 additions & 19 deletions static/search.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
});
},

Expand Down
10 changes: 10 additions & 0 deletions test/search.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});
});