From c0c03714512f2eaf123a51bbc2a26049d10373f3 Mon Sep 17 00:00:00 2001 From: shale Date: Mon, 20 Jul 2015 11:21:55 -0700 Subject: [PATCH] [Search] Timeout passed from SearchAggregator The timeout value is now an optional parameter of the providers, and the search aggregator now passes a common default value to all of them. --- .../features/search/src/SearchAggregator.js | 9 +++++++- .../providers/ElasticsearchSearchProvider.js | 21 +++++++++++------ .../src/providers/GenericSearchProvider.js | 23 ++++++++++++------- 3 files changed, 37 insertions(+), 16 deletions(-) diff --git a/platform/features/search/src/SearchAggregator.js b/platform/features/search/src/SearchAggregator.js index 263d7dff29..30be3720dd 100644 --- a/platform/features/search/src/SearchAggregator.js +++ b/platform/features/search/src/SearchAggregator.js @@ -29,6 +29,9 @@ define( function () { "use strict"; + var DEFUALT_TIMEOUT = 1000, + DEFAULT_MAX_RESULTS = 100; + /** * Allows multiple services which provide search functionality * to be treated as one. @@ -112,7 +115,11 @@ define( // Get result list promises for (var i = 0; i < providers.length; i += 1) { - resultsPromises.push(providers[i].query(inputID, validType)); + resultsPromises.push( + providers[i].query( + inputID, validType, DEFAULT_MAX_RESULTS, DEFUALT_TIMEOUT + ) + ); } // Wait for the promises to fufill diff --git a/platform/features/search/src/providers/ElasticsearchSearchProvider.js b/platform/features/search/src/providers/ElasticsearchSearchProvider.js index d2a7a6bd84..4d8f62bdac 100644 --- a/platform/features/search/src/providers/ElasticsearchSearchProvider.js +++ b/platform/features/search/src/providers/ElasticsearchSearchProvider.js @@ -33,8 +33,7 @@ define( // so hide them here. var ID = "_id", SCORE = "_score", - DEFAULT_MAX_RESULTS = 100, - DEFAULT_TIMEOUT = 1000; + DEFAULT_MAX_RESULTS = 100; /** * A model service which reads domain object models from an external @@ -171,9 +170,12 @@ define( * 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 queryElasticsearch(inputID, validType, maxResults) { - var searchTerm; + function queryElasticsearch(inputID, validType, maxResults, timeout) { + var searchTerm, + esQuery; // Check to see if the user provided a maximum // number of results to display @@ -188,12 +190,17 @@ define( // Process search term searchTerm = processSearchTerm(searchTerm); + // Create the query to elasticsearch + esQuery = ROOT + "/_search/?q=" + searchTerm + + "&size=" + maxResults; + if (timeout) { + esQuery += "&timeout=" + timeout; + } + // Get the data... return $http({ method: "GET", - url: ROOT + "/_search/?q=" + searchTerm + - "&size=" + maxResults + - "&timeout=" + DEFAULT_TIMEOUT + url: esQuery }).then(function (rawResults) { // ...then process the data return processResults(rawResults, validType); diff --git a/platform/features/search/src/providers/GenericSearchProvider.js b/platform/features/search/src/providers/GenericSearchProvider.js index bd73128a31..62e6639cd2 100644 --- a/platform/features/search/src/providers/GenericSearchProvider.js +++ b/platform/features/search/src/providers/GenericSearchProvider.js @@ -43,7 +43,7 @@ define( * @param {WorkerService} workerService the service which allows * more easy creation of web workers. */ - function GenericSearchProvider(/*$rootScope, */objectService, /*workerService*/) { + function GenericSearchProvider($rootScope, objectService, workerService) { /* var worker = workerService.run('genericSearchWorker'), lastestItems; @@ -88,7 +88,7 @@ define( // Recursive helper function for getItems() function itemsHelper(children, i) { var date = new Date; - if (date.getTime() >= stopTime) { + if (stopTime && date.getTime() >= stopTime) { // This indexing of items has timed out console.log('timed out'); console.log('returning', children); @@ -110,14 +110,18 @@ define( } // Converts the filetree into a list - function getItems() { + function getItems(timeout) { // Aquire My Items (root folder) return objectService.getObjects(['mine']).then(function (objects) { // Get all of its descendents - // Set a timeout for itemsHelper - var date = new Date; - stopTime = date.getTime() + DEFAULT_TIMEOUT; + if (timeout) { + // Set a timeout for itemsHelper + var date = new Date; + stopTime = date.getTime() + timeout; + } + // If there was no timeout provided, leave undefined + // itemsHelper should just treat this as having no timeout return itemsHelper([objects.mine], 0).then(function (items) { // Turn them into searchResult objects (object, id, and score) @@ -217,8 +221,10 @@ define( * 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, validType, maxResults) { + function queryGeneric(inputID, validType, maxResults, timeout) { var input, terms = [], searchResults = [], @@ -231,12 +237,13 @@ define( maxResults = DEFAULT_MAX_RESULTS; } + // Get the user input input = document.getElementById(inputID).value; // Get items list //requestItems(); // Test out the worker - return getItems().then(function (searchResultItems) { + return getItems(timeout).then(function (searchResultItems) { // Keep track of the number of results to display if (searchResultItems.length < maxResults) { resultsLength = searchResultItems.length;