コード例 #1
0
        static async Task Main(string[] args)
        {
            string serviceName = "";
            string indexName   = "";
            string apiKey      = "";

            if (String.IsNullOrEmpty(apiKey))
            {
                throw new InvalidOperationException("Update with your settings");
            }

            // Create a SearchIndexClient to send create/delete index commands
            Uri serviceEndpoint           = new Uri($"https://{serviceName}.search.windows.net/");
            AzureKeyCredential credential = new AzureKeyCredential(apiKey);
            SearchIndexClient  index      = new SearchIndexClient(serviceEndpoint, credential);

            // Create a SearchClient to load and query documents
            SearchClient queryClient = new SearchClient(serviceEndpoint, indexName, credential);

            var count = await queryClient.GetDocumentCountAsync();

            Console.WriteLine($"Found {count} docs");

            SearchIndexerClient indexer = new SearchIndexerClient(serviceEndpoint, credential);

            (await indexer.GetIndexerNamesAsync()).Value
            .ToList()
            .ForEach((item) => Console.WriteLine($"Index: {item}"));

            // await index.UploadSampleDocumentAsync(indexName, apiKey);
            // await index.DeleteDocumentAsync(indexName, "todo", apiKey);
        }
コード例 #2
0
        public async Task BufferedSender()
        {
            await using SearchResources resources = SearchResources.CreateWithNoIndexes(this);
            SearchClient searchClient = null;

            try
            {
                searchClient = await CreateIndexAsync(resources);

                // Simple
                {
                    #region Snippet:Azure_Search_Documents_Tests_Samples_Sample05_IndexingDocuments_BufferedSender1
                    await using SearchIndexingBufferedSender <Product> indexer =
                                    searchClient.CreateIndexingBufferedSender <Product>();
                    await indexer.UploadDocumentsAsync(GenerateCatalog(count : 100000));

                    #endregion
                }

                await WaitForDocumentCountAsync(searchClient, 100000);

                // Check
                #region Snippet:Azure_Search_Documents_Tests_Samples_Sample05_IndexingDocuments_BufferedSender2
                //@@ await indexer.FlushAsync();
                Assert.AreEqual(100000, (int)await searchClient.GetDocumentCountAsync());
                #endregion
            }
            finally
            {
                if (searchClient != null)
                {
                    await resources.GetIndexClient().DeleteIndexAsync(searchClient.IndexName);
                }
            }
        }
コード例 #3
0
        public async Task HandleErrorsAsync()
        {
            await using SearchResources resources = await SearchResources.GetSharedHotelsIndexAsync(this);

            Environment.SetEnvironmentVariable("SEARCH_ENDPOINT", resources.Endpoint.ToString());
            Environment.SetEnvironmentVariable("SEARCH_API_KEY", resources.PrimaryApiKey);

            #region Snippet:Azure_Search_Tests_Samples_HandleErrorsAsync
            Uri endpoint = new Uri(Environment.GetEnvironmentVariable("SEARCH_ENDPOINT"));
            AzureKeyCredential credential = new AzureKeyCredential(
                Environment.GetEnvironmentVariable("SEARCH_API_KEY"));

            // Create an invalid SearchClient
            string       fakeIndexName = "doesnotexist";
            SearchClient searchClient  = new SearchClient(endpoint, fakeIndexName, credential);
            /*@@*/ searchClient = InstrumentClient(new SearchClient(endpoint, fakeIndexName, credential, GetSearchClientOptions()));
            try
            {
                await searchClient.GetDocumentCountAsync();
            }
            catch (RequestFailedException ex) when(ex.Status == 404)
            {
                Console.WriteLine("Index wasn't found.");
            }
            #endregion Snippet:Azure_Search_Tests_Samples_HandleErrorsAsync
        }
コード例 #4
0
        public async Task GetCountAsync()
        {
            await using SearchResources resources = await SearchResources.GetSharedHotelsIndexAsync(this);

            Environment.SetEnvironmentVariable("SEARCH_ENDPOINT", resources.Endpoint.ToString());
            Environment.SetEnvironmentVariable("SEARCH_API_KEY", resources.PrimaryApiKey);
            Environment.SetEnvironmentVariable("SEARCH_INDEX", resources.IndexName);

            #region Snippet:Azure_Search_Tests_Samples_GetCountAsync
            // Create a SearchClient
            Uri endpoint = new Uri(Environment.GetEnvironmentVariable("SEARCH_ENDPOINT"));
            AzureKeyCredential credential = new AzureKeyCredential(
                Environment.GetEnvironmentVariable("SEARCH_API_KEY"));
            string       indexName    = Environment.GetEnvironmentVariable("SEARCH_INDEX");
            SearchClient searchClient = new SearchClient(endpoint, indexName, credential);
#if !SNIPPET
            searchClient = InstrumentClient(new SearchClient(endpoint, indexName, credential, GetSearchClientOptions()));
#endif

            // Get and report the number of documents in the index
            Response <long> count = await searchClient.GetDocumentCountAsync();

            Console.WriteLine($"Search index {indexName} has {count.Value} documents.");
            #endregion Snippet:Azure_Search_Tests_Samples_GetCountAsync
        }
コード例 #5
0
        public async Task GetDocumentCount()
        {
            await using SearchResources search = await SearchResources.GetSharedHotelsIndexAsync(this);

            SearchClient    client   = search.GetSearchClient();
            Response <long> response = await client.GetDocumentCountAsync();

            Assert.AreEqual(200, response.GetRawResponse().Status);
            Assert.AreEqual(SearchResources.TestDocuments.Length, response.Value);
        }
