public PagedResult <EmployerSearchModel> Search(EmployerSearchParameters searchParams, bool orderByRelevance) { List <SearchCachedOrganisation> allOrganisations = SearchRepository.CachedOrganisations .Where(o => o.IncludeInViewingService) .ToList(); List <SearchCachedOrganisation> filteredOrganisations = FilterByOrganisations(allOrganisations, searchParams); if (searchParams.Keywords == null) { List <SearchCachedOrganisation> orderedOrganisations = filteredOrganisations.OrderBy(o => o.OrganisationName.OriginalValue).ToList(); List <SearchCachedOrganisation> paginatedResultsForAllOrganisations = PaginateResults( orderedOrganisations, searchParams.Page, searchParams.PageSize); return(new PagedResult <EmployerSearchModel> { Results = ConvertToEmployerSearchModels(paginatedResultsForAllOrganisations), CurrentPage = searchParams.Page, PageSize = searchParams.PageSize, ActualRecordTotal = orderedOrganisations.Count, VirtualRecordTotal = orderedOrganisations.Count }); } string query = searchParams.Keywords.Trim().ToLower(); bool queryContainsPunctuation = WordSplittingRegex.ContainsPunctuationCharacters(query); List <string> searchTerms = SearchHelper.ExtractSearchTermsFromQuery(query, queryContainsPunctuation); var matchingOrganisations = new List <SearchCachedOrganisation>(); var convertedResults = new List <EmployerSearchModel>(); if (searchParams.SearchType == SearchType.NotSet) { throw new NotImplementedException(); } if (searchParams.SearchType == SearchType.ByEmployerName) { matchingOrganisations = GetMatchingOrganisationsByName( filteredOrganisations, searchTerms, query, queryContainsPunctuation); List <RankedViewingSearchOrganisation> organisationsWithRankings = CalculateOrganisationRankings( matchingOrganisations, searchTerms, query, queryContainsPunctuation); List <RankedViewingSearchOrganisation> rankedOrganisations = orderByRelevance ? OrderOrganisationsByRank(organisationsWithRankings) : OrderOrganisationsAlphabetically(organisationsWithRankings); List <RankedViewingSearchOrganisation> paginatedResults = PaginateResults( rankedOrganisations, searchParams.Page, searchParams.PageSize); convertedResults = ConvertRankedOrgsToEmployerSearchModels(paginatedResults); } if (searchParams.SearchType == SearchType.BySectorType) { matchingOrganisations = GetMatchingOrganisationsBySicCode( filteredOrganisations, searchTerms, query, queryContainsPunctuation); // Only alphabetically for SIC code search List <SearchCachedOrganisation> orderedOrganisations = matchingOrganisations.OrderBy(o => o.OrganisationName.OriginalValue).ToList(); List <SearchCachedOrganisation> paginatedSicCodeResults = PaginateResults( orderedOrganisations, searchParams.Page, searchParams.PageSize); convertedResults = ConvertSearchCachedOrganisationsToEmployerSearchModels(paginatedSicCodeResults); } var pagedResult = new PagedResult <EmployerSearchModel> { Results = convertedResults, CurrentPage = searchParams.Page, PageSize = searchParams.PageSize, ActualRecordTotal = matchingOrganisations.Count, VirtualRecordTotal = matchingOrganisations.Count }; return(pagedResult); }