From a55607dc23d4a4e78740bca085665e0068539b71 Mon Sep 17 00:00:00 2001 From: slhale Date: Fri, 28 Aug 2015 15:12:06 -0700 Subject: [PATCH] [Search] Worker presorts search results Changed the generic search worker to sort the search results before truncating the results list to match maxResults. #91. --- .../src/services/GenericSearchProvider.js | 29 +++++++++-------- .../src/services/GenericSearchWorker.js | 32 +++++++++++-------- 2 files changed, 33 insertions(+), 28 deletions(-) diff --git a/platform/search/src/services/GenericSearchProvider.js b/platform/search/src/services/GenericSearchProvider.js index 014d8d7fda..4b26af4dd0 100644 --- a/platform/search/src/services/GenericSearchProvider.js +++ b/platform/search/src/services/GenericSearchProvider.js @@ -79,27 +79,28 @@ define( // Handles responses from the web worker. Namely, the results of // a search request. function handleResponse(event) { - var ids = [], - id; + var ids = []; // If we have the results from a search if (event.data.request === 'search') { - // Convert the ids given from the web worker into domain objects - for (id in event.data.results) { - ids.push(id); - } + + // Get the ids into an array + event.data.results.forEach(function (result) { + ids.push(result.id); + }); + + // Get domainObjects with the ids objectService.getObjects(ids).then(function (objects) { - var searchResults = [], - id; + var searchResults = []; - // Create searchResult objects - for (id in objects) { + // Create searchResult objects with the gotten domainObjects + event.data.results.forEach(function (result) { searchResults.push({ - object: objects[id], - id: id, - score: event.data.results[id] + object: objects[result.id], + id: result.id, + score: result.score }); - } + }); // Resove the promise corresponding to this pendingQueries[event.data.timestamp].resolve({ diff --git a/platform/search/src/services/GenericSearchWorker.js b/platform/search/src/services/GenericSearchWorker.js index 57be98b423..469dfbf895 100644 --- a/platform/search/src/services/GenericSearchWorker.js +++ b/platform/search/src/services/GenericSearchWorker.js @@ -93,14 +93,14 @@ * * timestamp: The time identifier from when the query was made */ function search(data) { - // This results dictionary will have domain object ID keys which - // point to the value the domain object's score. - var results = {}, + // This results array will have domain object IDs and scores together + // [{score: number, id: string}] + var results = [], input = data.input.toLocaleLowerCase(), terms = convertToTerms(input), message = { request: 'search', - results: {}, + results: [], total: 0, timestamp: data.timestamp, timedOut: false @@ -111,6 +111,7 @@ // If the user input is empty, we want to have no search results. if (input !== '') { + // Start getting search results for (i = 0; i < indexedItems.length; i += 1) { // If this is taking too long, then stop if (Date.now() > data.timestamp + data.timeout) { @@ -121,23 +122,26 @@ // Score and add items score = scoreItem(indexedItems[i], input, terms); if (score > 0) { - results[indexedItems[i].id] = score; + results.push({id: indexedItems[i].id, score: score}); message.total += 1; } } } + // Sort the results by score + results.sort(function (a, b) { + if (a.score > b.score) { + return -1; + } else if (b.score > a.score) { + return 1; + } else { + return 0; + } + }); + // Truncate results if there are more than maxResults if (message.total > data.maxResults) { - i = 0; - for (id in results) { - message.results[id] = results[id]; - i += 1; - if (i >= data.maxResults) { - break; - } - } - // TODO: This seems inefficient. + message.results = results.slice(0, data.maxResults); } else { message.results = results; }