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);
                }
            }
        }
        public async Task CreateIndex()
        {
            await using SearchResources resources = SearchResources.CreateWithNoIndexes(this);
            Environment.SetEnvironmentVariable("SEARCH_ENDPOINT", resources.Endpoint.ToString());
            Environment.SetEnvironmentVariable("SEARCH_API_KEY", resources.PrimaryApiKey);

            #region Snippet:Azure_Search_Tests_Samples_Readme_CreateIndex
            Uri    endpoint = new Uri(Environment.GetEnvironmentVariable("SEARCH_ENDPOINT"));
            string key      = Environment.GetEnvironmentVariable("SEARCH_API_KEY");

            // Create a service client
            AzureKeyCredential credential = new AzureKeyCredential(key);
            SearchIndexClient  client     = new SearchIndexClient(endpoint, credential);
            /*@@*/ client = resources.GetIndexClient();

            // Create the index using FieldBuilder.
            #region Snippet:Azure_Search_Tests_Samples_Readme_CreateIndex_New_SearchIndex
            //@@SearchIndex index = new SearchIndex("hotels")
            /*@@*/ SearchIndex index = new SearchIndex(Recording.Random.GetName())
            {
                Fields     = new FieldBuilder().Build(typeof(Hotel)),
                Suggesters =
                {
                    // Suggest query terms from the hotelName field.
                    new SearchSuggester("sg", "hotelName")
                }
            };
            #endregion Snippet:Azure_Search_Tests_Samples_Readme_CreateIndex_New_SearchIndex

            client.CreateIndex(index);
            #endregion Snippet:Azure_Search_Tests_Samples_Readme_CreateIndex

            resources.IndexName = index.Name;
        }
        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);
                }
            }
        }
Exemple #4
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);
                }
            }
        }
