static void CreateTargetIndex() { // Use the schema file to create a copy of this index // I like using REST here since I can just take the response as-is string json = File.ReadAllText(SourceIndexName + ".schema"); // Do some cleaning of this file to change index name, etc json = "{" + json.Substring(json.IndexOf("\"name\"")); int indexOfIndexName = json.IndexOf("\"", json.IndexOf("name\"") + 5) + 1; int indexOfEndOfIndexName = json.IndexOf("\"", indexOfIndexName); json = json.Substring(0, indexOfIndexName) + TargetIndexName + json.Substring(indexOfEndOfIndexName); Uri ServiceUri = new Uri("https://" + SourceSearchServiceName + ".search.windows.net"); HttpClient HttpClient = new HttpClient(); HttpClient.DefaultRequestHeaders.Add("api-key", SourceAPIKey); try { Uri uri = new Uri(ServiceUri, "/indexes"); HttpResponseMessage response = AzureSearchHelper.SendSearchRequest(HttpClient, HttpMethod.Post, uri, json); response.EnsureSuccessStatusCode(); } catch (Exception ex) { Console.WriteLine("Error: {0}", ex.Message.ToString()); } }
static string GetIndexSchema() { // Extract the schema for this index // I like using REST here since I can just take the response as-is Uri ServiceUri = new Uri("https://" + SourceSearchServiceName + ".search.windows.net"); HttpClient HttpClient = new HttpClient(); HttpClient.DefaultRequestHeaders.Add("api-key", SourceAPIKey); string Schema = string.Empty; try { Uri uri = new Uri(ServiceUri, "/indexes/" + SourceIndexName); HttpResponseMessage response = AzureSearchHelper.SendSearchRequest(HttpClient, HttpMethod.Get, uri); AzureSearchHelper.EnsureSuccessfulSearchResponse(response); Schema = response.Content.ReadAsStringAsync().Result.ToString(); } catch (Exception ex) { Console.WriteLine("Error: {0}", ex.Message.ToString()); } return(Schema); }
static void ImportFromJSON(BlobContainerFileProvider fileProvider) { // Take JSON file and import this as-is to target index Uri ServiceUri = new Uri("https://" + SourceSearchServiceName + ".search.windows.net"); HttpClient HttpClient = new HttpClient(); HttpClient.DefaultRequestHeaders.Add("api-key", SourceAPIKey); try { // Delete any existing files foreach (BlobContainerFileInfo file in (BlobContainerDirectoryContents)fileProvider.GetDirectoryContents(null)) { if (file.Name.StartsWith(SourceIndexName)) { Console.WriteLine("Uploading documents from file {0}", file.Name); Uri uri = new Uri(ServiceUri, "/indexes/" + TargetIndexName + "/docs/index"); using (var ms = (MemoryStream)file.CreateReadStream()) { string json = ms.ToString(); HttpResponseMessage response = AzureSearchHelper.SendSearchRequest(HttpClient, HttpMethod.Post, uri, json); response.EnsureSuccessStatusCode(); } } } //foreach (string fileName in Directory.GetFiles(Directory.GetCurrentDirectory(), SourceIndexName + "*.json")) //{ // Console.WriteLine("Uploading documents from file {0}", fileName); // string json = File.ReadAllText(fileName); // Uri uri = new Uri(ServiceUri, "/indexes/" + TargetIndexName + "/docs/index"); // HttpResponseMessage response = AzureSearchHelper.SendSearchRequest(HttpClient, HttpMethod.Post, uri, json); // response.EnsureSuccessStatusCode(); //} } catch (Exception ex) { Console.WriteLine("Error: {0}", ex.Message.ToString()); } }