[Search] Choose higher score & optional parameters

Changed the search aggregator's filterRepeats
function to choose the version of the searchResult
object that has the higher score, when it
encounters multiple searchResult objects
corresponding to a domain object ID.
Also changed the search providers and aggregator
such that the validType parameter is now optional,
with a function that always returns true as the
defualt.
This commit is contained in:
shale 2015-07-21 09:41:08 -07:00
parent f4bd7d7a44
commit c79d1f2648
3 changed files with 72 additions and 22 deletions

View File

@ -52,6 +52,47 @@ define(
// Remove extra objects that have the same ID
function filterRepeats(results) {
var ids = [],
idToIndicies = {}, // 'dictionary' mapping IDs to a list of indicies
filteredResults = [];
// Create a list of indicies of objects that correspond to any object ID
for (var i = 0; i < results.length; i++) {
var id = results[i].id;
if (idToIndicies[id]) {
// If the ID already exists in the dictionary, push this index to
// the end of the array it points to
idToIndicies[id].push(i);
} else {
// Else make a new entry in the dictionary with this ID, pointing
// to this index
idToIndicies[id] = [i];
// And also add this ID to the list of IDs that we have seen
ids.push(id);
}
}
// Now for each ID in the dictionary, we want to use the version of
// the object that has a higher score
for (var i = 0; i < ids.length; i++) {
var id = ids[i],
indicies = idToIndicies[id],
highestScoringObject;
highestScoringObject = results[ indicies[0] ];
for (var j = 0; j < indicies.length; j++) {
// If the score of the object corresponding to this index of the results
// list has a higher score than the one we have, choose it instead
if (results[indicies[j]].score > highestScoringObject.score) {
highestScoringObject = results[indicies[j]];
}
}
filteredResults.push(highestScoringObject);
}
return filteredResults;
/*
var ids = [];
for (var i = 0; i < results.length; i += 1) {
@ -67,6 +108,7 @@ define(
}
return results;
*/
}
// Order the objects from highest to lowest score in the array
@ -91,6 +133,8 @@ define(
return results;
}
// 'Loop' over the promises using recursion so that the promises are fufilled by the
// time that we are done
function getPromisedResults(resultsPromises, promiseIndex, finalResults) {
if (promiseIndex >= resultsPromises.length) {
return finalResults;

View File

@ -48,6 +48,7 @@ define(
*/
function ElasticsearchSearchProvider($http, objectService, ROOT) {
// TODO: Fix the above docstring
var validType = function () {return true;};
// Check to see if the input has any special options
function isDefaultFormat(searchTerm) {
@ -67,7 +68,6 @@ define(
return searchTerm.split(' ').map(function (s) {
if (s.includes('"')) {
console.log('true');
return s;
} else {
return s + '~' + editDistance;
@ -104,8 +104,7 @@ define(
resultsLength = results.length,
ids = [],
scores = {},
searchResults = [],
i;
searchResults = [];
if (rawResults.data.hits.total > resultsLength) {
// TODO: Somehow communicate this to the user
@ -113,24 +112,21 @@ define(
}
// Get the result objects' IDs
for (i = 0; i < resultsLength; i += 1) {
for (var i = 0; i < resultsLength; i += 1) {
ids.push(results[i][ID]);
}
// Get the result objects' scores
for (i = 0; i < resultsLength; i += 1) {
for (var i = 0; i < resultsLength; i += 1) {
//scores.push(results[i][SCORE]);
scores[ ids[i] ] = results[i][SCORE];
}
// Get the domain objects from their IDs
return objectService.getObjects(ids).then(function (objects) {
var id,
j;
// Filter by search term
for (j = 0; j < resultsLength; j += 1) {
id = ids[j];
for (var j = 0; j < resultsLength; j += 1) {
var id = ids[j];
// Include items we can get models for
if (objects[id].getModel) {
@ -165,15 +161,15 @@ define(
*
* @param inputID the name of the ID property of the html text
* 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 passedValidType (optional) a function which takes a
* model for an object and determines if it is a valid type to
* include in the final list of results; default returns true
* @param maxResults (optional) the maximum number of results
* that this function should return
* @param timeout (optional) the time after which the search should
* stop calculations and return partial results
*/
function queryElasticsearch(inputID, validType, maxResults, timeout) {
function queryElasticsearch(inputID, passedValidType, maxResults, timeout) {
var searchTerm,
esQuery;
@ -184,6 +180,11 @@ define(
maxResults = DEFAULT_MAX_RESULTS;
}
// Check to see if a valid type function was provided
if (passedValidType) {
validType = passedValidType;
}
// Get the user input
searchTerm = document.getElementById(inputID).value;

View File

@ -44,6 +44,8 @@ define(
* more easy creation of web workers.
*/
function GenericSearchProvider($rootScope, objectService, workerService) {
var validType = function () {return true;};
/*
var worker = workerService.run('genericSearchWorker'),
lastestItems;
@ -90,8 +92,6 @@ define(
var date = new Date;
if (stopTime && date.getTime() >= stopTime) {
// This indexing of items has timed out
console.log('timed out');
console.log('returning', children);
return children;
} else if (i >= children.length) {
// Done!
@ -180,7 +180,7 @@ define(
}
// Filter through a list of searchResults based on a search term
function filterResults(results, originalInput, resultsLength, validType) {
function filterResults(results, originalInput, resultsLength) {
var terms,
searchResults = [],
itemModel;
@ -216,15 +216,15 @@ define(
*
* @param inputID the name of the ID property of the html text
* 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 passedValidType (optional) a function which takes a
* model for an object and determines if it is a valid type to
* include in the final list of results; default returns true
* @param maxResults (optional) the maximum number of results
* that this function should return
* @param timeout (optional) the time after which the search should
* stop calculations and return partial results
*/
function queryGeneric(inputID, validType, maxResults, timeout) {
function queryGeneric(inputID, passedValidType, maxResults, timeout) {
var input,
terms = [],
searchResults = [],
@ -237,6 +237,11 @@ define(
maxResults = DEFAULT_MAX_RESULTS;
}
// Check to see if a valid type function was provided
if (passedValidType) {
validType = passedValidType;
}
// Get the user input
input = document.getElementById(inputID).value;
@ -252,7 +257,7 @@ define(
}
// Then filter through the items list
searchResults = filterResults(searchResultItems, input, resultsLength, validType);
searchResults = filterResults(searchResultItems, input, resultsLength);
//console.log('filtered searchResults (in Everything)', searchResults);
return searchResults;