Skip to content

Commit 443d2f9

Browse files
Chasen Le Harachasenlehara
authored andcommitted
Improve search results for terms like “restModel”
This improves the search results for queries such as restModel, QueryLogic, and DefineMap. This is done by: 1) Concatenating the module’s name (e.g. `can-define/map/map` is converted to `definemapmap`) and indexing the result 2) Giving preference to packages over other pages/results Fixes #499
1 parent 18d3a3a commit 443d2f9

File tree

3 files changed

+53
-2
lines changed

3 files changed

+53
-2
lines changed

static/search-logic.js

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,18 @@ module.exports = {
1313

1414
this.ref('name');
1515
this.field('title');
16+
this.field('concatenatedName');
1617
this.field('description');
18+
this.field('isPackage');
1719
this.field('name');
1820
this.field('url');
1921

2022
items.forEach(function(item) {
2123
if (!item.title) {
2224
item.title = item.name;
2325
}
26+
item.concatenatedName = item.name.replace('can-', '').replace(/-/g, '').replace(/\//g, '');
27+
item.isPackage = (item.collection !== undefined).toString();
2428
this.add(item);
2529
}.bind(this));
2630
});
@@ -47,9 +51,16 @@ module.exports = {
4751
q.term('can-' + searchTerm, { boost: 12 });
4852

4953
// look for terms that match the beginning or end of this query
54+
q.term(searchTerm, { wildcard: lunr.Query.wildcard.LEADING | lunr.Query.wildcard.TRAILING });
55+
5056
// look in the title field specifically to boost matches in it
5157
q.term(searchTerm, { fields: ['title'], wildcard: lunr.Query.wildcard.LEADING | lunr.Query.wildcard.TRAILING });
52-
q.term(searchTerm, { wildcard: lunr.Query.wildcard.LEADING | lunr.Query.wildcard.TRAILING });
58+
59+
// support searches for strings like “restModel”
60+
q.term(searchTerm, { fields: ['concatenatedName'], wildcard: lunr.Query.wildcard.TRAILING });
61+
62+
// favor modules
63+
q.term('true', { boost: 6, fields: ['isPackage'] });
5364
}
5465

5566
// look for matches in any of the fields and apply a medium positive boost

static/search.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ var Control = require("can-control");
44
var LoadingBar = require('./loading-bar');
55
var searchResultsRenderer = require("../templates/search-results.stache!steal-stache");
66
var joinURIs = require("can-util/js/join-uris/");
7-
var currentIndexVersion = 4;// Bump this whenever the index code is changed
7+
var currentIndexVersion = 5;// Bump this whenever the index code is changed
88

99
var Search = Control.extend({
1010

test/search.js

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,46 @@ QUnit.test('Search for “define/map”', function(assert) {
141141
});
142142
});
143143

144+
QUnit.test('Search for “restModel”', function(assert) {
145+
var done = assert.async();
146+
setUpSearchControl.then(function() {
147+
var results = searchLogic.search('restModel');
148+
assert.equal(results.length > 1, true, 'got more than 1 result');
149+
assert.equal(indexOfPageInResults('can-rest-model', results), 0, 'first result is the can-rest-model page');
150+
done();
151+
});
152+
});
153+
154+
QUnit.test('Search for “QueryLogic”', function(assert) {
155+
var done = assert.async();
156+
setUpSearchControl.then(function() {
157+
var results = searchLogic.search('QueryLogic');
158+
assert.equal(results.length > 1, true, 'got more than 1 result');
159+
assert.equal(indexOfPageInResults('can-query-logic', results), 0, 'first result is the can-query-logic page');
160+
done();
161+
});
162+
});
163+
164+
QUnit.test('Search for “DefineList”', function(assert) {
165+
var done = assert.async();
166+
setUpSearchControl.then(function() {
167+
var results = searchLogic.search('DefineList');
168+
assert.equal(results.length > 1, true, 'got more than 1 result');
169+
assert.equal(indexOfPageInResults('can-define/list/list', results), 0, 'first result is the can-define/list/list page');
170+
done();
171+
});
172+
});
173+
174+
QUnit.test('Search for “DefineMap”', function(assert) {
175+
var done = assert.async();
176+
setUpSearchControl.then(function() {
177+
var results = searchLogic.search('DefineMap');
178+
assert.equal(results.length > 1, true, 'got more than 1 result');
179+
assert.equal(indexOfPageInResults('can-define/map/map', results), 0, 'first result is the can-define/map/map page');
180+
done();
181+
});
182+
});
183+
144184
QUnit.test('Speed while searching for can-*', function(assert) {
145185
var done = assert.async();
146186
setUpSearchControl.then(function() {

0 commit comments

Comments
 (0)