Skip to content
This repository has been archived by the owner on Jul 9, 2021. It is now read-only.

Commit

Permalink
Merge pull request #114 from SkillsFundingAgency/FC-544-Add_Missing_p…
Browse files Browse the repository at this point in the history
…roperties_to_provider_search_result

Fc 544 adds provider name search endpoint
  • Loading branch information
ramaswamy-battula committed Aug 23, 2019
2 parents e4df4e0 + 055f046 commit 298558c
Show file tree
Hide file tree
Showing 19 changed files with 403 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public class ProviderSearchResultItem

public List<string> DeliveryModes { get; set; }

public double Distance { get; set; }
public double? Distance { get; set; }

public double? EmployerSatisfaction { get; set; }

Expand All @@ -31,5 +31,8 @@ public class ProviderSearchResultItem
public bool IsLevyPayerOnly { get; set; }

public bool CurrentlyNotStartingNewApprentices { get; set; }
public bool IsHigherEducationInstitute { get; set; }
public IList<string> Aliases { get; set; }
public string Uri { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
using System.Collections.Generic;

namespace SFA.DAS.Apprenticeships.Api.Types.V3
{
public sealed class ProviderSearchResults : PagedResults<ProviderSearchResultItem>
{
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<Instances count="1" />
<ConfigurationSettings>
<Setting name="AchievementRateDataBaseConnectionString" value=""/>
<Setting name="ElasticServerUrls" value="http://127.0.0.1:9200/"/>
<Setting name="ElasticServerUrls" value="https://localhost:9200/"/>
<Setting name="EnvironmentName" value="local" />
<Setting name="ga.trackingid" value="" />
<Setting name="ElasticsearchUsername" value="" />
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
using System;
using System.Collections.Generic;
using System.Net;
using System.Web.Http;
using System.Web.Http.Description;
using Microsoft.Web.Http;
using Microsoft.Web.Http;
using Sfa.Das.ApprenticeshipInfoService.Api.Attributes;
using Sfa.Das.ApprenticeshipInfoService.Core.Services;
using SFA.DAS.Apprenticeships.Api.Types.V3;
using SFA.DAS.NLog.Logger;
using Swashbuckle.Swagger.Annotations;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Net;
using System.Threading.Tasks;
using System.Web.Http;
using System.Web.Http.Description;

namespace Sfa.Das.ApprenticeshipInfoService.Api.Controllers.V3
{
Expand All @@ -17,14 +20,16 @@ namespace Sfa.Das.ApprenticeshipInfoService.Api.Controllers.V3
public class SearchV3Controller : ApiController
{
private readonly IApprenticeshipSearchServiceV3 _apprenticeshipSearchServiceV3;
private readonly IProviderNameSearchServiceV3 _providerNameSearchService;
private readonly ILog _logger;

public SearchV3Controller(
IApprenticeshipSearchServiceV3 apprenticeshipSearchServiceV2,
ILog logger)
ILog logger, IProviderNameSearchServiceV3 providerNameSearchService)
{
_apprenticeshipSearchServiceV3 = apprenticeshipSearchServiceV2;
_logger = logger;
_providerNameSearchService = providerNameSearchService;
}

/// <summary>
Expand Down Expand Up @@ -60,6 +65,44 @@ public IHttpActionResult SearchApprenticeships(string keywords, int page = 1, in
_logger.Error(e, "/apprenticeship-programmes/search");
throw;
}

}

/// <summary>
/// Search for providers
/// </summary>
/// <returns>a search result object</returns>
[SwaggerOperation("SearchProviders")]
[SwaggerResponse(HttpStatusCode.OK, "OK", typeof(ProviderSearchResults))]
[Route("providers/search")]
[HttpGet]
[ExceptionHandling]
[ApiExplorerSettings(IgnoreApi = true)]
public async Task<IHttpActionResult> SearchProviders(string keywords, int page = 1, int pageSize = 20)
{
try
{


var response = await _providerNameSearchService.SearchProviderNameAndAliases(keywords, page, pageSize);

foreach (var providerSearchResponseItem in response.Results)
{
providerSearchResponseItem.Uri = ResolveProviderUri(providerSearchResponseItem.Ukprn.ToString());
}

return Ok(response);
}
catch (ValidationException ex)
{
_logger.Error(ex, "/providers/search");
return BadRequest(ex.ToString());
}
catch (Exception e)
{
_logger.Error(e, "/providers/search");
throw;
}
}

/// <summary>
Expand Down Expand Up @@ -114,5 +157,10 @@ private List<int> ParseForLevels(string ids)

return null;
}

private string ResolveProviderUri(string id)
{
return Url.Link("DefaultApi", new { controller = "Providers", id = id });
}
}
}
24 changes: 24 additions & 0 deletions src/Sfa.Das.ApprenticeshipInfoService.Core/Helpers/QueryHelper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using System.Text.RegularExpressions;

