Skip to content

Instantly share code, notes, and snippets.

@HarryMcCarney
Created January 8, 2015 18:34
Show Gist options
  • Save HarryMcCarney/4df8ba30699ed34a8b39 to your computer and use it in GitHub Desktop.
Save HarryMcCarney/4df8ba30699ed34a8b39 to your computer and use it in GitHub Desktop.
Azure Search
using System.Collections.Generic;
using Newtonsoft.Json;
namespace FCJobs.StorylineSuggest
{
public class ArticleIndex
{
[JsonProperty(PropertyName = "name")]
public string Name { get; set; }
[JsonProperty(PropertyName = "fields")]
public List<Field> Fields { get; set; }
public ArticleIndex()
{
Name = "articles";
Fields = new List<Field>
{
new Field() {Name = "articleId", Type = "Edm.String", Key = "true", Searchable = "false"},
new Field() {Name = "text", Type = "Edm.String", Key = "false", Searchable = "true"},
new Field() {Name = "created", Type = "Edm.String", Key = "false", Searchable = "false"},
new Field() {Name = "url", Type = "Edm.String", Key = "false", Searchable = "false"}
};
}
}
public class Field
{
[JsonProperty(PropertyName = "name")]
public string Name { get; set; }
[JsonProperty(PropertyName = "type")]
public string Type { get; set; }
[JsonProperty(PropertyName = "key")]
public string Key { get; set; }
[JsonProperty(PropertyName = "searchable")]
public string Searchable { get; set; }
}
}
using System;
using System.Configuration;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json;
namespace FCJobs.StorylineSuggest
{
internal class IndexCreator
{
public void CreateIndex()
{
var indexDoc = JsonConvert.SerializeObject(new ArticleIndex());
using (var client = new HttpClient())
{
var url =
new Uri("https://" + ConfigurationManager.AppSettings["SearchServiceName"] +
".search.windows.net/indexes/articles?api-version=2014-07-31-Preview");
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
using (var req = new HttpRequestMessage(HttpMethod.Put, url))
{
req.Content = new StringContent(indexDoc, Encoding.UTF8, "application/json");
req.Content.Headers.ContentType = new MediaTypeHeaderValue("Application/Json");
req.Headers.Add("api-key", ConfigurationManager.AppSettings["SearchServiceApiKey"]);
using (HttpResponseMessage resp = client.SendAsync(req).Result)
{
resp.EnsureSuccessStatusCode();
var res = resp.Content.ReadAsStringAsync().Result;
Console.WriteLine(res);
}
}
}
}
}
}
internal class DocumentInserter
{
public List<Article> Articles { get; set; }
public DocumentInserter(List<Article> articles)
{
Articles = articles;
}
public void Insert()
{
dynamic container = new ExpandoObject();
foreach (var a in Articles)
{
a.SearchAction = "upload";
container.value = new List<Article> {a};
UploadDocument(JsonConvert.SerializeObject(container));
Console.WriteLine("uploaded " + a.Url);
}
}
public void UploadDocument(string document)
{
using (var client = new HttpClient())
{
var url =
new Uri("https://" + ConfigurationManager.AppSettings["SearchServiceName"] +
".search.windows.net/indexes/articles/docs/index?api-version=2014-07-31-Preview");
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
using (var req = new HttpRequestMessage(HttpMethod.Post, url))
{
req.Content = new StringContent(document, Encoding.UTF8, "application/json");
req.Content.Headers.ContentType = new MediaTypeHeaderValue("Application/Json");
req.Headers.Add("api-key", ConfigurationManager.AppSettings["SearchServiceApiKey"]);
using (HttpResponseMessage resp = client.SendAsync(req).Result)
{
resp.EnsureSuccessStatusCode();
var res = resp.Content.ReadAsStringAsync().Result;
Console.WriteLine(res);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment