using System; using System.Text; using KenticoCloud.Deliver; using Microsoft.Azure.Search; using Microsoft.Azure.Search.Models; using Newtonsoft.Json; using Newtonsoft.Json.Converters; using Newtonsoft.Json.Serialization; using System.Configuration; using System.Xml.Linq; using System.Text.RegularExpressions; using System.Web; private static JsonSerializerSettings _jsonSettings; private static SearchServiceClient serviceClient; private static ISearchIndexClient indexClient; public static async void Run(TimerInfo myTimer, TraceWriter log) { try { // Get the search client serviceClient = new SearchServiceClient(ConfigurationManager.AppSettings["AzureSearchServiceName"], new SearchCredentials(ConfigurationManager.AppSettings["AzureSearchAPIKey"])); if (Convert.ToBoolean(ConfigurationManager.AppSettings["DeleteAndCreateIndex"])) { DeleteIndex(log); CreateIndex(log); } await LoadIndex(log); } catch (Exception ex) { log.Info(ex.Message); } } public static void CreateIndex(TraceWriter log) { string strReturn = ""; try { // Create the index definition var definition = new Index() { Name = ConfigurationManager.AppSettings["AzureSearchIndexName"], Fields = new[] { new Field ( "CodeName", DataType.String) { IsKey = true, IsSearchable = false, IsFilterable = false, IsSortable = false, IsFacetable = false, IsRetrievable = true}, new Field ( "Type", DataType.String) { IsKey = false, IsSearchable = false, IsFilterable = true, IsSortable = false, IsFacetable = true, IsRetrievable = true}, new Field ( "Name", DataType.String) { IsKey = false, IsSearchable = true, IsFilterable = true, IsSortable = true, IsFacetable = true, IsRetrievable = true}, new Field ( "PageAlias", DataType.String) { IsKey = false, IsSearchable = true, IsFilterable = true, IsSortable = true, IsFacetable = true, IsRetrievable = true}, new Field ( "Location", DataType.String) { IsKey = false, IsSearchable = true, IsFilterable = true, IsSortable = true, IsFacetable = true, IsRetrievable = true}, new Field ( "Date", DataType.DateTimeOffset) { IsKey = false, IsSearchable = false, IsFilterable = true, IsSortable = true, IsFacetable = true, IsRetrievable = true} }, }; // Create the index serviceClient.Indexes.Create(definition); strReturn = "Index created!"; } catch (Exception ex) { strReturn = "There was an issue creating the index: \r\n" + ex.Message.ToString(); } if (Convert.ToBoolean(ConfigurationManager.AppSettings["Debug"])) { log.Info(strReturn); } } public static async Task LoadIndex(TraceWriter log) { try { List lstActions = new List(); // Get the Kentico Cloud content DeliverClient client = new DeliverClient(ConfigurationManager.AppSettings["DeliverProjectID"]); var filters = new List { new InFilter("system.type", "speakingengagement,beer"), new Order("elements.date", OrderDirection.Descending), }; var responseContent = await client.GetItemsAsync(filters); foreach (var item in responseContent.Items) { var doc = new Document(); switch(item.System.Type) { case "speakingengagement": if (Convert.ToBoolean(ConfigurationManager.AppSettings["Debug"])) { log.Info(item.System.Id + " | " + item.GetString("name") + " | " + item.GetString("pagealias") + " | " + item.GetString("eventlocation") + " | " + item.GetDateTime("date").ToString()); } doc.Add("CodeName", item.System.Id); doc.Add("Type", item.System.Type); doc.Add("Name", item.GetString("name")); doc.Add("PageAlias", item.GetString("pagealias")); doc.Add("Location", item.GetString("eventlocation")); doc.Add("Date", item.GetDateTime("date")); break; case "beer": if (Convert.ToBoolean(ConfigurationManager.AppSettings["Debug"])) { log.Info(item.System.Id + " | " + item.GetString("name") + " | " + item.GetDateTime("date").ToString()); } doc.Add("CodeName", item.System.Id); doc.Add("Type", item.System.Type); doc.Add("Name", item.GetString("name")); doc.Add("PageAlias", item.GetString("pagealias")); doc.Add("Location", ""); doc.Add("Date", item.GetDateTime("date")); break; } lstActions.Add(IndexAction.MergeOrUpload(doc)); } // Get the list of blogs to add to the index var feedUrl = ConfigurationManager.AppSettings["BlogRSSURL"]; using (var webclient = new HttpClient()) { webclient.BaseAddress = new Uri(feedUrl); var responseMessage = await webclient.GetAsync(feedUrl); var responseString = await responseMessage.Content.ReadAsStringAsync(); //extract feed items XDocument blogs = XDocument.Parse(responseString); var blogsOut = from item in blogs.Descendants("item") select new BlogPost { PubDate = Convert.ToDateTime(item.Element("pubDate").Value), Title = item.Element("title").Value, Link = item.Element("link").Value, }; foreach(BlogPost post in blogsOut) { if (Convert.ToBoolean(ConfigurationManager.AppSettings["Debug"])) { log.Info(post.Title + " | " + post.Link + " | " + post.PubDate.ToString()); } var doc = new Document(); doc.Add("CodeName", Convert.ToBase64String(Encoding.UTF8.GetBytes(post.Title)).Replace('/', '_')); doc.Add("Type", "blog"); doc.Add("Name", post.Title); doc.Add("PageAlias", post.Link); doc.Add("Location", ""); doc.Add("Date", post.PubDate); lstActions.Add(IndexAction.MergeOrUpload(doc)); } } try { indexClient = serviceClient.Indexes.GetClient(ConfigurationManager.AppSettings["AzureSearchIndexName"]); indexClient.Documents.Index(new IndexBatch(lstActions)); } catch (IndexBatchException e) { if (Convert.ToBoolean(ConfigurationManager.AppSettings["Debug"])) { log.Info(e.Message); } } } catch (Exception ex) { log.Info(ex.Message.ToString()); } } public static void DeleteIndex(TraceWriter log) { string strReturn = ""; try { indexClient = serviceClient.Indexes.GetClient(ConfigurationManager.AppSettings["AzureSearchIndexName"]); strReturn = "Index deleted!"; } catch (Exception ex) { strReturn = "There was an issue deleting the index: \r\n" + ex.Message.ToString(); } if (Convert.ToBoolean(ConfigurationManager.AppSettings["Debug"])) { log.Info(strReturn); } } public class BlogPost { public string Title { get; set; } public string Link { get; set; } public string Description { get; set; } public DateTime PubDate { get; set; } public string DocumentTags { get; set; } }