Exemple #1
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);
        }
        public async Task GetDocumentCount()
        {
            await using SearchResources search = await SearchResources.GetSharedHotelsIndexAsync(this);

            SearchIndexClient client   = search.GetIndexClient();
            Response <long>   response = await client.GetDocumentCountAsync();

            Assert.AreEqual(200, response.GetRawResponse().Status);
            Assert.AreEqual(SearchResources.TestDocuments.Length, response.Value);
        }
        public async Task ClientRequestIdRountrips()
        {
            await using SearchResources resources = await SearchResources.GetSharedHotelsIndexAsync(this);

            SearchIndexClient client = resources.GetIndexClient();
            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);
        }
        public async Task RecentlyIndexedDynamicDocument()
        {
            await using SearchResources resources = await SearchResources.CreateWithEmptyHotelsIndexAsync(this);

            Hotel document = SearchResources.TestDocuments[0];

            await resources.GetIndexClient().IndexDocumentsAsync(
                IndexDocumentsBatch.Upload(new[] { document.AsDocument() }));

            await resources.WaitForIndexingAsync();

            Response <Hotel> response = await resources.GetQueryClient().GetDocumentAsync <Hotel>(document.HotelId);

            Assert.AreEqual(document.HotelId, response.Value.HotelId);
        }
        public async Task VerifyRoundtrip <T>(
            Func <T, string> getKey,
            T document,
            T expected = default,
            GetDocumentOptions options = null)
        {
            await using SearchResources resources = await SearchResources.CreateWithEmptyHotelsIndexAsync(this);

            await resources.GetIndexClient().IndexDocumentsAsync <T>(
                IndexDocumentsBatch.Upload <T>(new[] { document }));

            await resources.WaitForIndexingAsync();

            Response <T> response = await resources.GetQueryClient().GetDocumentAsync <T>(getKey(document), options);

            // Only validate expected properties
            AssertApproximate(expected ?? document, response.Value);
        }
        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);
        }
        public async Task Structs()
        {
            await using SearchResources resources = await SearchResources.CreateWithEmptyHotelsIndexAsync(this);

            SimpleStructHotel document = new SimpleStructHotel
            {
                HotelId   = "4",
                HotelName = "Value Inn"
            };

            await resources.GetIndexClient().IndexDocumentsAsync(
                IndexDocumentsBatch.Upload(new[] { document }));

            await resources.WaitForIndexingAsync();

            SearchIndexClient            client   = resources.GetQueryClient();
            Response <SimpleStructHotel> response = await client.GetDocumentAsync <SimpleStructHotel>(document.HotelId);

            Assert.AreEqual(document, response.Value);
        }
        public async Task EmptyValuesDynamicDocument()
        {
            await using SearchResources resources = await SearchResources.CreateWithEmptyHotelsIndexAsync(this);

            SearchDocument document =
                new SearchDocument
            {
                ["hotelId"]            = "1",
                ["hotelName"]          = null,
                ["tags"]               = new object[0],
                ["parkingIncluded"]    = null,
                ["lastRenovationDate"] = null,
                ["rating"]             = null,
                ["location"]           = null,
                ["address"]            = null,
                ["rooms"]              = new[]
                {
                    new SearchDocument
                    {
                        ["baseRate"]       = null,
                        ["bedOptions"]     = null,
                        ["sleepsCount"]    = null,
                        ["smokingAllowed"] = null,
                        ["tags"]           = new object[0]
                    }
                }
            };

            await resources.GetIndexClient().IndexDocumentsAsync(
                IndexDocumentsBatch.Upload(new[] { document }));

            await resources.WaitForIndexingAsync();

            Response <SearchDocument> response = await resources.GetQueryClient().GetDocumentAsync((string)document["hotelId"]);

            Assert.AreEqual(document["hotelId"], response.Value["hotelId"]);
        }