Created
June 28, 2016 06:57
-
-
Save hhyyg/7f258046d528b8c51a8f20065e0a94a7 to your computer and use it in GitHub Desktop.
Azure Search API Sample
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<packages> | |
<package id="Microsoft.AspNet.WebApi.Client" version="5.2.3" targetFramework="net46" /> | |
<package id="Newtonsoft.Json" version="6.0.4" targetFramework="net46" /> | |
</packages> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Newtonsoft.Json; | |
using System; | |
using System.Collections.Generic; | |
using System.Configuration; | |
using System.IO; | |
using System.Linq; | |
using System.Net.Http; | |
using System.Net.Http.Formatting; | |
using System.Reflection; | |
using System.Text; | |
using System.Threading.Tasks; | |
namespace Miso.AzureSearchSample.Client | |
{ | |
class Program | |
{ | |
static Uri baseUri = new Uri($"https://{ConfigurationManager.AppSettings["SearchServiceName"]}.search.windows.net"); | |
static string apiVersionQuery = "api-version=2015-02-28-Preview"; | |
static void Main(string[] args) | |
{ | |
//CreateIndexer(indexerName: "productindexerlucene", targetIndexName: "productindexlucene", dataSourceName: "mydb"); | |
//CreateIndexer(indexerName: "productindexermicrosoft", targetIndexName: "productindexmicrosoft", dataSourceName: "mydb"); | |
//CreateIndex("productindexlucene.json"); | |
//CreateIndex("productindexms.json"); | |
} | |
static HttpClient CreateClient() | |
{ | |
var client = new HttpClient(); | |
client.DefaultRequestHeaders.Add("api-key", ConfigurationManager.AppSettings["SearchServiceAdminApiKey"]); | |
client.DefaultRequestHeaders.Add("Accept", "application/json"); | |
return client; | |
} | |
static void CreateIndex(string jsonFileName) | |
{ | |
var url = new Uri(baseUri, relativeUri: $"indexes?{apiVersionQuery}"); | |
var request = new HttpRequestMessage(HttpMethod.Post, url); | |
request.Content = new StringContent(ReadFileText(jsonFileName), Encoding.UTF8, "application/json"); | |
using (var client = CreateClient()) | |
{ | |
var response = client.SendAsync(request).Result; | |
Console.WriteLine(response.StatusCode); | |
if (response.StatusCode != System.Net.HttpStatusCode.NoContent) | |
{ | |
Console.WriteLine(response.Content.ReadAsStringAsync().Result); | |
} | |
} | |
} | |
static string ReadFileText(string fileName) | |
{ | |
string path = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), fileName); | |
return File.ReadAllText(path); | |
} | |
static void CreateIndexer(string indexerName, string targetIndexName, string dataSourceName) | |
{ | |
var bodyContent = new | |
{ | |
name = indexerName, | |
description = "", | |
dataSourceName = dataSourceName, | |
targetIndexName = targetIndexName, | |
fieldMappings = new object[]{ | |
new { sourceFieldName = "CategoryNameCollection", mappingFunction = new { name = "jsonArrayToStringCollection" }} | |
} | |
}; | |
var url = new Uri(baseUri, relativeUri: $"indexers?{apiVersionQuery}"); | |
var request = new HttpRequestMessage(HttpMethod.Post, url); | |
request.Content = new StringContent(JsonConvert.SerializeObject(bodyContent), Encoding.UTF8, "application/json"); | |
using (var client = CreateClient()) | |
{ | |
var response = client.SendAsync(request).Result; | |
Console.WriteLine(response.StatusCode); | |
if (response.StatusCode != System.Net.HttpStatusCode.NoContent) | |
{ | |
Console.WriteLine(response.Content.ReadAsStringAsync().Result); | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment