public async Task Index()
        {
            await using SearchResources resources = await SearchResources.CreateWithEmptyHotelsIndexAsync(this);

            SearchIndexClient client = resources.GetQueryClient();

            #region Snippet:Azure_Search_Tests_Samples_Readme_Index
            IndexDocumentsBatch <Hotel> batch = IndexDocumentsBatch.Create(
                IndexDocumentsAction.Upload(new Hotel {
                Id = "783", Name = "Upload Inn"
            }),
                IndexDocumentsAction.Merge(new Hotel {
                Id = "12", Name = "Renovated Ranch"
            }));

            IndexDocumentsOptions options = new IndexDocumentsOptions {
                ThrowOnAnyError = true
            };
            //@@ client.IndexDocuments(batch, options);
            #endregion Snippet:Azure_Search_Tests_Samples_Readme_Index
            try
            {
                await client.IndexDocumentsAsync(batch, options);
            }
            catch (RequestFailedException)
            {
                // Ignore the non-existent merge failure
            }
        }
Esempio n. 2
0
        /// <summary>
        /// Create a hotels index with the standard test documents and as many
        /// extra empty documents needed to test.
        /// </summary>
        /// <param name="size">The total number of documents in the index.</param>
        /// <returns>SearchResources for testing.</returns>
        public async Task <SearchResources> CreateLargeHotelsIndexAsync(int size)
        {
            // Start with the standard test hotels
            SearchResources resources = await SearchResources.CreateWithHotelsIndexAsync(this);

            // Create empty hotels with just an ID for the rest
            int existingDocumentCount     = SearchResources.TestDocuments.Length;
            IEnumerable <string> hotelIds =
                Enumerable.Range(
                    existingDocumentCount + 1,
                    size - existingDocumentCount)
                .Select(id => id.ToString());
            List <SearchDocument> hotels = hotelIds.Select(id => new SearchDocument {
                ["hotelId"] = id
            }).ToList();

            // Upload the empty hotels in batches of 1000 until we're complete
            SearchIndexClient client = resources.GetIndexClient();

            for (int i = 0; i < hotels.Count; i += 1000)
            {
                IEnumerable <SearchDocument> nextHotels = hotels.Skip(i).Take(1000);
                if (!nextHotels.Any())
                {
                    break;
                }
                await client.IndexDocumentsAsync(IndexDocumentsBatch.Upload(nextHotels));

                await resources.WaitForIndexingAsync();
            }

            return(resources);
        }