Exemplo n.º 1
0
        static void CreateTargetIndex()
        {
            Console.WriteLine("\n  Create target index {0} in {1} search service", TargetIndexName, TargetSearchServiceName);
            // 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(BackupDirectory + "\\" + 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://" + TargetSearchServiceName + ".search.windows.net");
            HttpClient HttpClient = new HttpClient();

            HttpClient.DefaultRequestHeaders.Add("api-key", TargetAdminKey);

            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());
            }
        }
Exemplo n.º 2
0
        static void ImportFromJSON()
        {
            Console.WriteLine("\n  Upload index documents from saved JSON files");
            // Take JSON file and import this as-is to target index
            Uri        ServiceUri = new Uri("https://" + TargetSearchServiceName + ".search.windows.net");
            HttpClient HttpClient = new HttpClient();

            HttpClient.DefaultRequestHeaders.Add("api-key", TargetAdminKey);

            try
            {
                foreach (string fileName in Directory.GetFiles(BackupDirectory, 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());
            }
        }
Exemplo n.º 3
0
        static string GetIndexSchema()
        {
            // Extract the schema for this index
            // We use REST here because we can take the response as-is

            Uri        ServiceUri = new Uri("https://" + SourceSearchServiceName + ".search.windows.net");
            HttpClient HttpClient = new HttpClient();

            HttpClient.DefaultRequestHeaders.Add("api-key", SourceAdminKey);

            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);
        }
Exemplo n.º 4
0
        static void CreateTargetIndex(string IndexName)
        {
            // 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("..\\..\\..\\..\\NYCJobsWeb\\Schema_and_Data\\" + IndexName + ".schema");

            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());
            }
        }
Exemplo n.º 5
0
        protected virtual void ImportFromJson(string json)
        {
            try
            {
                log.LogInformation($"Begin upload index json with length of {json.Length}");

                Uri ServiceUri = new Uri("https://" + settings.TargetSearchServiceName + ".search.windows.net");
                Uri uri        = new Uri(ServiceUri, "/indexes/" + settings.TargetIndexName + "/docs/index");

                HttpResponseMessage response = AzureSearchHelper.SendSearchRequest(httpClient, HttpMethod.Post, uri, json);
                response.EnsureSuccessStatusCode();
                log.LogInformation($"Successfully uploaded index json with length of {json.Length}");
            }
            catch (Exception exc)
            {
                log.LogError(exc, exc.Message);
            }
        }
Exemplo n.º 6
0
 static void ImportFromJSON(string IndexName)
 {
     // Take JSON file and import this as-is to target index
     try
     {
         foreach (string fileName in Directory.GetFiles("..\\..\\..\\..\\NYCJobsWeb\\Schema_and_Data\\", IndexName + "*.json"))
         {
             Console.WriteLine("Uploading documents from file {0}", fileName);
             string json = File.ReadAllText(fileName);
             Uri    uri  = new Uri(ServiceUri, "/indexes/" + IndexName + "/docs/index");
             HttpResponseMessage response = AzureSearchHelper.SendSearchRequest(HttpClient, HttpMethod.Post, uri, json);
             response.EnsureSuccessStatusCode();
         }
     }
     catch (Exception ex)
     {
         Console.WriteLine("Error: {0}", ex.Message.ToString());
     }
 }
Exemplo n.º 7
0
 private static void DeleteIndex(string IndexName)
 {
     // Delete the index if it exists
     try
     {
         try
         {
             Uri uri = new Uri(ServiceUri, "/indexes/" + IndexName);
             HttpResponseMessage response = AzureSearchHelper.SendSearchRequest(HttpClient, HttpMethod.Delete, uri);
             response.EnsureSuccessStatusCode();
         }
         catch (Exception ex)
         {
             Console.WriteLine("Error: {0}", ex.Message.ToString());
         }
     }
     catch (Exception ex)
     {
         Console.WriteLine("Error deleting index: {0}\r\n", ex.Message);
     }
 }
