[Search] Comments and style

This commit is contained in:
slhale 2015-08-04 10:01:54 -07:00
parent 4439e1680d
commit 45bedb20c1
4 changed files with 29 additions and 28 deletions

View File

@ -37,9 +37,9 @@ define(
* to be treated as one. * to be treated as one.
* *
* @constructor * @constructor
* @param $q Angular's $q, for promise consolidation * @param $q Angular's $q, for promise consolidation.
* @param {SearchProvider[]} providers the search providers to be * @param {SearchProvider[]} providers The search providers to be
* aggregated * aggregated.
*/ */
function SearchAggregator($q, providers) { function SearchAggregator($q, providers) {
@ -82,7 +82,7 @@ define(
return results; return results;
} }
// For documentation, see sendQuery below. // For documentation, see query below.
function queryAll(inputText, maxResults) { function queryAll(inputText, maxResults) {
var i, var i,
timestamp = Date.now(), timestamp = Date.now(),

View File

@ -55,6 +55,7 @@ define(
} }
return searchTerm.split(' ').map(function (s) { return searchTerm.split(' ').map(function (s) {
// Don't add fuzziness for quoted strings
if (s.indexOf('"') !== -1) { if (s.indexOf('"') !== -1) {
return s; return s;
} else { } else {
@ -84,13 +85,12 @@ define(
// Add fuzziness for completeness // Add fuzziness for completeness
searchTerm = addFuzziness(searchTerm); searchTerm = addFuzziness(searchTerm);
//console.log('search term ', searchTerm);
return searchTerm; return searchTerm;
} }
// 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), then // a list of searchResult objects, then returns a result object
// returns an object in the format {hits: searchResult[], total: number} // (See documentation for query for object descriptions)
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,

View File

@ -38,12 +38,12 @@ define(
* the filetree without using external search implementations. * the filetree without using external search implementations.
* *
* @constructor * @constructor
* @param $q * @param $q Angular's $q, for promise consolidation.
* @param {ObjectService} objectService the service from which * @param {ObjectService} objectService The service from which
* domain objects can be gotten. * domain objects can be gotten.
* @param {WorkerService} workerService the service which allows * @param {WorkerService} workerService The service which allows
* more easy creation of web workers. * more easy creation of web workers.
* @param {roots[]} roots an array of all the root domain objects. * @param {roots[]} roots An array of all the root domain objects.
*/ */
function GenericSearchProvider($q, objectService, workerService, roots) { function GenericSearchProvider($q, objectService, workerService, roots) {
var worker = workerService.run('genericSearchWorker'), var worker = workerService.run('genericSearchWorker'),
@ -82,6 +82,8 @@ define(
worker.postMessage(message); worker.postMessage(message);
} }
// Handles responses from the web worker. Namely, the results of
// a search request.
function handleResponse(event) { function handleResponse(event) {
var ids = [], var ids = [],
id; id;
@ -96,7 +98,7 @@ define(
var searchResults = [], var searchResults = [],
id; id;
// Reset and repopulate the latest results // Create searchResult objects
for (id in objects) { for (id in objects) {
searchResults.push({ searchResults.push({
object: objects[id], object: objects[id],
@ -106,20 +108,18 @@ define(
} }
// Resove the promise corresponding to this // Resove the promise corresponding to this
pendingQueries[event.data.timestamp].resolve( pendingQueries[event.data.timestamp].resolve({
{ hits: searchResults,
hits: searchResults, total: event.data.total,
total: event.data.total, timedOut: event.data.timedOut
timedOut: event.data.timedOut });
}
);
}); });
} }
} }
worker.onmessage = handleResponse; worker.onmessage = handleResponse;
// Recursive helper function for getItems() // Helper function for getItems(). Indexes the tree.
function itemsHelper(children, i) { function itemsHelper(children, i) {
// Index the current node // Index the current node
indexItem(children[i]); indexItem(children[i]);
@ -153,7 +153,7 @@ define(
var objects = [], var objects = [],
id; id;
// Get each of the objects in objectsById // Get each of the domain objects in objectsById
for (id in objectsById) { for (id in objectsById) {
objects.push(objectsById[id]); objects.push(objectsById[id]);
} }
@ -164,18 +164,19 @@ define(
} }
// For documentation, see query below. // For documentation, see query below
function query(input, timestamp, maxResults, timeout) { function query(input, timestamp, maxResults, timeout) {
var terms = [], var terms = [],
searchResults = [], searchResults = [],
defer = $q.defer(); defer = $q.defer();
// Allow us to access this promise later to resolve it later
pendingQueries[timestamp] = defer; pendingQueries[timestamp] = defer;
// Check to see if the user provided a maximum // Check to see if the user provided a maximum
// number of results to display // number of results to display
if (!maxResults) { if (!maxResults) {
// Else, we provide a default value. // Else, we provide a default value
maxResults = DEFAULT_MAX_RESULTS; maxResults = DEFAULT_MAX_RESULTS;
} }
// Similarly, check if timeout was provided // Similarly, check if timeout was provided
@ -183,8 +184,7 @@ define(
timeout = DEFAULT_TIMEOUT; timeout = DEFAULT_TIMEOUT;
} }
// Instead, assume that the items have already been indexed, and // Send the query to the worker
// just send the query to the worker.
workerSearch(input, maxResults, timestamp, timeout); workerSearch(input, maxResults, timestamp, timeout);
return defer.promise; return defer.promise;
@ -208,8 +208,8 @@ define(
* * A domain object qualifies as a match for a search input if * * A domain object qualifies as a match for a search input if
* the object's name property contains any of the search terms * the object's name property contains any of the search terms
* (which are generated by splitting the input at spaces). * (which are generated by splitting the input at spaces).
* * Scores are higher for matches that have more than one of * * Scores are higher for matches that have more of the terms
* the terms as substrings. * as substrings.
* *
* @param input The text input that is the query. * @param input The text input that is the query.
* @param timestamp The time at which this function was called. * @param timestamp The time at which this function was called.

View File

@ -32,6 +32,7 @@
var indexedItems = []; var indexedItems = [];
// Helper function for index() // Helper function for index()
// Checks whether an item with this ID is already indexed
function conainsItem(id) { function conainsItem(id) {
var i; var i;
for (i = 0; i < indexedItems.length; i += 1) { for (i = 0; i < indexedItems.length; i += 1) {
@ -123,7 +124,7 @@
*/ */
function search(data) { function search(data) {
// This results dictionary will have domain object ID keys which // This results dictionary will have domain object ID keys which
// point to the domain object's score. // point to the value the domain object's score.
var results = {}, var results = {},
input = data.input.toLocaleLowerCase(), input = data.input.toLocaleLowerCase(),
terms = convertToTerms(input), terms = convertToTerms(input),