[Search] Revert to earlier commit

Reverted to before implementing next(). May re add
back some changes, such as removing validType and
then adding web workers.
This commit is contained in:
shale 2015-07-20 17:02:08 -07:00
parent 1891b24bde
commit f4bd7d7a44
5 changed files with 37 additions and 320 deletions

View File

@ -42,7 +42,7 @@
"provides": "searchService",
"type": "provider",
"implementation": "providers/GenericSearchProvider.js",
"depends": [ "$rootScope", "objectService", "workerService" ]
"depends": [ "objectService" ]
},
{
"provides": "searchService",
@ -55,13 +55,6 @@
"type": "aggregator",
"implementation": "SearchAggregator.js"
}
],
"workers": [
{
"key": "genericSearchWorker",
"scriptUrl": "workers/GenericSearchWorker.js",
"depends": [ "objectService" ]
}
]
}
}

View File

@ -41,8 +41,6 @@ define(
* aggregated
*/
function SearchAggregator(providers) {
var compiledResults = [];
// Determines if a searchResult object is a valid type
// to be displayed as a final result. Is passed to the
@ -54,64 +52,21 @@ define(
// Remove extra objects that have the same ID
function filterRepeats(results) {
var ids = [],
idToIndicies = {}, // 'dictionary' mapping IDs to a list of indicies
filteredResults = [];
var ids = [];
// 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);
}
/*
for (var i = 0; i < results.length; i += 1) {
if (results[i] === undefined) {
// Catch any leftover undefined objects
results.splice(i, 1);
i--;
} else if (ids.indexOf(results[i].id) !== -1) {
if (ids.indexOf(results[i].id) !== -1) {
// If this result's ID is already there, remove the object
results.splice(i, 1);
// Reduce loop index because we shortened the array
i--;
i -= 1;
} else {
// Otherwise add the ID to the list of the ones we have seen
ids.push(results[i].id);
}
}
*/
return filteredResults;
return results;
}
// Order the objects from highest to lowest score in the array
@ -136,8 +91,6 @@ 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;
@ -149,60 +102,6 @@ define(
}
}
function getPromisedItems(promises, index, fufilledPromises) {
if (index >= promises.length) {
return fufilledPromises;
} else {
return promises[index].then(function (results) {
fufilledPromises = fufilledPromises.concat(results);
return getPromisedItems(promises, index + 1, fufilledPromises);
});
}
}
// Add more results to compiledResults
// (As if the user presed 'load more results')
function loadMore() {
}
// Add x number of items to the compiledResults as the initial number of results that
// we display
function initialLoad(firstPromises) {
return getPromisedItems(firstPromises, 0, []).then(function (current) {
// Push the firsts onto the compiledResults
for (var i = 0; i < current.length; i++) {
if (current[i]) {
compiledResults.push(current[i]);
}
}
// Look for more results n times and add them to compiledResults
var outOfResults = [];
for (var i = 0; i < DEFAULT_MAX_RESULTS; i++) {
// If all of the providers are returning undefined, there are
// no more results to load
if (current.every(function (c) {
return c === undefined;
})) {
break;
}
// For each provider, load the next result and add it to compiledResults
for (var j = 0; j < current.length; j++) {
if (current[j]) {
var nextResult = current[j].next();
if (nextResult) {
compiledResults.push(nextResult);
}
current[j] = nextResult;
}
}
}
return compiledResults;
});
}
// Recieves results in the format of a serachResult object. It
// has the members id, object, and score. It has a function
// next() which returns the next highest score search result
@ -212,15 +111,11 @@ define(
// merges the results lists so that there are not redundant
// results
function mergeResults(inputID) {
//var resultsPromises = [];
var resultsPromises = [];
// The first result from each provider. Each should have a next() function.
var firstPromises = [];
// Get the initial result promises
// Get result list promises
for (var i = 0; i < providers.length; i += 1) {
//resultsPromises.push(
firstPromises.push(
resultsPromises.push(
providers[i].query(
inputID, validType, DEFAULT_MAX_RESULTS, DEFUALT_TIMEOUT
)
@ -228,20 +123,12 @@ define(
}
// Wait for the promises to fufill
return initialLoad(firstPromises).then(function (c) {
// Get rid of the repeated objects and put in correct order
c = filterRepeats(c);
c = orderByScore(c);
return c;
});
/*
return getPromisedResults(resultsPromises, 0, []).then(function (c) {
// Get rid of the repeated objects and put in correct order
c = filterRepeats(c);
c = orderByScore(c);
return c;
});
*/
}
return {

View File

@ -48,8 +48,6 @@ define(
*/
function ElasticsearchSearchProvider($http, objectService, ROOT) {
// TODO: Fix the above docstring
var latestSearchResults = [],
currentResultIndex = 0;
// Check to see if the input has any special options
function isDefaultFormat(searchTerm) {
@ -69,6 +67,7 @@ define(
return searchTerm.split(' ').map(function (s) {
if (s.includes('"')) {
console.log('true');
return s;
} else {
return s + '~' + editDistance;
@ -98,28 +97,6 @@ define(
return searchTerm;
}
// Get the next search result
function next() {
// Because elasticsearch only returns matching things, we just
// need to step through the array
currentResultIndex++;
if (currentResultIndex > latestSearchResults.length) {
// If we go past the end of the array, we return undefined
return undefined;
} else {
return latestSearchResults[currentResultIndex];
}
}
function first() {
// Since next() immeditely does 'i++', start before the start of the array
currentResultIndex = -1;
var n = next();
return n;
}
// Processes results from the format that elasticsearch returns to
// a list of objects in the format that mct-representation can use
function processResults(rawResults, validType) {
@ -127,7 +104,8 @@ define(
resultsLength = results.length,
ids = [],
scores = {},
searchResults = [];
searchResults = [],
i;
if (rawResults.data.hits.total > resultsLength) {
// TODO: Somehow communicate this to the user
@ -135,23 +113,24 @@ define(
}
// Get the result objects' IDs
for (var i = 0; i < resultsLength; i += 1) {
for (i = 0; i < resultsLength; i += 1) {
ids.push(results[i][ID]);
}
// Get the result objects' scores
for (var i = 0; i < resultsLength; i += 1) {
for (i = 0; i < resultsLength; i += 1) {
//scores.push(results[i][SCORE]);
scores[ ids[i] ] = results[i][SCORE];
}
//console.log('scores {}', scores);
// Get the domain objects from their IDs
return objectService.getObjects(ids).then(function (objects) {
var id,
j;
// Filter by search term
for (var j = 0; j < resultsLength; j += 1) {
var id = ids[j];
for (j = 0; j < resultsLength; j += 1) {
id = ids[j];
// Include items we can get models for
if (objects[id].getModel) {
@ -161,21 +140,13 @@ define(
searchResults.push({
id: id,
object: objects[id],
score: scores[id],
next: next
score: scores[id]
});
}
}
}
/*
for (var k = 0; k < searchResults.length; k++) {
console.log('ES score', searchResults[k].score, 'for', searchResults[k].object.getModel().name);
}
*/
//console.log('setting latest search results with', searchResults);
latestSearchResults = searchResults;
//console.log('searchResults (in ES provider)', searchResults);
return searchResults;
});
}
@ -220,7 +191,8 @@ define(
searchTerm = processSearchTerm(searchTerm);
// Create the query to elasticsearch
esQuery = ROOT + "/_search/?q=" + searchTerm + "&size=" + maxResults;
esQuery = ROOT + "/_search/?q=" + searchTerm +
"&size=" + maxResults;
if (timeout) {
esQuery += "&timeout=" + timeout;
}
@ -231,11 +203,7 @@ define(
url: esQuery
}).then(function (rawResults) {
// ...then process the data
processResults(rawResults, validType);
// and return the first result
var f = first();
// console.log('ES return', f);
return f;
return processResults(rawResults, validType);
});
}

View File

@ -44,12 +44,6 @@ define(
* more easy creation of web workers.
*/
function GenericSearchProvider($rootScope, objectService, workerService) {
var latestItems = [],
currentResultIndex = 0,
currentSeachInput = '',
curentSearchTerms = [],
validType = function () {return true;};
/*
var worker = workerService.run('genericSearchWorker'),
lastestItems;
@ -83,19 +77,21 @@ define(
});
//counter += 1;
}
*/
function handleResponse(event) {
latest = event.data;
$rootScope.$apply();
//requestNext();
}
*/
// Recursive helper function for getItems()
function itemsHelper(children, i) {
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!
@ -135,8 +131,7 @@ define(
searchResultItems.push({
id: items[i].getId(),
object: items[i],
score: 0, // Assign actual score when filtering for term
next: next
score: 0 // Assign actual score when filtering for term
});
}
@ -146,6 +141,8 @@ define(
});
}
// Process the search input. Makes an array of search terms
// by splitting up the input at spaces.
function process(input) {
@ -182,9 +179,8 @@ define(
return score * weight;
}
/*
// Filter through a list of searchResults based on a search term
function filterResults(results, originalInput, resultsLength) {
function filterResults(results, originalInput, resultsLength, validType) {
var terms,
searchResults = [],
itemModel;
@ -206,51 +202,6 @@ define(
return searchResults;
}
*/
// Get the next item from latestItems
function next() {
var i = currentResultIndex,
gotNext = false,
nextResult;
// Look for the next item that qualifies as a search result
while (!gotNext) {
i++;
if (i > latestItems.length) {
// If we go past the end of the array, we return undefined
gotNext = true;
nextResult = undefined;
//currentResultIndex = i;
} else if (latestItems[i]) {
// Prevent errors from getModel not being defined
if (latestItems[i].object.getModel) {
latestItems[i].score = score(latestItems[i], curentSearchTerms, currentSeachInput);
//console.log('item', latestItems[i].object.getModel().name, 'score', latestItems[i].score);
// Include any items that match the terms and are of valid type
if (latestItems[i].score > 0 && validType(latestItems[i].object.getModel())) {
// Add the result to the result list
nextResult = latestItems[i];
//nextResult.next = next;
currentResultIndex = i;
gotNext = true;
}
}
}
}
return nextResult;
}
// Get the first result in latestItems that is a search result
function first(input) {
// Set up the global variables
currentSeachInput = input;
curentSearchTerms = process(input);
// Since next() immeditely does 'i++', start before the start of the array
currentResultIndex = -1;
return next();
}
/**
* Searches through the filetree for domain objects which match
@ -265,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 passedValidType (optional) 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; default returns true
* @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
* that this function should return
* @param timeout (optional) the time after which the search should
* stop calculations and return partial results
*/
function queryGeneric(inputID, passedValidType, maxResults, timeout) {
function queryGeneric(inputID, validType, maxResults, timeout) {
var input,
terms = [],
searchResults = [],
@ -286,8 +237,6 @@ define(
maxResults = DEFAULT_MAX_RESULTS;
}
// Set the global validType function with the passed one
validType = passedValidType;
// Get the user input
input = document.getElementById(inputID).value;
@ -295,9 +244,6 @@ define(
// Get items list
//requestItems(); // Test out the worker
return getItems(timeout).then(function (searchResultItems) {
// Set global items variable
latestItems = searchResultItems;
// Keep track of the number of results to display
if (searchResultItems.length < maxResults) {
resultsLength = searchResultItems.length;
@ -306,13 +252,10 @@ define(
}
// Then filter through the items list
//searchResults = filterResults(searchResultItems, input, resultsLength);
searchResults = filterResults(searchResultItems, input, resultsLength, validType);
// Get the first search result
var firstResult = first(input);
//console.log('generic return', firstResult);
return firstResult;
//console.log('filtered searchResults (in Everything)', searchResults);
return searchResults;
});
}

View File

@ -1,74 +0,0 @@
/*****************************************************************************
* Open MCT Web, Copyright (c) 2014-2015, United States Government
* as represented by the Administrator of the National Aeronautics and Space
* Administration. All rights reserved.
*
* Open MCT Web is licensed under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0.
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*
* Open MCT Web includes source code licensed under additional open source
* licenses. See the Open Source Licenses file (LICENSES.md) included with
* this source code distribution or the Licensing information page available
* at runtime from the About dialog for additional information.
*****************************************************************************/
/*global self*/
(function () {
"use strict";
// Recursive helper function for getItems()
function itemsHelper(children, i) {
if (i >= children.length) {
// Done!
return children;
} else if (children[i].hasCapability('composition')) {
// This child has children
return children[i].getCapability('composition').invoke().then(function (grandchildren) {
// Add grandchildren to the end of the list
// They will also be checked for composition
return itemsHelper(children.concat(grandchildren), i + 1);
});
} else {
// This child is a leaf
return itemsHelper(children, i + 1);
}
}
// Converts the filetree into a list
function getItems(objectService) {
// Aquire My Items (root folder)
return objectService.getObjects(['mine']).then(function (objects) {
// Get all of its descendents
return itemsHelper([objects.mine], 0).then(function (items) {
// Turn them into searchResult objects (object, id, and score)
var searchResultItems = [];
for (var i = 0; i < items.length; i += 1) {
searchResultItems.push({
id: items[i].getId(),
object: items[i],
score: 0 // Assign actual score when filtering for term
});
}
//console.log('searchResultItems (in Everything)', searchResultItems);
return searchResultItems;
});
});
}
self.onmessage = function (event) {
//console.log('in worker .. 1');
//console.log('event.data', event.data);
//console.log('objects 0', objects[0]);
self.postMessage(itemsHelper(event.data, 0));
};
}());