namespace Sfa.Das.ApprenticeshipInfoService.Core.Helpers
{
public static class QueryHelper
{
public static string FormatQuery(string query, bool toLower = true)
{
return string.IsNullOrWhiteSpace(query) ? "*" : ReplaceUnacceptableCharacters(query, toLower);
}

public static string FormatQueryReturningEmptyStringIfEmptyOrNull(string query, bool toLower = true)
{
return string.IsNullOrEmpty(query) ? string.Empty : ReplaceUnacceptableCharacters(query, toLower);
}

private static string ReplaceUnacceptableCharacters(string query, bool toLower)
{
var queryformatted = Regex.Replace(query, @"[+\-&|!(){}\[\]^""~?:\\/]", @" ");

return toLower ? queryformatted.ToLowerInvariant() : queryformatted;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using System.Collections.Generic;

namespace Sfa.Das.ApprenticeshipInfoService.Core.Models
{
public class ProviderNameSearchResult
{
public long UkPrn { get; set; }
public string ProviderName { get; set; }
public List<string> Aliases { get; set; }
public bool IsEmployerProvider { get; set; }

public bool IsHigherEducationInstitute { get; set; }
public bool HasNonLevyContract { get; set; }

public bool IsLevyPayerOnly { get; set; }

public bool CurrentlyNotStartingNewApprentices { get; set; }
public bool NationalProvider { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
namespace Sfa.Das.ApprenticeshipInfoService.Core.Models.Responses
{
public enum ProviderNameSearchResponseCodes
{
Success,
SearchFailed,
NoSearchResultsFound,
SearchTermTooShort
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using System.Threading.Tasks;
using SFA.DAS.Apprenticeships.Api.Types.V3;

namespace Sfa.Das.ApprenticeshipInfoService.Core.Services
{
public interface IProviderNameSearchServiceV3
{
Task<ProviderSearchResults> SearchProviderNameAndAliases(string searchTerm, int page, int take);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,13 @@
<Compile Include="Helpers\IFundingCapCalculator.cs" />
<Compile Include="Helpers\IPaginationHelper.cs" />
<Compile Include="Helpers\PaginationHelper.cs" />
<Compile Include="Helpers\QueryHelper.cs" />
<Compile Include="Models\Address.cs" />
<Compile Include="Models\ApprenticeshipDetails.cs" />
<Compile Include="Models\ApprenticeshipProduct.cs" />
<Compile Include="Models\ApprenticeshipBasic.cs" />
<Compile Include="Models\ApprenticeshipSearchResults.cs" />
<Compile Include="Models\ProviderNameSearchResult.cs" />
<Compile Include="Models\ProviderSearchResultsItem.cs" />
<Compile Include="Models\DetailProviderResponse.cs" />
<Compile Include="Models\ContactInformation.cs" />
Expand All @@ -77,6 +79,7 @@
<Compile Include="Models\Location.cs" />
<Compile Include="Models\FrameworkProviderSearchResultsItem.cs" />
<Compile Include="Models\Coordinate.cs" />
<Compile Include="Models\Responses\ProviderNameSearchResponseCodes.cs" />
<Compile Include="Models\Responses\StandardProviderSearchResultsItemResponse.cs" />
<Compile Include="Models\StandardProviderSearchResultsItem.cs" />
<Compile Include="Models\TrainingLocation.cs" />
Expand All @@ -89,6 +92,7 @@
<Compile Include="Services\IApprenticeshipProviderRepository.cs" />
<Compile Include="Services\IApprenticeshipSearchServiceV3.cs" />
<Compile Include="Services\IGetProviderApprenticeshipLocationsV3.cs" />
<Compile Include="Services\IProviderNameSearchServiceV3.cs" />
<Compile Include="Services\IProviderSearchService.cs" />
<Compile Include="Services\IGetAssessmentOrgs.cs" />
<Compile Include="Services\IGetProviders.cs" />
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System.Collections.Generic;
using System.Diagnostics;
using System.Reflection;
using Sfa.Das.ApprenticeshipInfoService.Infrastructure.Elasticsearch.Querys;
using Sfa.Das.ApprenticeshipInfoService.Infrastructure.Models;
using Sfa.Das.ApprenticeshipInfoService.Infrastructure.Services;
using SFA.DAS.NLog.Logger;
Expand Down Expand Up @@ -42,12 +43,15 @@ public InfrastructureRegistry()
For<IElasticsearchCustomClient>().Use<ElasticsearchCustomClient>();
For<IControllerHelper>().Use<ControllerHelper>();
For<IAnalyticsService>().Use<AnalyticsService>();
For<IQueryHelper>().Use<QueryHelper>();
For<IQueryHelper>().Use<Elasticsearch.QueryHelper>();
For<IActiveApprenticeshipChecker>().Use<ActiveApprenticeshipChecker>();
For<IFundingCapCalculator>().Use<FundingCapCalculator>();
For<IPaginationHelper>().Use<PaginationHelper>();
For<IGetIfaStandardsUrlService>().Use<GetIfaStandardsUrlService>();
For<IApprenticeshipSearchResultsMapping>().Use<ApprenticeshipSearchResultsMapping>();
For<IProviderNameSearchServiceV3>().Use<ProviderNameSearchServiceV3>();
For<IProviderNameSearchProviderQuery>().Use<ProviderNameSearchProviderQuery>();
For<IProviderNameSearchMapping>().Use<ProviderNameSearchMapping>();
}

private IDictionary<string, object> GetProperties()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using Nest;
using Sfa.Das.ApprenticeshipInfoService.Core.Models;

namespace Sfa.Das.ApprenticeshipInfoService.Infrastructure.Elasticsearch.Querys
{
public interface IProviderNameSearchProviderQuery
{
ISearchResponse<ProviderNameSearchResult> GetResults(string term, int take, int skip);
long GetTotalMatches(string term);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
using System;
using Nest;
using Sfa.Das.ApprenticeshipInfoService.Core.Configuration;
using Sfa.Das.ApprenticeshipInfoService.Core.Models;

namespace Sfa.Das.ApprenticeshipInfoService.Infrastructure.Elasticsearch.Querys
{
public class ProviderNameSearchProviderQuery : IProviderNameSearchProviderQuery
{
private readonly IElasticsearchCustomClient _elasticsearchCustomClient;
private readonly IConfigurationSettings _applicationSettings;

public ProviderNameSearchProviderQuery(IElasticsearchCustomClient elasticsearchCustomClient, IConfigurationSettings applicationSettings)
{
_elasticsearchCustomClient = elasticsearchCustomClient;
_applicationSettings = applicationSettings;
}

public ISearchResponse<ProviderNameSearchResult> GetResults(string term, int take, int skip)
{
return _elasticsearchCustomClient.Search<ProviderNameSearchResult>(s => s
.Index(_applicationSettings.ProviderIndexAlias)
.Type("providerapidocument")
.Skip(skip)
.Take(take)
.Query(q => q
.Bool(b => b
.Filter(IsNotEmployerProvider())
.Must(mu => mu
.QueryString(qs => qs
.Fields(fs => fs
.Field(f => f.ProviderName)
.Field(f => f.Aliases))
.Query(term)))
)));
}

public long GetTotalMatches(string term)
{
var initialDetails = _elasticsearchCustomClient.Search<ProviderNameSearchResult>(s => s
.Index(_applicationSettings.ProviderIndexAlias)
.Type("providerapidocument")
.Query(q => q
.Bool(b => b
.Filter(IsNotEmployerProvider())
.Must(mu => mu
.QueryString(qs => qs
.Fields(fs => fs
.Field(f => f.ProviderName)
.Field(f => f.Aliases))
.Query(term)))
)));

return initialDetails.HitsMetaData.Total;
}

private static Func<QueryContainerDescriptor<ProviderNameSearchResult>, QueryContainer> IsNotEmployerProvider()
{
return f => f
.Term(t => t
.Field(fi => fi.IsEmployerProvider).Value(false));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ private static ProviderApprenticeshipLocationSearchResult MapToProviderApprentic
TotalResults = searchResponse.HitsMetaData?.Total ?? 0,
PageNumber = page,
PageSize = pageSize,
Results = searchResponse.Hits?.Select(MapHitToStandardProviderSearchResultItem),
Results = searchResponse.Hits?.Select(MapHitToProviderSearchResultItem),
TrainingOptionsAggregation = trainingOptionsAggregation,
NationalProvidersAggregation = nationalProvidersAggregation
};
Expand All @@ -197,7 +197,7 @@ private static ProviderApprenticeshipLocationSearchResult MapToProviderApprentic
TotalResults = searchResponse.HitsMetaData?.Total ?? 0,
PageNumber = page,
PageSize = pageSize,
Results = searchResponse.Hits?.Select(MapHitToStandardProviderSearchResultItem),
Results = searchResponse.Hits?.Select(MapHitToProviderSearchResultItem),
TrainingOptionsAggregation = trainingOptionsAggregation,
NationalProvidersAggregation = nationalProvidersAggregation
};
Expand All @@ -222,7 +222,7 @@ private static ProviderApprenticeshipLocationSearchResult MapToProviderApprentic
return aggregationResult;
}

private static ProviderSearchResultItem MapHitToStandardProviderSearchResultItem(IHit<IApprenticeshipProviderSearchResultsItem> hit)
private static ProviderSearchResultItem MapHitToProviderSearchResultItem(IHit<IApprenticeshipProviderSearchResultsItem> hit)
{
return new ProviderSearchResultItem
{
Expand All @@ -239,7 +239,8 @@ private static ProviderSearchResultItem MapHitToStandardProviderSearchResultItem
OverallCohort = hit.Source.OverallCohort,
HasNonLevyContract = hit.Source.HasNonLevyContract,
IsLevyPayerOnly = hit.Source.IsLevyPayerOnly,
CurrentlyNotStartingNewApprentices = hit.Source.CurrentlyNotStartingNewApprentices
CurrentlyNotStartingNewApprentices = hit.Source.CurrentlyNotStartingNewApprentices,
IsHigherEducationInstitute = hit.Source.IsHigherEducationInstitute
};
}
}
Expand Down

0 comments on commit 298558c

Please sign in to comment.