Exemple #1
0
        static async void MakeRequests()
        {
            // Note, this will create a new Azure Search Index for the text and the key phrases
            Console.WriteLine("Creating Azure Search index...");
            AzureSearch.CreateIndex(serviceClient, indexName);
            // Apply the Machine Learning Text Extraction to retrieve only the key phrases
            Console.WriteLine("Extracting key phrases from processed text... \r\n");
            KeyPhraseResult keyPhraseResult = await TextExtraction.ProcessText();

            Console.WriteLine("Found the following phrases... \r\n");
            foreach (var phrase in keyPhraseResult.KeyPhrases)
            {
                Console.WriteLine(phrase);
            }
            // Take the resulting key phrases to a new Azure Search Index
            // It is highly recommended that you upload documents in batches rather
            // individually like is done here
            Console.WriteLine("Uploading extracted text to Azure Search...\r\n");
            AzureSearch.UploadDocuments(indexClient, "1", keyPhraseResult);
            Console.WriteLine("Wait 5 seconds for content to become searchable...\r\n");
            Thread.Sleep(5000);
            // Execute a test search
            Console.WriteLine("Execute Search...");
            AzureSearch.SearchDocuments(indexClient, "Azure Search");
            Console.WriteLine("All done.  Press any key to continue.");
            Console.ReadLine();
        }
        private const string ServiceBaseUri   = "https://westus.api.cognitive.microsoft.com/"; //This you will get when you have added TextAnalytics in Azure
        public static async Task <KeyPhraseResult> ProcessText()
        {
            string filetext = "Build great search experiences for your web and mobile apps. " +
                              "Many applications use search as the primary interaction pattern for their users. When it comes to search, user expectations are high. They expect great relevance, suggestions, near-instantaneous responses, multiple languages, faceting, and more. Azure Search makes it easy to add powerful and sophisticated search capabilities to your website or application. The integrated Microsoft natural language stack, also used in Bing and Office, has been improved over 16 years of development. Quickly and easily tune search results, and construct rich, fine-tuned ranking models to tie search results to business goals. Reliable throughput and storage provide fast search indexing and querying to support time-sensitive search scenarios. " +
                              "Reduce complexity with a fully managed service. " +
                              "Azure Search removes the complexity of setting up and managing your own search index. This fully managed service helps you avoid the hassle of dealing with index corruption, service availability, scaling, and service updates. Create multiple indexes with no incremental cost per index. Easily scale up or down as the traffic and data volume of your application changes.";
            KeyPhraseResult keyPhraseResult = new KeyPhraseResult();

            using (var httpClient = new HttpClient())
            {
                httpClient.BaseAddress = new Uri(ServiceBaseUri);
                // Request headers.
                httpClient.DefaultRequestHeaders.Add("Ocp-Apim-Subscription-Key", azureMLTextAnalyticsKey);
                httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
                byte[] byteData = Encoding.UTF8.GetBytes("{\"documents\":[" +
                                                         "{\"id\":\"1\",\"text\":\"" + filetext + "\"},]}");
                // Detect key phrases:
                var keyPhrasesRequest = "text/analytics/v2.0/keyPhrases";
                // get key phrases
                using (var getcontent = new ByteArrayContent(byteData))
                {
                    getcontent.Headers.ContentType = new MediaTypeHeaderValue("application/json");
                    var response = await httpClient.PostAsync(keyPhrasesRequest, getcontent);

                    Task <string> contentTask = response.Content.ReadAsStringAsync();
                    string        content     = contentTask.Result;
                    if (!response.IsSuccessStatusCode)
                    {
                        throw new Exception("Call to get key phrases failed with HTTP status code: " +
                                            response.StatusCode + " and contents: " + content);
                    }
                    var result = JsonConvert.DeserializeObject <RootObject>(content);
                    keyPhraseResult.KeyPhrases = result.documents[0].keyPhrases;
                }
            }
            return(keyPhraseResult);
        }
Exemple #3
0
        public static void UploadDocuments(SearchIndexClient indexClient, string fileId, KeyPhraseResult keyPhraseResult)
        {
            List <IndexAction> indexOperations = new List <IndexAction>();
            var doc = new Document();

            doc.Add("fileId", fileId);
            doc.Add("keyPhrases", keyPhraseResult.KeyPhrases.ToList());
            indexOperations.Add(IndexAction.Upload(doc));
            try
            {
                indexClient.Documents.Index(new IndexBatch(indexOperations));
            }
            catch (IndexBatchException e)
            {
                // Sometimes when your Search service is under load, indexing will fail for some of the documents in
                // the batch. Depending on your application, you can take compensating actions like delaying and
                // retrying. For this simple demo, we just log the failed document keys and continue.
                Console.WriteLine(
                    "Failed to index some of the documents: {0}",
                    String.Join(", ", e.IndexingResults.Where(r => !r.Succeeded).Select(r => r.Key)));
            }
        }