public Task <FaocSearchResult> OnlineCourseSearch(ILogger log, OnlineCourseSearchCriteria criteria) { return(_courseSearchServiceWrapper.SearchOnlineCourses(criteria)); }
public async Task <FaocSearchResult> SearchOnlineCourses(OnlineCourseSearchCriteria criteria) { Throw.IfNull(criteria, nameof(criteria)); _log.LogInformation("FAOC search criteria.", criteria); _log.LogInformation("FAOC search uri.", _uri.ToString()); var sortBy = criteria.SortBy ?? CourseSearchSortBy.Relevance; var filterClauses = new List <string>(); if (criteria.StartDateFrom.HasValue) { filterClauses.Add($"StartDate ge {criteria.StartDateFrom.Value:o}"); } if (criteria.StartDateTo.HasValue) { filterClauses.Add($"StartDate le {criteria.StartDateTo.Value:o}"); } if (criteria.QualificationLevels?.Any() ?? false) { filterClauses.Add($"search.in(NotionalNVQLevelv2, '{string.Join("|", criteria.QualificationLevels.Select(EscapeFilterValue))}', '|')"); } if (!string.IsNullOrWhiteSpace(criteria.ProviderName)) { var providerNameEscaped = EscapeFilterValue(criteria.ProviderName); filterClauses.Add($"search.ismatchscoring('{providerNameEscaped}', 'ProviderName', 'simple', 'any')"); } var filter = string.Join(" and ", filterClauses); var orderBy = sortBy == CourseSearchSortBy.StartDateDescending ? "StartDate desc" : sortBy == CourseSearchSortBy.StartDateAscending ? "StartDate asc" : "search.score() desc"; var(limit, start) = ResolvePagingParams(criteria.Limit, criteria.Start); var searchText = TranslateCourseSearchSubjectText(criteria.SubjectKeyword); var results = await _queryIndex.Documents.SearchAsync <AzureSearchOnlineCourse>( searchText, new SearchParameters() { Facets = new[] { "NotionalNVQLevelv2,count:100", "ProviderName,count:100", }, Filter = filter, IncludeTotalResultCount = true, SearchFields = new[] { "QualificationCourseTitle", "CourseName", }, SearchMode = SearchMode.All, Top = limit, Skip = start, OrderBy = new[] { orderBy } }); return(new FaocSearchResult() { Limit = limit, Start = start, Total = (int)results.Count.Value, Facets = results.Facets, Items = results.Results.Select(r => new FaocSearchResultItem() { Course = r.Document, Score = r.Score }) }); string EscapeFilterValue(string v) => v.Replace("'", "''"); }
public async Task <ActionResult> OnlineCourseSearch([FromBody] OnlineCourseSearchRequest request) { var criteria = new OnlineCourseSearchCriteria() { Limit = request.Limit, ProviderName = request.ProviderName, QualificationLevels = request.QualificationLevels, SortBy = request.SortBy, Start = request.Start, StartDateFrom = request.StartDateFrom, StartDateTo = request.StartDateTo, SubjectKeyword = request.SubjectKeyword, }; var result = await _service.OnlineCourseSearch(_log, criteria); var response = new OnlineCourseSearchResponse() { Limit = result.Limit, Start = result.Start, Total = result.Total, Facets = result.Facets.ToDictionary( f => _courseSearchFacetMapping.GetValueOrDefault(f.Key, f.Key), f => f.Value.Select(v => new FacetCountResult() { Value = v.Value, Count = v.Count.GetValueOrDefault() })), Results = result.Items.Select(i => new OnlineCourseSearchResponseItem() { Id = i.Course.Id, QualificationCourseTitle = i.Course.QualificationCourseTitle, LearnAimRef = i.Course.LearnAimRef, NotionalNVQLevelv2 = i.Course.NotionalNVQLevelv2, AwardOrgCode = i.Course.AwardOrgCode, QualificationType = i.Course.QualificationType, CourseDescription = i.Course.CourseDescription, EntryRequirements = i.Course.EntryRequirements, WhatYoullLearn = i.Course.WhatYoullLearn, HowYoullLearn = i.Course.HowYoullLearn, WhatYoullNeed = i.Course.WhatYoullNeed, HowYoullBeAssessed = i.Course.HowYoullBeAssessed, WhereNext = i.Course.WhereNext, AdultEducationBudget = i.Course.AdultEducationBudget, AdvancedLearnerLoan = i.Course.AdvancedLearnerLoan, CourseName = i.Course.CourseName, CourseId = i.Course.CourseId, CourseRunId = i.Course.CourseRunId, StartDate = !i.Course.FlexibleStartDate ? i.Course.StartDate : null, FlexibleStartDate = i.Course.FlexibleStartDate, CourseWebsite = i.Course.CourseWebsite, ProviderUKPRN = i.Course.ProviderUKPRN, ProviderName = i.Course.ProviderName, ProviderAddressLine1 = i.Course.ProviderAddressLine1, ProviderAddressLine2 = i.Course.ProviderAddressLine2, ProviderTown = i.Course.ProviderTown, ProviderPostcode = i.Course.ProviderPostcode, ProviderCounty = i.Course.ProviderCounty, ProviderEmail = i.Course.ProviderEmail, ProviderTelephone = i.Course.ProviderTelephone, ProviderFax = i.Course.ProviderFax, ProviderWebsite = i.Course.ProviderWebsite, ProviderLearnerSatisfaction = i.Course.ProviderLearnerSatisfaction, ProviderEmployerSatisfaction = i.Course.ProviderEmployerSatisfaction, }).ToList() }; return(new OkObjectResult(response)); }