[Search] Changed provider return type

The provider now returns an object that has
a hits property which contains what it
previously returned, and also a total property
which contains the total number of results.
This commit is contained in:
slhale 2015-07-30 13:54:56 -07:00
parent a7cd6d8807
commit 60dad014cc
3 changed files with 29 additions and 19 deletions

View File

@ -125,13 +125,13 @@ define(
} }
// Get promises for results arrays // Get promises for results arrays
return $q.all(resultPromises).then(function (resultsArrays) { return $q.all(resultPromises).then(function (resultObjects) {
var results = [], var results = [],
i; i;
// Merge results // Merge results
for (i = 0; i < resultsArrays.length; i += 1) { for (i = 0; i < resultObjects.length; i += 1) {
results = results.concat(resultsArrays[i]); results = results.concat(resultObjects[i].hits);
} }
results = filterRepeats(results); results = filterRepeats(results);
results = orderByScore(results); results = orderByScore(results);

View File

@ -89,7 +89,8 @@ define(
} }
// Processes results from the format that elasticsearch returns to // Processes results from the format that elasticsearch returns to
// a list of search result objects (that contain domain objects) // a list of search result objects (that contain domain objects), then
// returns an object in the format {hits: searchResult[], total: number}
function processResults(rawResults, timestamp) { function processResults(rawResults, timestamp) {
var results = rawResults.data.hits.hits, var results = rawResults.data.hits.hits,
resultsLength = results.length, resultsLength = results.length,
@ -98,13 +99,6 @@ define(
searchResults = [], searchResults = [],
i; i;
/*
if (rawResults.data.hits.total > resultsLength) {
// TODO: Somehow communicate this to the user
//console.log('Total number of results greater than displayed results');
}
*/
// Get the result objects' IDs // Get the result objects' IDs
for (i = 0; i < resultsLength; i += 1) { for (i = 0; i < resultsLength; i += 1) {
ids.push(results[i][ID]); ids.push(results[i][ID]);
@ -134,7 +128,10 @@ define(
} }
} }
return searchResults; return {
hits: searchResults,
total: rawResults.data.hits.total
};
}); });
} }
@ -151,7 +148,7 @@ define(
// 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 (searchTerm !== '') { if (searchTerm !== '') {
// Process search term // Process the search term
searchTerm = processSearchTerm(searchTerm); searchTerm = processSearchTerm(searchTerm);
// Create the query to elasticsearch // Create the query to elasticsearch
@ -170,7 +167,7 @@ define(
return processResults(rawResults, timestamp); return processResults(rawResults, timestamp);
}); });
} else { } else {
return []; return {hits: [], total: 0};
} }
} }
@ -178,7 +175,11 @@ define(
/** /**
* Searches through the filetree for domain objects using a search * Searches through the filetree for domain objects using a search
* term. This is done through querying elasticsearch. Returns a * term. This is done through querying elasticsearch. Returns a
* promise for an array of domain object results. * promise for a result object that has the format
* {hits: searchResult[], total: number}
* where a searchResult has the format
* {id: domainObject ID, object: domainObject, score: number}
*
* Notes: * Notes:
* * The order of the results is from highest to lowest score, * * The order of the results is from highest to lowest score,
* as elsaticsearch determines them to be. * as elsaticsearch determines them to be.

View File

@ -92,12 +92,12 @@ define(
ids.push(id); ids.push(id);
} }
objectService.getObjects(ids).then(function (objects) { objectService.getObjects(ids).then(function (objects) {
var results = [], var searchResults = [],
id; id;
// Reset and repopulate the latest results // Reset and repopulate the latest results
for (id in objects) { for (id in objects) {
results.push({ searchResults.push({
object: objects[id], object: objects[id],
id: id, id: id,
score: event.data.results[id] score: event.data.results[id]
@ -105,7 +105,12 @@ define(
} }
// Resove the promise corresponding to this // Resove the promise corresponding to this
pendingQueries[event.data.timestamp].resolve(results); pendingQueries[event.data.timestamp].resolve(
{
hits: searchResults,
total: searchResults.length // TODO: Make worker return this
}
);
}); });
} }
} }
@ -192,7 +197,11 @@ define(
* Searches through the filetree for domain objects which match * Searches through the filetree for domain objects which match
* the search term. This function is to be used as a fallback * the search term. This function is to be used as a fallback
* in the case where other search services are not avaliable. * in the case where other search services are not avaliable.
* Returns a promise for an array of domain object results. * Returns a promise for a result object that has the format
* {hits: searchResult[], total: number}
* where a searchResult has the format
* {id: domainObject ID, object: domainObject, score: number}
*
* Notes: * Notes:
* * The order of the results is not guarenteed. * * The order of the results is not guarenteed.
* * A domain object qualifies as a match for a search input if * * A domain object qualifies as a match for a search input if