[Search] Generic tracks total hits

The generic search worker keeps track of the total
number of search hits before truncating to match
the max results.
This commit is contained in:
slhale
2015-07-30 14:04:23 -07:00
parent 60dad014cc
commit 51bc7c6a7f
2 changed files with 13 additions and 7 deletions

View File

@ -108,7 +108,7 @@ define(
pendingQueries[event.data.timestamp].resolve( pendingQueries[event.data.timestamp].resolve(
{ {
hits: searchResults, hits: searchResults,
total: searchResults.length // TODO: Make worker return this total: event.data.total
} }
); );
}); });

View File

@ -132,14 +132,13 @@
var results = {}, var results = {},
input = data.input.toLocaleLowerCase(), input = data.input.toLocaleLowerCase(),
terms = convertToTerms(input), terms = convertToTerms(input),
timesToLoop = Math.min(indexedItems.length, data.maxNumber), total,
i, i,
score, score;
message;
// If the user input is empty, we want to have no search results. // If the user input is empty, we want to have no search results.
if (input !== '') { if (input !== '') {
for (i = 0; i < timesToLoop; i += 1) { for (i = 0; i < indexedItems.length; i += 1) {
score = scoreItem(indexedItems[i], input, terms); score = scoreItem(indexedItems[i], input, terms);
if (score > 0) { if (score > 0) {
results[indexedItems[i].id] = score; results[indexedItems[i].id] = score;
@ -147,12 +146,19 @@
} }
} }
message = { // Store the total hits number
total = results.length;
// Truncate results if there are more than maxResults
if (results.length > data.maxNumber) {
results = results.slice(0, data.maxNumber);
}
return {
request: 'search', request: 'search',
results: results, results: results,
total: total,
timestamp: data.timestamp timestamp: data.timestamp
}; };
return message;
// TODO: After a search is completed, do we need to // TODO: After a search is completed, do we need to
// clear out indexedItems? // clear out indexedItems?