コード例 #6
0
        public async Task SimpleIndexing()
        {
            await using SearchResources resources = SearchResources.CreateWithNoIndexes(this);
            SearchClient searchClient = null;

            try
            {
                searchClient = await CreateIndexAsync(resources);

                // Simple
                #region Snippet:Azure_Search_Documents_Tests_Samples_Sample05_IndexingDocuments_SimpleIndexing1
                IEnumerable <Product> products = GenerateCatalog(count: 1000);
                await searchClient.UploadDocumentsAsync(products);

                #endregion

                await WaitForDocumentCountAsync(searchClient, 1000);

                // When using the free SKU, there may be enough load to prevent
                // immediately replication to all replicas and we get back the
                // wrong count. Wait another second before checking again. We
                // may also upgrade to a basic SKU, but that will take longer
                // to provision.
                await DelayAsync(TimeSpan.FromSeconds(1));

                // Check
                #region Snippet:Azure_Search_Documents_Tests_Samples_Sample05_IndexingDocuments_SimpleIndexing2
                Assert.AreEqual(1000, (int)await searchClient.GetDocumentCountAsync());
                #endregion

                // Too many
                try
                {
                    #region Snippet:Azure_Search_Documents_Tests_Samples_Sample05_IndexingDocuments_SimpleIndexing3
                    IEnumerable <Product> all = GenerateCatalog(count: 100000);
                    await searchClient.UploadDocumentsAsync(all);

                    #endregion

                    Assert.Fail("Expected too many documents failure.");
                }
                catch (RequestFailedException ex)
                {
                    Assert.AreEqual(400, ex.Status);
                }
            }
            finally
            {
                if (searchClient != null)
                {
                    await resources.GetIndexClient().DeleteIndexAsync(searchClient.IndexName);
                }
            }
        }
コード例 #7
0
        public async Task ClientRequestIdRountrips()
        {
            await using SearchResources resources = await SearchResources.GetSharedHotelsIndexAsync(this);

            SearchClient    client   = resources.GetSearchClient();
            Guid            id       = Recording.Random.NewGuid();
            Response <long> response = await client.GetDocumentCountAsync(
                new SearchRequestOptions { ClientRequestId = id });

            Assert.AreEqual(id.ToString(), response.GetRawResponse().ClientRequestId);
        }
コード例 #8
0
        public async Task ClientRequestIdRountrips()
        {
            await using SearchResources resources = await SearchResources.GetSharedHotelsIndexAsync(this);

            SearchClient    client   = resources.GetSearchClient();
            Guid            id       = Recording.Random.NewGuid();
            Response <long> response = await client.GetDocumentCountAsync(
                new SearchRequestOptions { ClientRequestId = id });

            // TODO: #10604 - C# generator doesn't properly support ClientRequestId yet
            // (Assertion is here to remind us to fix this when we do - just
            // change to AreEqual and re-record)
            Assert.AreNotEqual(id.ToString(), response.GetRawResponse().ClientRequestId);
        }
コード例 #9
0
        public async Task SimpleIndexing()
        {
            await using SearchResources resources = SearchResources.CreateWithNoIndexes(this);
            SearchClient searchClient = null;

            try
            {
                searchClient = await CreateIndexAsync(resources);

                // Simple
                #region Snippet:Azure_Search_Documents_Tests_Samples_Sample05_IndexingDocuments_SimpleIndexing1
                IEnumerable <Product> products = GenerateCatalog(count: 1000);
                await searchClient.UploadDocumentsAsync(products);

                #endregion

                await WaitForDocumentCountAsync(searchClient, 1000);

                // Check
                #region Snippet:Azure_Search_Documents_Tests_Samples_Sample05_IndexingDocuments_SimpleIndexing2
                Assert.AreEqual(1000, (int)await searchClient.GetDocumentCountAsync());
                #endregion

                // Too many
                try
                {
                    #region Snippet:Azure_Search_Documents_Tests_Samples_Sample05_IndexingDocuments_SimpleIndexing3
                    IEnumerable <Product> all = GenerateCatalog(count: 100000);
                    await searchClient.UploadDocumentsAsync(all);

                    #endregion

                    Assert.Fail("Expected too many documents failure.");
                }
                catch (RequestFailedException ex)
                {
                    Assert.AreEqual(400, ex.Status);
                }
            }
            finally
            {
                if (searchClient != null)
                {
                    await resources.GetIndexClient().DeleteIndexAsync(searchClient.IndexName);
                }
            }
        }
コード例 #10
0
        public async Task IndexSharesPipeline()
        {
            await using SearchResources resources = await SearchResources.GetSharedHotelsIndexAsync(this);

            TestPipelinePolicy custom = new TestPipelinePolicy();

            Assert.AreEqual(0, custom.RequestCount);

            SearchClientOptions options = new SearchClientOptions(ServiceVersion);

            options.AddPolicy(custom, HttpPipelinePosition.PerCall);
            SearchIndexClient serviceClient = resources.GetIndexClient(options);

            SearchClient client = serviceClient.GetSearchClient(resources.IndexName);

            _ = await client.GetDocumentCountAsync();

            Assert.AreEqual(1, custom.RequestCount);
        }