Skip to content

Commit b97dab8

Browse files
authored
Merge pull request #688 from TechnologyEnhancedLearning/releases/tucana
Merge did you mean feature as part of Tucana to Test
2 parents 1c679fd + ae5e5f2 commit b97dab8

File tree

4 files changed

+45
-0
lines changed

4 files changed

+45
-0
lines changed

LearningHub.Nhs.WebUI/Models/Search/SearchResultViewModel.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,5 +78,10 @@ public class SearchResultViewModel
7878
/// Gets or sets the catalogue result paging.
7979
/// </summary>
8080
public PagingViewModel CatalogueResultPaging { get; set; }
81+
82+
/// <summary>
83+
/// Gets or sets a value indicating whether Did You Mean Enabled or not.
84+
/// </summary>
85+
public bool DidYouMeanEnabled { get; set; }
8186
}
8287
}

LearningHub.Nhs.WebUI/Services/SearchService.cs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ namespace LearningHub.Nhs.WebUI.Services
66
using System.Net.Http;
77
using System.Security.Principal;
88
using System.Text;
9+
using System.Text.RegularExpressions;
910
using System.Threading.Tasks;
1011
using System.Web;
1112
using HtmlAgilityPack;
@@ -61,6 +62,7 @@ public async Task<SearchResultViewModel> PerformSearch(IPrincipal user, SearchRe
6162
var searchSortItemList = SearchHelper.GetSearchSortList();
6263
var selectedSortItem = searchSortItemList.Where(x => x.SearchSortType == (SearchSortTypeEnum)searchSortType).FirstOrDefault();
6364
var groupId = Guid.Parse(searchRequest.GroupId);
65+
bool didYouMeanEnabled = false;
6466

6567
var resourceSearchPageSize = this.settings.FindwiseSettings.ResourceSearchPageSize;
6668
var catalogueSearchPageSize = this.settings.FindwiseSettings.CatalogueSearchPageSize;
@@ -112,6 +114,35 @@ public async Task<SearchResultViewModel> PerformSearch(IPrincipal user, SearchRe
112114

113115
resourceResult = resourceResultTask.Result;
114116
catalogueResult = catalogueResultTask.Result;
117+
118+
// Did you mean suggestion when no hits found
119+
if (resourceResult?.TotalHits == 0 && catalogueResult?.TotalHits == 0 && (resourceResult?.Spell?.Suggestions?.Count > 0 || catalogueResult?.Spell?.Suggestions?.Count > 0))
120+
{
121+
didYouMeanEnabled = true;
122+
123+
// pass the spell suggestion as new search text - resources
124+
if (resourceResult?.Spell?.Suggestions?.Count > 0)
125+
{
126+
resourceSearchRequestModel.SearchText = Regex.Replace(resourceResult?.Spell?.Suggestions?.FirstOrDefault().ToString(), "<.*?>", string.Empty);
127+
128+
// calling findwise endpoint with new search text - resources
129+
resourceResultTask = this.GetSearchResultAsync(resourceSearchRequestModel);
130+
}
131+
132+
// pass the spell suggestion as new search text - catalogues
133+
if (catalogueResult?.Spell?.Suggestions?.Count > 0)
134+
{
135+
catalogueSearchRequestModel.SearchText = Regex.Replace(catalogueResult?.Spell?.Suggestions?.FirstOrDefault().ToString(), "<.*?>", string.Empty);
136+
137+
// calling findwise endpoint with new search text - catalogues
138+
catalogueResultTask = this.GetCatalogueSearchResultAsync(catalogueSearchRequestModel);
139+
}
140+
141+
await Task.WhenAll(resourceResultTask, catalogueResultTask);
142+
143+
resourceResult = resourceResultTask.Result;
144+
catalogueResult = catalogueResultTask.Result;
145+
}
115146
}
116147

117148
var searchfilters = new List<SearchFilterModel>();
@@ -198,6 +229,7 @@ public async Task<SearchResultViewModel> PerformSearch(IPrincipal user, SearchRe
198229
PageSize = catalogueSearchPageSize,
199230
TotalItems = catalogueResult?.TotalHits ?? 0,
200231
},
232+
DidYouMeanEnabled = didYouMeanEnabled,
201233
};
202234

203235
return searchResultViewModel;

LearningHub.Nhs.WebUI/Views/Search/Index.cshtml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,12 @@
1313
<h1 class="nhsuk-u-margin-top-5 nhsuk-u-margin-bottom-6">
1414
Search results @(!string.IsNullOrEmpty(Model.SearchString) ? "for " + Model.SearchString : string.Empty)
1515
</h1>
16+
@if (Model.DidYouMeanEnabled)
17+
{
18+
<h6 class="nhsuk-u-margin-top-5 nhsuk-u-margin-bottom-6">
19+
We couldn't find a match for <em>@(Model.SearchString)</em>, try adjusting your search terms or explore the suggestions below.
20+
</h6>
21+
}
1622

1723
<div class="nhsuk-grid-row">
1824
<div class="nhsuk-grid-column-two-thirds">

WebAPI/LearningHub.Nhs.API/Controllers/SearchController.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -333,6 +333,7 @@ private async Task<SearchViewModel> GetSearchResults(SearchRequestModel searchRe
333333

334334
searchViewModel.Feedback = results.Feedback;
335335
searchViewModel.RelatedCatalogues = await this.catalogueService.GetCatalogues(catalogueIds);
336+
searchViewModel.Spell = results.Spell;
336337

337338
return searchViewModel;
338339
}
@@ -404,6 +405,7 @@ private async Task<SearchCatalogueViewModel> GetCatalogueSearchResults(Catalogue
404405

405406
searchViewModel.SearchId = catalogueSearchRequestModel.SearchId > 0 ? catalogueSearchRequestModel.SearchId : results.SearchId;
406407
searchViewModel.Feedback = results.Feedback;
408+
searchViewModel.Spell = results.Spell;
407409

408410
return searchViewModel;
409411
}

0 commit comments

Comments
 (0)