@@ -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 ;
0 commit comments