diff --git a/LearningHub.Nhs.WebUI/Models/Search/SearchResultViewModel.cs b/LearningHub.Nhs.WebUI/Models/Search/SearchResultViewModel.cs index bf8837d01..c4b0e6a6c 100644 --- a/LearningHub.Nhs.WebUI/Models/Search/SearchResultViewModel.cs +++ b/LearningHub.Nhs.WebUI/Models/Search/SearchResultViewModel.cs @@ -78,5 +78,10 @@ public class SearchResultViewModel /// Gets or sets the catalogue result paging. /// public PagingViewModel CatalogueResultPaging { get; set; } + + /// + /// Gets or sets a value indicating whether Did You Mean Enabled or not. + /// + public bool DidYouMeanEnabled { get; set; } } } diff --git a/LearningHub.Nhs.WebUI/Services/SearchService.cs b/LearningHub.Nhs.WebUI/Services/SearchService.cs index 8e22e0eeb..54f8e7edf 100644 --- a/LearningHub.Nhs.WebUI/Services/SearchService.cs +++ b/LearningHub.Nhs.WebUI/Services/SearchService.cs @@ -6,6 +6,7 @@ namespace LearningHub.Nhs.WebUI.Services using System.Net.Http; using System.Security.Principal; using System.Text; + using System.Text.RegularExpressions; using System.Threading.Tasks; using System.Web; using HtmlAgilityPack; @@ -61,6 +62,7 @@ public async Task PerformSearch(IPrincipal user, SearchRe var searchSortItemList = SearchHelper.GetSearchSortList(); var selectedSortItem = searchSortItemList.Where(x => x.SearchSortType == (SearchSortTypeEnum)searchSortType).FirstOrDefault(); var groupId = Guid.Parse(searchRequest.GroupId); + bool didYouMeanEnabled = false; var resourceSearchPageSize = this.settings.FindwiseSettings.ResourceSearchPageSize; var catalogueSearchPageSize = this.settings.FindwiseSettings.CatalogueSearchPageSize; @@ -112,6 +114,35 @@ public async Task PerformSearch(IPrincipal user, SearchRe resourceResult = resourceResultTask.Result; catalogueResult = catalogueResultTask.Result; + + // Did you mean suggestion when no hits found + if (resourceResult?.TotalHits == 0 && catalogueResult?.TotalHits == 0 && (resourceResult?.Spell?.Suggestions?.Count > 0 || catalogueResult?.Spell?.Suggestions?.Count > 0)) + { + didYouMeanEnabled = true; + + // pass the spell suggestion as new search text - resources + if (resourceResult?.Spell?.Suggestions?.Count > 0) + { + resourceSearchRequestModel.SearchText = Regex.Replace(resourceResult?.Spell?.Suggestions?.FirstOrDefault().ToString(), "<.*?>", string.Empty); + + // calling findwise endpoint with new search text - resources + resourceResultTask = this.GetSearchResultAsync(resourceSearchRequestModel); + } + + // pass the spell suggestion as new search text - catalogues + if (catalogueResult?.Spell?.Suggestions?.Count > 0) + { + catalogueSearchRequestModel.SearchText = Regex.Replace(catalogueResult?.Spell?.Suggestions?.FirstOrDefault().ToString(), "<.*?>", string.Empty); + + // calling findwise endpoint with new search text - catalogues + catalogueResultTask = this.GetCatalogueSearchResultAsync(catalogueSearchRequestModel); + } + + await Task.WhenAll(resourceResultTask, catalogueResultTask); + + resourceResult = resourceResultTask.Result; + catalogueResult = catalogueResultTask.Result; + } } var searchfilters = new List(); @@ -198,6 +229,7 @@ public async Task PerformSearch(IPrincipal user, SearchRe PageSize = catalogueSearchPageSize, TotalItems = catalogueResult?.TotalHits ?? 0, }, + DidYouMeanEnabled = didYouMeanEnabled, }; return searchResultViewModel; diff --git a/LearningHub.Nhs.WebUI/Views/Search/Index.cshtml b/LearningHub.Nhs.WebUI/Views/Search/Index.cshtml index f740f04cf..eb76017f6 100644 --- a/LearningHub.Nhs.WebUI/Views/Search/Index.cshtml +++ b/LearningHub.Nhs.WebUI/Views/Search/Index.cshtml @@ -13,6 +13,12 @@

Search results @(!string.IsNullOrEmpty(Model.SearchString) ? "for " + Model.SearchString : string.Empty)

+ @if (Model.DidYouMeanEnabled) + { +
+ We couldn't find a match for @(Model.SearchString), try adjusting your search terms or explore the suggestions below. +
+ }
diff --git a/WebAPI/LearningHub.Nhs.API/Controllers/SearchController.cs b/WebAPI/LearningHub.Nhs.API/Controllers/SearchController.cs index ab4e417d9..e9fd2990b 100644 --- a/WebAPI/LearningHub.Nhs.API/Controllers/SearchController.cs +++ b/WebAPI/LearningHub.Nhs.API/Controllers/SearchController.cs @@ -319,6 +319,7 @@ private async Task GetSearchResults(SearchRequestModel searchRe searchViewModel.Feedback = results.Feedback; searchViewModel.RelatedCatalogues = await this.catalogueService.GetCatalogues(catalogueIds); + searchViewModel.Spell = results.Spell; return searchViewModel; } @@ -390,6 +391,7 @@ private async Task GetCatalogueSearchResults(Catalogue searchViewModel.SearchId = catalogueSearchRequestModel.SearchId > 0 ? catalogueSearchRequestModel.SearchId : results.SearchId; searchViewModel.Feedback = results.Feedback; + searchViewModel.Spell = results.Spell; return searchViewModel; }