Exemple #5
0
        public async Task CreateIndex()
        {
            await using SearchResources resources = SearchResources.CreateWithNoIndexes(this);
            Environment.SetEnvironmentVariable("SEARCH_ENDPOINT", resources.Endpoint.ToString());
            Environment.SetEnvironmentVariable("SEARCH_API_KEY", resources.PrimaryApiKey);

            #region Snippet:Azure_Search_Tests_Samples_Readme_CreateIndex
            Uri    endpoint = new Uri(Environment.GetEnvironmentVariable("SEARCH_ENDPOINT"));
            string key      = Environment.GetEnvironmentVariable("SEARCH_API_KEY");

            // Create a service client
            AzureKeyCredential credential = new AzureKeyCredential(key);
            SearchIndexClient  client     = new SearchIndexClient(endpoint, credential);
            /*@@*/ client = resources.GetIndexClient();

            // Create the index
            //@@SearchIndex index = new SearchIndex("hotels")
            /*@@*/ SearchIndex index = new SearchIndex(Recording.Random.GetName())
            {
                Fields =
                {
                    new SimpleField("hotelId",                    SearchFieldDataType.String)
                    {
                        IsKey = true,                             IsFilterable= true, IsSortable  = true
                    },
                    new SearchableField("hotelName")
                    {
                        IsFilterable = true,                      IsSortable  = true
                    },
                    new SearchableField("description")
                    {
                        AnalyzerName = LexicalAnalyzerName.EnLucene
                    },
                    new SearchableField("tags",                   collection: true)
                    {
                        IsFilterable = true,                      IsFacetable = true
                    },
                    new ComplexField("address")
                    {
                        Fields =
                        {
                            new SearchableField("streetAddress"),
                            new SearchableField("city")
                            {
                                IsFilterable = true,             IsSortable                = true, IsFacetable = true
                            },
                            new SearchableField("stateProvince")
                            {
                                IsFilterable = true,             IsSortable                = true, IsFacetable = true
                            },
                            new SearchableField("country")
                            {
                                IsFilterable = true,             IsSortable                = true, IsFacetable = true
                            },
                            new SearchableField("postalCode")
                            {
                                IsFilterable = true,             IsSortable                = true, IsFacetable = true
                            }
                        }
                    }
                },
                Suggesters =
                {
                    // Suggest query terms from both the hotelName and description fields.
                    new SearchSuggester("sg", "hotelName", "description")
                }
            };

            client.CreateIndex(index);
            #endregion Snippet:Azure_Search_Tests_Samples_Readme_CreateIndex
        }
        public async Task CreateManualIndex()
        {
            await using SearchResources resources = SearchResources.CreateWithNoIndexes(this);
            SearchIndexClient client = resources.GetIndexClient();

            #region Snippet:Azure_Search_Tests_Samples_Readme_CreateManualIndex
            // Create the index using field definitions.
            #region Snippet:Azure_Search_Tests_Samples_Readme_CreateManualIndex_New_SearchIndex
            //@@SearchIndex index = new SearchIndex("hotels")
            /*@@*/ SearchIndex index = new SearchIndex(Recording.Random.GetName())
            {
                Fields =
                {
                    new SimpleField("hotelId",                    SearchFieldDataType.String)
                    {
                        IsKey = true,                             IsFilterable= true, IsSortable  = true
                    },
                    new SearchableField("hotelName")
                    {
                        IsFilterable = true,                      IsSortable  = true
                    },
                    new SearchableField("description")
                    {
                        AnalyzerName = LexicalAnalyzerName.EnLucene
                    },
                    new SearchableField("tags",                   collection: true)
                    {
                        IsFilterable = true,                      IsFacetable = true
                    },
                    new ComplexField("address")
                    {
                        Fields =
                        {
                            new SearchableField("streetAddress"),
                            new SearchableField("city")
                            {
                                IsFilterable = true,             IsSortable                = true, IsFacetable = true
                            },
                            new SearchableField("stateProvince")
                            {
                                IsFilterable = true,             IsSortable                = true, IsFacetable = true
                            },
                            new SearchableField("country")
                            {
                                IsFilterable = true,             IsSortable                = true, IsFacetable = true
                            },
                            new SearchableField("postalCode")
                            {
                                IsFilterable = true,             IsSortable                = true, IsFacetable = true
                            }
                        }
                    }
                },
                Suggesters =
                {
                    // Suggest query terms from the hotelName field.
                    new SearchSuggester("sg", "hotelName")
                }
            };
            #endregion Snippet:Azure_Search_Tests_Samples_Readme_CreateManualIndex_New_SearchIndex

            client.CreateIndex(index);
            #endregion Snippet:Azure_Search_Tests_Samples_Readme_CreateManualIndex

            resources.IndexName = index.Name;
        }
        public async Task CreateIndex()
        {
            await using SearchResources resources = SearchResources.CreateWithNoIndexes(this);
            Environment.SetEnvironmentVariable("SEARCH_ENDPOINT", resources.Endpoint.ToString());
            Environment.SetEnvironmentVariable("SEARCH_API_KEY", resources.PrimaryApiKey);

            #region Snippet:Azure_Search_Tests_Sample2_FieldBuilderIgnore_CreateIndex
            Uri    endpoint = new Uri(Environment.GetEnvironmentVariable("SEARCH_ENDPOINT"));
            string key      = Environment.GetEnvironmentVariable("SEARCH_API_KEY");

            // Define client options to use camelCase when serializing property names.
            SearchClientOptions options = new SearchClientOptions(ServiceVersion)
            {
                Serializer = new JsonObjectSerializer(
                    new JsonSerializerOptions
                {
                    PropertyNamingPolicy = JsonNamingPolicy.CamelCase
                })
            };

            // Create a service client.
            AzureKeyCredential credential  = new AzureKeyCredential(key);
            SearchIndexClient  indexClient = new SearchIndexClient(endpoint, credential, options);
#if !SNIPPET
            indexClient = resources.GetIndexClient(options);
#endif

            // Create the FieldBuilder using the same serializer.
            FieldBuilder fieldBuilder = new FieldBuilder
            {
                Serializer = options.Serializer
            };

            // Create the index using FieldBuilder.
#if SNIPPET
            SearchIndex index = new SearchIndex("movies")
#else
            SearchIndex index = new SearchIndex(Recording.Random.GetName())
#endif
            {
                Fields     = fieldBuilder.Build(typeof(Movie)),
                Suggesters =
                {
                    // Suggest query terms from the "name" field.
                    new SearchSuggester("n", "name")
                }
            };

            // Define the "genre" field as a string.
            SearchableField genreField = new SearchableField("genre")
            {
                AnalyzerName = LexicalAnalyzerName.Values.EnLucene,
                IsFacetable  = true,
                IsFilterable = true
            };
            index.Fields.Add(genreField);

            // Create the index.
            indexClient.CreateIndex(index);
            #endregion Snippet:Azure_Search_Tests_Sample2_FieldBuilderIgnore_CreateIndex

            // Make sure the index is removed.
            resources.IndexName = index.Name;

            #region Snippet:Azure_Search_Tests_Sample2_FieldBuilderIgnore_UploadDocument
            Movie movie = new Movie
            {
                Id     = Guid.NewGuid().ToString("n"),
                Name   = "The Lord of the Rings: The Return of the King",
                Genre  = MovieGenre.Fantasy,
                Year   = 2003,
                Rating = 9.1
            };

            // Add a movie to the index.
            SearchClient searchClient = indexClient.GetSearchClient(index.Name);
            searchClient.UploadDocuments(new[] { movie });
            #endregion Snippet:Azure_Search_Tests_Sample2_FieldBuilderIgnore_UploadDocument
        }