public ActionResult GetResult(string keywords, bool?isstrict, string topSearchKeywords) { bool isStrictQuery = isstrict ?? false; //TODO: use Input Sanitizer instead var keywordsCollection = InputSanitiser.ExtractWordsFromInput(keywords).ToList(); //sanitise input bool isPhrase = keywordsCollection.Count > 1; var sanitisedKeywordsAsString = string.Join(" ", keywordsCollection); var didYouMean = sanitisedKeywordsAsString; //First search exact word var results = _siteSearchService.GetRawResults(sanitisedKeywordsAsString, false).ToList(); //If no reasults and q is not strict - search fuzzy but display the corrected word if (!results.Any() && !isStrictQuery && keywordsCollection.Any()) { if (isPhrase) { RankedPhrase topPhrase = PhraseSuggester.GetTopPhraseAndResults(sanitisedKeywordsAsString); if (topPhrase != null) { didYouMean = topPhrase.Content; results = topPhrase.Results.ToList(); } } else { didYouMean = _spellChecker.Check(sanitisedKeywordsAsString); results = _siteSearchService.GetRawResults(sanitisedKeywordsAsString, true).ToList(); } } //Finally if still no results return the partial with top search words suggestions if (!results.Any()) { List <string> topSearchedTerms = topSearchKeywords.Split(',').ToList(); var vm = new NoResultsFoundVm { SearchedTerm = sanitisedKeywordsAsString, Keywords = topSearchedTerms }; return(PartialView("_NoSearchResultsAndSuggestions", vm)); } else { var contentResults = _siteSearchService.ConvertSearchResultToPublishedContentWithTemplate(results, ContentCache, UmbracoContext); var mappedResults = _siteSearchService.MapToCustomResults(contentResults); var vm = new SiteSearchVm { SearchedTerm = sanitisedKeywordsAsString, SpellCheckerSuggestionWord = didYouMean, SearcResults = mappedResults, }; return(PartialView("_SiteSearchResultsAndSuggestionsPartial", vm)); } }
public void Should_Return_No_Results_Partial_With_Listed_Top_Searches_If_Nothing_Found() { _siteSearchService.Setup(y => y.GetRawResults(It.IsAny <string>(), false)).Returns(new List <SearchResult>()); //return empty list _siteSearchService.Setup(y => y.GetRawResults(It.IsAny <string>(), true)).Returns(new List <SearchResult>()); //return empty list _controller = GetController(true); var result = _controller.GetResult(Keyword, null, TopKeywords) as PartialViewResult; Assert.IsInstanceOf(typeof(NoResultsFoundVm), result.Model); Assert.AreEqual("_NoSearchResultsAndSuggestions", result.ViewName); NoResultsFoundVm vm = (NoResultsFoundVm)result.Model; Assert.AreEqual(vm.Keywords, TopKeywords.Split(',').ToList()); }