Exemplo n.º 1
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.º 2
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.º 3
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.º 4
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);
     }
 }
        static void ImportFromJSON()
        {
            // 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
            {
                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());
            }
        }
Exemplo n.º 6
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.º 7
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");
        }