示例#1
0
        public async Task <HttpResponseMessage> Get(string indexName, [FromUri] ExtendedSuggestionQueryModel suggestionQuery)
        {
            if (suggestionQuery == null || string.IsNullOrWhiteSpace(suggestionQuery.Search) || suggestionQuery.Search.Length < 3)
            {
                return(Request.CreateResponse(HttpStatusCode.BadRequest, "The search value should have at least 3 characters."));
            }

            var result = await _searchClient.SuggestAsync(indexName, suggestionQuery.MapToSuggestionQuery());

            if (!result.IsSuccess)
            {
                return(Request.CreateResponse(result.StatusCode, result));
            }

            return(Request.CreateResponse(HttpStatusCode.OK, result));
        }
        public async Task <dynamic> SuggestTrustByName(string name)
        {
            var connection = ApiConnection.Create(_searchInstance, _key);
            var client     = new IndexQueryClient(connection);

            Func <SuggestionResultRecord, ExpandoObject> processResult = r =>
            {
                dynamic retVal = new ExpandoObject();
                retVal.Id   = r.Properties["MATNumber"]?.ToString();
                retVal.Text = r.Properties["TrustOrCompanyName"] as string;
                return(retVal);
            };

            var response = await client.SuggestAsync(_index, new SuggestionQuery(name)
                                                     .SuggesterName("namesuggest")
                                                     .Fuzzy(false)
                                                     .Select("MATNumber")
                                                     .Select("TrustOrCompanyName")
                                                     .SearchField("TrustOrCompanyName")
                                                     .Top(10));

            if (!response.IsSuccess)
            {
                throw new ApplicationException($"Azure Search trust suggestion error {response.Error.Code}: {response.Error.Message}");
            }

            var results = response.Body.Records;

            var matches = (from r in results
                           select processResult(r));

            dynamic ret = new ExpandoObject();

            ret.Matches = matches;
            return(ret);
        }
        public async Task <dynamic> SuggestSchoolByName(string name)
        {
            var connection = ApiConnection.Create(_searchInstance, _key);
            var client     = new IndexQueryClient(connection);

            Func <SuggestionResultRecord, ExpandoObject> processResult = r =>
            {
                dynamic retVal     = new ExpandoObject();
                var     postCode   = r.Properties["Postcode"] as string;
                var     town       = r.Properties["Town"] as string;
                var     schoolName = r.Properties["EstablishmentName"] as string;
                retVal.Id = r.Properties["URN"]?.ToString();

                if (!string.IsNullOrWhiteSpace(postCode) && !string.IsNullOrWhiteSpace(town)) // town and postcode
                {
                    retVal.Text = $"{schoolName} ({town}, {postCode})";
                }
                else if (!string.IsNullOrWhiteSpace(postCode) && string.IsNullOrWhiteSpace(town)) // just postcode
                {
                    retVal.Text = $"{schoolName} ({postCode})";
                }
                else if (string.IsNullOrWhiteSpace(postCode) && !string.IsNullOrWhiteSpace(town)) // just town
                {
                    retVal.Text = $"{schoolName} ({town})";
                }
                else if (string.IsNullOrWhiteSpace(postCode) && string.IsNullOrWhiteSpace(town)
                         ) // neither town nor post code
                {
                    retVal.Text = schoolName;
                }
                if (r.Properties["EstablishmentStatus"].ToString() == "Closed")
                {
                    retVal.Text += " (Closed)";
                }

                return(retVal);
            };

            var response = await client.SuggestAsync(_index, new SuggestionQuery(name)
                                                     .SuggesterName("nameSuggester")
                                                     .Fuzzy(false)
                                                     .Select("EstablishmentName")
                                                     .Select("URN")
                                                     .Select("Town")
                                                     .Select("Postcode")
                                                     .Select("EstablishmentStatus")
                                                     //.Filter("EstablishmentStatus eq 'Open'" +
                                                     //        " and TypeOfEstablishment ne 'Higher Education Institutions'" +
                                                     //        " and TypeOfEstablishment ne 'LA Nursery School'" +
                                                     //        " and TypeOfEstablishment ne 'Other Independent School'" +
                                                     //        " and TypeOfEstablishment ne 'Other Independent Special School'" +
                                                     //        " and TypeOfEstablishment ne 'Welsh Establishment'" +
                                                     //        " and TypeOfEstablishment ne 'Special Post 16 Institution'" +
                                                     //        " and TypeOfEstablishment ne 'Sixth Form Centres'" +
                                                     //        " and TypeOfEstablishment ne 'Service Childrens Education'" +
                                                     //        " and TypeOfEstablishment ne 'Secure Units'" +
                                                     //        " and TypeOfEstablishment ne 'Offshore Schools'" +
                                                     //        " and TypeOfEstablishment ne 'Institution funded by other Government Department'" +
                                                     //        " and TypeOfEstablishment ne 'Free Schools - 16-19'" +
                                                     //        " and TypeOfEstablishment ne 'British Schools Overseas'" +
                                                     //        " and TypeOfEstablishment ne 'Academy 16-19 Sponsor Led'" +
                                                     //        " and TypeOfEstablishment ne 'Academy 16-19 Converter'" +
                                                     //        " and StatutoryLowAge ne '16'" +
                                                     //        " and StatutoryLowAge ne '17'" +
                                                     //        " and StatutoryLowAge ne '18'" +
                                                     //        " and StatutoryLowAge ne '19'")
                                                     //.Filter("StatutoryHighAge ne '1'" +
                                                     //        " and StatutoryHighAge ne '2'" +
                                                     //        " and StatutoryHighAge ne '3'" +
                                                     //        " and StatutoryHighAge ne '4'" +
                                                     //        " and StatutoryHighAge ne '5'")//Todo: Remove .Do not filter out nurseries.
                                                     .SearchField("EstablishmentName")
                                                     .Top(10));

            if (!response.IsSuccess)
            {
                throw new ApplicationException(
                          $"Edubase school suggestion error {response.Error.Code}: {response.Error.Message}");
            }
            var results = response.Body.Records;

            var matches = (from r in results
                           select processResult(r));

            dynamic ret = new ExpandoObject();

            ret.Matches = matches;
            return(ret);
        }