Exemplo n.º 8
0
        static Indexes GetIndexes()
        {
            // Get all the indexes from the source
            Uri        ServiceUri = new Uri("https://" + SourceSearchServiceName + ".search.windows.net");
            HttpClient HttpClient = new HttpClient();

            HttpClient.DefaultRequestHeaders.Add("api-key", SourceAPIKey);

            string indexResponse = string.Empty;

            try
            {
                Uri uri = new Uri(ServiceUri, "/indexes/");
                HttpResponseMessage response = AzureSearchHelper.SendSearchRequest(HttpClient, HttpMethod.Get, uri, null, "$select=name");
                AzureSearchHelper.EnsureSuccessfulSearchResponse(response);
                indexResponse = response.Content.ReadAsStringAsync().Result.ToString();
            }
            catch (Exception ex)
            {
                Console.WriteLine("Error: {0}", ex.Message.ToString());
            }

            return(JsonConvert.DeserializeObject <Indexes>(indexResponse));
        }
Exemplo n.º 9
0
        static void ReCreateSynonymMap()
        {
            // Get the Synonym map from the source
            Uri        sourceServiceUri = new Uri("https://" + SourceSearchServiceName + ".search.windows.net");
            HttpClient HttpClient       = new HttpClient();

            HttpClient.DefaultRequestHeaders.Add("api-key", SourceAPIKey);

            string synonymMapName = "synonym-map";
            string synonymBody    = "";

            try
            {
                // QnAMaker uses the synonymmap of synonym-map
                Uri uri = new Uri(sourceServiceUri, "/synonymmaps/" + synonymMapName + "/");
                HttpResponseMessage response = AzureSearchHelper.SendSearchRequest(HttpClient, HttpMethod.Get, uri);
                AzureSearchHelper.EnsureSuccessfulSearchResponse(response);
                synonymBody = response.Content.ReadAsStringAsync().Result.ToString();
            }
            catch (Exception ex)
            {
                Console.WriteLine("Error: {0}", ex.Message.ToString());
            }

            // Delete the source synonymmap if it exists
            Uri targetServiceUri = new Uri("https://" + TargetSearchServiceName + ".search.windows.net");

            HttpClient.DefaultRequestHeaders.Remove("api-key");
            HttpClient.DefaultRequestHeaders.Add("api-key", TargetAPIKey);

            try
            {
                // QnAMaker uses the synonymmap of synonym-map
                Uri uri = new Uri(targetServiceUri, "/synonymmaps/" + synonymMapName + "/");
                HttpResponseMessage response = AzureSearchHelper.SendSearchRequest(HttpClient, HttpMethod.Delete, uri);
                AzureSearchHelper.EnsureSuccessfulSearchResponse(response);
                string resp = response.Content.ReadAsStringAsync().Result.ToString();
            }
            catch (Exception ex)
            {
                Console.WriteLine("Error: {0}", ex.Message.ToString());
            }

            // Create the new synonym in the target if it exists
            if (!string.IsNullOrEmpty(synonymBody))
            {
                synonymBody = "{" + synonymBody.Substring(synonymBody.IndexOf("\"name\""));
                try
                {
                    // QnAMaker uses the synonymmap of synonym-map
                    Uri uri = new Uri(targetServiceUri, "/synonymmaps/" + synonymMapName + "/");
                    HttpResponseMessage response = AzureSearchHelper.SendSearchRequest(HttpClient, HttpMethod.Put, uri, synonymBody);
                    AzureSearchHelper.EnsureSuccessfulSearchResponse(response);
                    synonymBody = response.Content.ReadAsStringAsync().Result.ToString();
                }
                catch (Exception ex)
                {
                    Console.WriteLine("Error: {0}", ex.Message.ToString());
                }
            }

            Console.WriteLine("Synonym Map created in Target");
        }