Ejemplo n.º 1
0
        public IActionResult SearchGet(string query, string orderBy = "relevance")
        {
            if (query == null)
            {
                return(View("../Admin/Search", new AdminSearchViewModel()));
            }

            var viewModel = new AdminSearchViewModel {
                SearchQuery = query, OrderBy = orderBy
            };

            if (string.IsNullOrWhiteSpace(query))
            {
                viewModel.Error = "Search query must not be empty";
            }
            else
            {
                bool orderByRelevance = orderBy == "relevance";
                AdminSearchResultsViewModel results = adminSearchService.Search(query, orderByRelevance);

                viewModel.SearchResults = results;
            }

            return(View("../Admin/Search", viewModel));
        }
Ejemplo n.º 2
0
        public AdminSearchResultsViewModel Search(string query, bool orderByRelevance)
        {
            query = query.Trim();

            bool queryContainsPunctuation = WordSplittingRegex.ContainsPunctuationCharacters(query);

            List <string> searchTerms = SearchHelper.ExtractSearchTermsFromQuery(query, queryContainsPunctuation);

            // Do this before we run the search, in case the cache is updated whilst the search is running
            DateTime timeDetailsLoaded = SearchRepository.CacheLastUpdated;

            var results = new AdminSearchResultsViewModel
            {
                OrganisationResults          = SearchOrganisations(query, searchTerms, queryContainsPunctuation, orderByRelevance),
                UserResults                  = SearchUsers(query, searchTerms, queryContainsPunctuation, orderByRelevance),
                SearchCacheUpdatedSecondsAgo = (int)VirtualDateTime.Now.Subtract(timeDetailsLoaded).TotalSeconds,
            };

            return(results);
        }
        public AdminSearchResultsViewModel Search(string query)
        {
            var searchTerms = ExtractSearchTermsFromQuery(query);

            List <AdminSearchServiceOrganisation> allOrganisations;
            List <AdminSearchServiceUser>         allUsers;
            DateTime timeDetailsLoaded;
            bool     usedCache;

            var loadingStart = VirtualDateTime.Now;

            if (cacheLastUpdated < VirtualDateTime.Now.AddSeconds(-70))
            {
                allOrganisations  = LoadAllOrganisations(dataRepository);
                allUsers          = LoadAllUsers(dataRepository);
                timeDetailsLoaded = VirtualDateTime.Now;
                usedCache         = false;
            }
            else
            {
                allOrganisations  = cachedOrganisations;
                allUsers          = cachedUsers;
                timeDetailsLoaded = cacheLastUpdated;
                usedCache         = true;
            }

            var loadingEnd = VirtualDateTime.Now;

            var filteringStart        = VirtualDateTime.Now;
            var matchingOrganisations = GetMatchingOrganisations(allOrganisations, searchTerms, query);
            var matchingUsers         = GetMatchingUsers(allUsers, searchTerms);
            var filteringEnd          = VirtualDateTime.Now;

            var orderingStart = VirtualDateTime.Now;
            var matchingOrganisationsOrderedByName =
                matchingOrganisations.OrderBy(o => o.OrganisationName.ToLower()).ToList();
            var matchingUsersOrderedByName =
                matchingUsers.OrderBy(u => u.FullName).ToList();
            var orderingEnd = VirtualDateTime.Now;

            var highlightingStart = VirtualDateTime.Now;
            var matchingOrganisationsWithHighlightedMatches =
                HighlightOrganisationMatches(matchingOrganisationsOrderedByName, searchTerms, query);
            var matchingUsersWithHighlightedMatches =
                HighlightUserMatches(matchingUsersOrderedByName, searchTerms);
            var highlightingEnd = VirtualDateTime.Now;

            var results = new AdminSearchResultsViewModel
            {
                OrganisationResults = matchingOrganisationsWithHighlightedMatches,
                UserResults         = matchingUsersWithHighlightedMatches,

                LoadingMilliSeconds      = loadingEnd.Subtract(loadingStart).TotalMilliseconds,
                FilteringMilliSeconds    = filteringEnd.Subtract(filteringStart).TotalMilliseconds,
                OrderingMilliSeconds     = orderingEnd.Subtract(orderingStart).TotalMilliseconds,
                HighlightingMilliSeconds = highlightingEnd.Subtract(highlightingStart).TotalMilliseconds,

                SearchCacheUpdatedSecondsAgo = (int)VirtualDateTime.Now.Subtract(timeDetailsLoaded).TotalSeconds,
                UsedCache = usedCache
            };

            return(results);
        }