hhyyg
6/28/2016 - 6:57 AM

Azure Search API Sample

Azure Search API Sample

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);
                }
            }
        }
    }
}
<?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>