[Serach] Callbacks and renaming

Added validType() as a callback parameter to the search
providers. It is defined in the search aggregator, and
is used to put any restrictions on object type in the
displayed search results.
Renamed EverythingSearchProvider to GenericSearchProvider.
This commit is contained in:
shale 2015-07-17 11:24:33 -07:00
parent 492dbcbc51
commit c75d94289b
4 changed files with 39 additions and 34 deletions

View File

@ -47,7 +47,7 @@
{ {
"provides": "searchService", "provides": "searchService",
"type": "provider", "type": "provider",
"implementation": "providers/EverythingSearchProvider.js", "implementation": "providers/GenericSearchProvider.js",
"depends": [ "objectService" ] "depends": [ "objectService" ]
}, },
{ {

View File

@ -39,15 +39,12 @@ define(
*/ */
function SearchAggregator(providers) { function SearchAggregator(providers) {
function getPromisedResults(resultsPromises, promiseIndex, finalResults) { // Determines if a searchResult object is a valid type
if (promiseIndex >= resultsPromises.length) { // to be displayed as a final result. Is passed to the
return finalResults; // search providers as an argument.
} else { function validType(model) {
return resultsPromises[promiseIndex].then(function (results) { // Nothing is currently disallowed
finalResults = finalResults.concat(results); return true;
return getPromisedResults(resultsPromises, promiseIndex + 1, finalResults);
});
}
} }
// Remove extra objects that have the same ID // Remove extra objects that have the same ID
@ -85,6 +82,17 @@ define(
return results; return results;
} }
function getPromisedResults(resultsPromises, promiseIndex, finalResults) {
if (promiseIndex >= resultsPromises.length) {
return finalResults;
} else {
return resultsPromises[promiseIndex].then(function (results) {
finalResults = finalResults.concat(results);
return getPromisedResults(resultsPromises, promiseIndex + 1, finalResults);
});
}
}
// Recieves results in the format of a serachResult object. It // Recieves results in the format of a serachResult object. It
// has the members id, object, and score. It has a function // has the members id, object, and score. It has a function
// next() which returns the next highest score search result // next() which returns the next highest score search result
@ -98,7 +106,7 @@ define(
// Get result list promises // Get result list promises
for (var i = 0; i < providers.length; i += 1) { for (var i = 0; i < providers.length; i += 1) {
resultsPromises.push(providers[i].query(inputID)); resultsPromises.push(providers[i].query(inputID, validType));
} }
// Wait for the promises to fufill // Wait for the promises to fufill

View File

@ -98,7 +98,7 @@ define(
// Processes results from the format that elasticsearch returns to // Processes results from the format that elasticsearch returns to
// a list of objects in the format that mct-representation can use // a list of objects in the format that mct-representation can use
function processResults(rawResults) { function processResults(rawResults, validType) {
var results = rawResults.data.hits.hits, var results = rawResults.data.hits.hits,
resultsLength = results.length, resultsLength = results.length,
ids = [], ids = [],
@ -131,9 +131,10 @@ define(
for (j = 0; j < resultsLength; j += 1) { for (j = 0; j < resultsLength; j += 1) {
id = ids[j]; id = ids[j];
// Include any item except folders // Include items we can get models for
if (objects[id].getModel) { if (objects[id].getModel) {
if (objects[id].getModel().type !== "folder") { // Check to see if they are allowed to be included
if (validType(objects[id].getModel())) {
// Format the results as searchResult objects // Format the results as searchResult objects
searchResults.push({ searchResults.push({
id: id, id: id,
@ -155,7 +156,6 @@ define(
* 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.
* * Folders are not included in the results.
* * Wildcards are supported. * * Wildcards are supported.
* * Fuzziness is used to produce more results that are still * * Fuzziness is used to produce more results that are still
* relevant. (All results within a certain edit distance.) * relevant. (All results within a certain edit distance.)
@ -164,10 +164,13 @@ define(
* *
* @param inputID the name of the ID property of the html text * @param inputID the name of the ID property of the html text
* input where this funcion should find the search term * input where this funcion should find the search term
* @param validType a function which takes a model for an object
* and determines if it is of a valid type to include in the
* final list of results
* @param maxResults (optional) the maximum number of results * @param maxResults (optional) the maximum number of results
* that this function should return * that this function should return
*/ */
function queryElasticsearch(inputID, maxResults) { function queryElasticsearch(inputID, validType, maxResults) {
var searchTerm; var searchTerm;
// Check to see if the user provided a maximum // Check to see if the user provided a maximum
@ -190,7 +193,7 @@ define(
"&size=" + maxResults "&size=" + maxResults
}).then(function (rawResults) { }).then(function (rawResults) {
// ...then process the data // ...then process the data
return processResults(rawResults); return processResults(rawResults, validType);
}); });
} }

View File

@ -22,7 +22,7 @@
/*global define*/ /*global define*/
/** /**
* Module defining EverythingSearchProvider. Created by shale on 07/16/2015. * Module defining GenericSearchProvider. Created by shale on 07/16/2015.
*/ */
define( define(
[], [],
@ -42,7 +42,7 @@ define(
* @param {string} SPACE the name of the persistence space from which * @param {string} SPACE the name of the persistence space from which
* models should be retrieved. * models should be retrieved.
*/ */
function EverythingSearchProvider(objectService) { function GenericSearchProvider(objectService) {
// Recursive helper function for getItems() // Recursive helper function for getItems()
function itemsHelper(children, i) { function itemsHelper(children, i) {
@ -98,7 +98,7 @@ define(
} }
// Determines if this item can be a valid result for this search term // Determines if this item can be a valid result for this search term
function validResult(item, term) { function match(item, term) {
var itemModel = item.object.getModel(), var itemModel = item.object.getModel(),
itemName = itemModel.name.toLocaleLowerCase(), itemName = itemModel.name.toLocaleLowerCase(),
itemType = itemModel.type.toLocaleLowerCase(); itemType = itemModel.type.toLocaleLowerCase();
@ -106,16 +106,8 @@ define(
return itemName.includes(term) || itemType.includes(term); return itemName.includes(term) || itemType.includes(term);
} }
// Determines if this item is a valid type for a search result
function validType(item) {
var itemModel = item.object.getModel();
// Only folders are disallowed
return itemModel.type !== "folder";
}
// Filter through a list of searchResults based on a search term // Filter through a list of searchResults based on a search term
function filterResults(results, term, resultsLength) { function filterResults(results, term, resultsLength, validType) {
var searchResults = [], var searchResults = [],
itemModel; itemModel;
@ -123,7 +115,7 @@ define(
// Prevent errors from getModel not being defined // Prevent errors from getModel not being defined
if (results[i].object.getModel) { if (results[i].object.getModel) {
// Include any items that match the term and are of valid type // Include any items that match the term and are of valid type
if (validResult(results[i], term) && validType(results[i])) { if (match(results[i], term) && validType(results[i].object.getModel())) {
// Score the result // Score the result
score(results[i], term); score(results[i], term);
// Add the result to the result list // Add the result to the result list
@ -144,15 +136,17 @@ define(
* * A domain object qualifies as a match for a search term if * * A domain object qualifies as a match for a search term if
* the object's name property contains the exact search term * the object's name property contains the exact search term
* as a substring. * as a substring.
* * Folders are not included in the results.
* * Wildcards are not supported. * * Wildcards are not supported.
* *
* @param inputID the name of the ID property of the html text * @param inputID the name of the ID property of the html text
* input where this funcion should find the search term * input where this funcion should find the search term
* @param validType a function which takes a model for an object
* and determines if it is of a valid type to include in the
* final list of results
* @param maxResults (optional) the maximum number of results * @param maxResults (optional) the maximum number of results
* that this function should return * that this function should return
*/ */
function queryManual(inputID, maxResults) { function queryManual(inputID, validType, maxResults) {
var term, var term,
searchResults = [], searchResults = [],
resultsLength; resultsLength;
@ -180,7 +174,7 @@ define(
} }
// Then filter through the items list // Then filter through the items list
searchResults = filterResults(searchResultItems, term, resultsLength); searchResults = filterResults(searchResultItems, term, resultsLength, validType);
//console.log('filtered searchResults (in Everything)', searchResults); //console.log('filtered searchResults (in Everything)', searchResults);
return searchResults; return searchResults;
@ -193,6 +187,6 @@ define(
} }
return EverythingSearchProvider; return GenericSearchProvider;
} }
); );