public static List <object> Suggest(string searchPhrase) { var uri = new Uri(Settings.ServiceUri, "/indexes/" + Settings.IndexName + "/docs/suggest?fuzzy=true&$select=url&search=" + Uri.EscapeDataString(searchPhrase)); HttpResponseMessage response = AzureSearchHelper.SendSearchRequest(HttpMethod.Get, uri); EnsureSuccessfulSearchResponse(response); var result = DeserializeJson <dynamic>(response.Content.ReadAsStringAsync().Result); var options = new List <object>(); foreach (var option in result.value) { options.Add(new { label = (string)option["@search.text"], value = (string)option["url"] }); //quick & dirty camelCasing of "label" and "value" when returned by Web API... } return(options); }
public static string CreateIndex() { //Some changes are possible to do to an existing index, but others need a complete whipe of index first. //DeleteIndex(); var uri = new Uri(Settings.ServiceUri, "/indexes/" + Settings.IndexName); string json = AzureSearchHelper.SerializeJson(new IndexDefinition()); HttpResponseMessage response = AzureSearchHelper.SendSearchRequest(HttpMethod.Put, uri, json); string successMessage = "Index created/updated successfully"; if (!response.IsSuccessStatusCode) { successMessage = response.Content == null ? null : response.Content.ReadAsStringAsync().Result; } return(successMessage); }
/// <summary> /// Add content to index. Udates index if content already exists in index. /// </summary> /// <param name="addEntities"></param> public static void AddContentToIndex(IEnumerable <IContent> addEntities) { var documentsJson = new StringBuilder(); var indexFields = new IndexDefinition().Fields; foreach (var content in addEntities.Take(1000)) //TODO: handle more than 1000 (do multiple service requests) { documentsJson.Append(CreateUploadDocumentJson(content, indexFields) + ","); } string json = "{ value: [ " + documentsJson.ToString().TrimEnd(',') + " ]}"; var uri = new Uri(Settings.ServiceUri, "/indexes/" + Settings.IndexName + "/docs/index"); HttpResponseMessage response = AzureSearchHelper.SendSearchRequest(HttpMethod.Post, uri, json); //TODO: The response can be "partial success". Some docs have been indexed, but others have failed. Needs to be handled(?). //if (!response.IsSuccessStatusCode) //{ // //TODO: Log this or something string error = response.Content == null ? null : response.Content.ReadAsStringAsync().Result; //} }
void ContentService_Trashed(IContentService sender, MoveEventArgs <IContent> e) { var movedEntities = e.MoveInfoCollection.Select(eventInfo => eventInfo.Entity).ToList(); AzureSearchHelper.DeleteContentFromIndex(movedEntities); }
void ContentService_Deleted(IContentService sender, DeleteEventArgs <IContent> e) { AzureSearchHelper.DeleteContentFromIndex(e.DeletedEntities); }
void ContentService_UnPublished(IPublishingStrategy sender, PublishEventArgs <IContent> e) { AzureSearchHelper.DeleteContentFromIndex(e.PublishedEntities); }
void ContentService_Published(IPublishingStrategy sender, PublishEventArgs <IContent> e) { AzureSearchHelper.AddContentToIndex(e.PublishedEntities); }
public List <object> Suggest(string term) { return(AzureSearchHelper.Suggest(term)); }