private async Task <QueryResultsModel> ExecuteSearch(string index, string query, string searchFields, string filter, string orderBy, int skip, int take) { var connection = ApiConnection.Create(_searchInstance, _key); var searchFieldsArray = searchFields.Split(','); var orderByField = string.IsNullOrEmpty(orderBy) && searchFieldsArray.Length > 0 ? searchFieldsArray[0] : orderBy; var client = new IndexQueryClient(connection); var searchQueryModel = new SearchQuery(query); searchQueryModel.SearchFields = searchFields; if (!string.IsNullOrWhiteSpace(filter)) { searchQueryModel = searchQueryModel.Filter(filter); } //searchQueryModel.Facets = new[] { "TypeOfEstablishment", "OverallPhase", "ReligiousCharacter", "OfstedRating" }; searchQueryModel.OrderBy = orderByField; searchQueryModel = searchQueryModel.Mode(SearchMode.All).Count(true).Skip(skip).Top(take); var response = await client.SearchAsync(index, searchQueryModel); if (!response.IsSuccess) { throw new ApplicationException($"Azure Search trust search error {response.Error.Code}: {response.Error.Message}"); } return(new QueryResultsModel(response.Body.Count, response.Body.Facets, response.Body.Records.Select(x => x.Properties), take, skip)); }
// make a connection to Azure Search using RedDog.Search client public void Connect() { connection = ApiConnection.Create(ServiceName, ServiceKey); managementClient = new IndexManagementClient(connection); queryClient = new IndexQueryClient(connection); connected = true; }
public static async Task <IApiResponse <SearchQueryResult> > Search(string search, string[] facets) { var conn = ApiConnection.Create(Keys.ListingsServiceUrl, Keys.ListingsServiceKey); var queryClient = new IndexQueryClient(conn); var query = new SearchQuery(search) .Count(true) .Select("Id,Color,Options,Type,Package,Image") .OrderBy("Color"); if (facets != null) { StringBuilder facet = new StringBuilder(); foreach (string f in facets) { facet.Append(f + ","); } string facetResult = facet.ToString(); //trim trailing comma facetResult = facetResult.Substring(0, facetResult.Count() - 1); query.Facet(facetResult); } var searchResults = await queryClient.SearchAsync(Keys.ListingsServiceIndexName, query); return(searchResults); }
/// <summary> /// 検索 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private async void button4_Click(object sender, EventArgs e) { //インデクサ実行終了までにはAPIのレスポンス帰ってきてから少し時間がかかる模様 とりあえずwaitする if (!flg) { MessageBox.Show("インデックスを作成してね。作成したら少し時間をおいて(5秒くらい)アクセスしてね"); return; } var connection = ApiConnection.Create("datest01", "A53D975119F80AA1D75895645A5234BE"); // 検索を行う場合は IndexQueryClient を使う var c = new IndexQueryClient(connection); textBox2.Clear(); for (int i = 0; i < fileNames.Length; i++) { // SearchQueryの引数が検索したい文字列 var result = await c.SearchAsync("index" + i, new SearchQuery(textBox1.Text).Count(true)); if (result.StatusCode == HttpStatusCode.NotFound) { continue; } foreach (var item in result.Body.Records) { textBox2.AppendText(string.Format("filename={0}\n", item.Properties["metadata_storage_name"])); } } }
public string GetInstalledVersion(string schema) { var queryClient = new IndexQueryClient(_db); var results = queryClient.SearchAsync("automaticmigration", new SearchQuery(schema) .OrderBy("TimeOfUpdate") .SearchField("SchemaName") .Top(1) .Count(true)).Result; if (results == null || results.IsSuccess == false || results.Body == null || results.Body.Count < 1) { return(null); } return(results.Body.Records.SingleOrDefault().Properties["Version"].ToString()); }
public async Task <QueryResultsModel> ExecuteGeoSpatialSearch(string index, string lat, string lon, decimal distance, string filter, string orderBy, int skip, int take) { const string search = "*"; var latitude = double.Parse(lat); var longitude = double.Parse(lon); var filterBuilder = new StringBuilder(); var orderByField = string.IsNullOrEmpty(orderBy) ? string.Format(GeoDistanceLocationOrderFormat, latitude, longitude) : orderBy; filterBuilder.AppendFormat(GeoDistanceLocationSearchFormat, latitude, longitude, distance); if (!string.IsNullOrEmpty(filter)) { filterBuilder.AppendFormat(" and " + filter); } var connection = ApiConnection.Create(_searchInstance, _key); var client = new IndexQueryClient(connection); var response = await client.SearchAsync(index, new SearchQuery(search) .OrderBy(orderByField) .Facet("TypeOfEstablishment") .Facet("OverallPhase") .Facet("ReligiousCharacter") .Facet("OfstedRating") .Count(true) .Filter(filterBuilder.ToString()) .Skip(skip) .Top(take)); if (!response.IsSuccess) { Console.WriteLine("{0}: {1}", response.Error.Code, response.Error.Message); } var results = new QueryResultsModel(response.Body.Count, response.Body.Facets, CalcDistance(response.Body.Records.Select(x => x.Properties), lat, lon), take, skip) { QueryLat = latitude.ToString(), QueryLong = longitude.ToString() }; return(results); }
/// <summary> /// Initializes a new instance of the <see cref="SearchBase"/> class /// </summary> /// <param name="log">Log</param> /// <param name="serviceName">name of the search service</param> /// <param name="serviceAdminKey">key to access the search service</param> /// <param name="indexName">which index to talk to</param> /// <param name="documentHandleName">the key into the index</param> protected SearchBase(ILog log, string serviceName, string serviceAdminKey, string indexName, string documentHandleName) { this.Log = log; // check service credentials if (string.IsNullOrEmpty(serviceName) || string.IsNullOrEmpty(serviceAdminKey)) { this.Log.LogException("got bad search credentials"); } // create connection and clients and index this.Log.LogInformation("creating connection and index clients to Azure Search"); this.connection = ApiConnection.Create(serviceName, serviceAdminKey); this.managementClient = new IndexManagementClient(this.connection); this.queryClient = new IndexQueryClient(this.connection); this.indexName = indexName; this.documentHandleName = documentHandleName; }
private async Task <QueryResultsModel> ExecuteSearch(string index, string query, string searchFields, string filter, string orderBy, int skip, int take, IEnumerable <string> facets) { var set = _aliases.FirstOrDefault(x => x.Any(a => AliasFound(query, a))); if (set != null) { var word = set.FirstOrDefault(x => AliasFound(query, x)); query = query.Replace(word, string.Concat("(", string.Join("|", set), ") "), true); } var connection = ApiConnection.Create(_searchInstance, _key); var searchFieldsArray = searchFields.Split(','); var orderByField = string.IsNullOrEmpty(orderBy) && searchFieldsArray.Length > 0 ? searchFieldsArray[0].Replace("EstablishmentName", "EstablishmentNameUpperCase") : orderBy.Replace("EstablishmentName", "EstablishmentNameUpperCase"); var client = new IndexQueryClient(connection); var searchQueryModel = new SearchQuery(query); searchQueryModel.SearchFields = searchFields; if (!string.IsNullOrWhiteSpace(filter)) { searchQueryModel = searchQueryModel.Filter(filter); } searchQueryModel.Facets = facets; searchQueryModel.OrderBy = orderByField; searchQueryModel = searchQueryModel.Mode(SearchMode.All).Count(true).Skip(skip).Top(take); var response = await client.SearchAsync(index, searchQueryModel); if (!response.IsSuccess) { throw new ApplicationException( $"Edubase school search error {response.Error.Code}: {response.Error.Message}"); } return(new QueryResultsModel(response.Body.Count, response.Body.Facets, response.Body.Records.Select(x => x.Properties), take, skip)); }
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<List<SpotSearchResult>> GetSpotsByName(string name) { var client = new IndexQueryClient(connection); var results = await client.SearchAsync("spot", new SearchQuery(name) //.OrderBy("posttext") .SearchField("name").Top(500)); if (!results.IsSuccess) throw new Exception( String.Format("Query failed, resaon: {0}", results.StatusCode.ToString()) ); List<SpotSearchResult> spotresult = new List<SpotSearchResult>(); foreach (SearchQueryRecord record in results.Body.Records) { SpotSearchResult result = new SpotSearchResult(); result.Uid = record.Properties["id"].ToString(); result.Name = record.Properties["name"].ToString(); result.Description = record.Properties["description"].ToString(); if (record.Properties["location"] != null) { dynamic test = JsonConvert.DeserializeObject(record.Properties["location"].ToString()); result.Location = new Location(); result.Location.Latitude = Convert.ToDouble(test.coordinates.Last.ToString()); result.Location.Longitude = Convert.ToDouble(test.coordinates.First.ToString()); spotresult.Add(result); } } return spotresult; }
public void UntrackSchemas(IEnumerable <string> schemas) { var queryClient = new IndexQueryClient(_db); foreach (var schema in schemas) { var migrations = queryClient.SearchAsync("automaticmigration", new SearchQuery(schema) .OrderBy("TimeOfUpdate") .SearchField("SchemaName")).Result; if (!migrations.IsSuccess) { throw new ApplicationException("Error untracking schema: could not find migrations in schema " + schema + ", error: " + migrations.Error.Message); } if (migrations.Body != null) { var indexClient = new IndexManagementClient(_db); var result = indexClient.PopulateAsync("automaticmigration", migrations.Body.Records.Select(record => new IndexOperation(IndexOperationType.Delete, "Id", record.Properties["Id"].ToString())).ToArray()).Result; } } }
public LookupController(IndexQueryClient searchClient) { _searchClient = searchClient; }
public SuggestionsController(IndexQueryClient searchClient) { _searchClient = searchClient; }
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); }
public SearchController(IndexQueryClient searchClient) { _searchClient = searchClient; }
public async Task<List<SpotSearchResult>> GetSpotsNearby(double longitude, double latitude) { var client = new IndexQueryClient(connection); var results = await client.SearchAsync("spot", new SearchQuery() //.OrderBy("posttext") .SearchField("name") //geo.distance(location, geography'POINT(<long> <lat>)' .Filter(String.Format("geo.distance(location, geography'POINT({0} {1})') le 50", longitude.ToString(CultureInfo.InvariantCulture.NumberFormat), latitude.ToString(CultureInfo.InvariantCulture.NumberFormat) )) ); if (!results.IsSuccess) throw new Exception( String.Format("Query failed, resaon: {0}", results.StatusCode.ToString()) ); List<SpotSearchResult> spotresult = new List<SpotSearchResult>(); foreach (SearchQueryRecord record in results.Body.Records) { SpotSearchResult result = new SpotSearchResult(); result.Uid = record.Properties["id"].ToString(); result.Name = record.Properties["name"].ToString(); result.Description = record.Properties["description"].ToString(); dynamic test = JsonConvert.DeserializeObject(record.Properties["location"].ToString()); result.Location.Latitude = Convert.ToDouble(test.coordinates.Last.ToString(), CultureInfo.InvariantCulture.NumberFormat); result.Location.Longitude = Convert.ToDouble(test.coordinates.First.ToString(), CultureInfo.InvariantCulture.NumberFormat); spotresult.Add(result); } return spotresult; }