[Search] Aggregator aggregates

The search aggregator now combines the search
results from each search provider and returns
that. Objects may appear in the list more
than once.
This commit is contained in:
shale
2015-07-16 11:31:11 -07:00
parent cd3c2312a5
commit 25208a9074
2 changed files with 34 additions and 51 deletions

View File

@ -38,60 +38,41 @@ define(
* aggregated * aggregated
*/ */
function SearchAggregator($q, providers) { function SearchAggregator($q, providers) {
return {
query: providers[0].query
};
/* function getPromisedResults(resultsPromises, promiseIndex, finalResults) {
// Pick a domain object model to use, favoring the one if (promiseIndex >= resultsPromises.length) {
// with the most recent timestamp return finalResults;
function pick(a, b) { } else {
var aModified = (a || {}).modified || Number.NEGATIVE_INFINITY, return resultsPromises[promiseIndex].then(function (results) {
bModified = (b || {}).modified || Number.NEGATIVE_INFINITY; finalResults = finalResults.concat(results);
return (aModified > bModified) ? a : (b || a); return getPromisedResults(resultsPromises, promiseIndex + 1, finalResults);
});
}
} }
// Merge results from multiple providers into one // Calls the searches of each of the providers, then
// large result object. // merges the results lists so that there are not redundant
function mergeModels(provided, ids) { // results
var result = {}; function mergeResults(inputID) {
ids.forEach(function (id) { var resultsPromises = [],
provided.forEach(function (models) { mergedResults = [];
if (models[id]) {
result[id] = pick(result[id], models[id]); for (var i = 0; i < providers.length; i += 1) {
resultsPromises.push(providers[i].query(inputID));
} }
mergedResults = getPromisedResults(resultsPromises, 0, []);
//return mergedResults;
return mergedResults.then(function (c) {
//console.log('returning ', c);
return c;
}); });
});
return result;
} }
return { return {
/** query: mergeResults
* Get models with the specified identifiers.
*
* This will invoke the `getModels()` method of all providers
* given at constructor-time, and aggregate the result into
* one object.
*
* Note that the returned object may contain a subset or a
* superset of the models requested.
*
* @param {string[]} ids an array of domain object identifiers
* @returns {Promise.<object>} a promise for an object
* containing key-value pairs,
* where keys are object identifiers and values
* are object models.
*/
/*
getModels: function (ids) {
return $q.all(providers.map(function (provider) {
return provider.getModels(ids);
})).then(function (provided) {
return mergeModels(provided, ids);
});
}
}; };
*/
} }
return SearchAggregator; return SearchAggregator;

View File

@ -29,6 +29,8 @@ define(
function () { function () {
"use strict"; "use strict";
var DEFAULT_MAX_RESULTS = 100;
/** /**
* A model service which reads domain object models from an external * A model service which reads domain object models from an external
* persistence service. * persistence service.