Beispiel #1
0
        public async Task DeleteAsync <T>(T item, IGraphRequestContext graphRequestContext) where T : class
        {
            var client = Get <T>(graphRequestContext);

            var batch = IndexDocumentsBatch.Create(IndexDocumentsAction.Delete(item));
            await client.IndexDocumentsAsync(batch);
        }
        private static void AddDocuments()
        {
            var credential = new AzureKeyCredential(adminKey);
            var client     = new SearchClient(endpoint, indexName, credential);
            var action1    = IndexDocumentsAction.Upload(new Ninja
            {
                Id   = Guid.NewGuid().ToString(),
                Name = "Naruto Uzumaki"
            });
            var action2 = IndexDocumentsAction.Upload(new Ninja
            {
                Id   = Guid.NewGuid().ToString(),
                Name = "Sasuke Uchiha"
            });
            var batch = IndexDocumentsBatch.Create(action1, action2);

            try
            {
                IndexDocumentsResult result = client.IndexDocuments(batch);
            }
            catch (Exception)
            {
                Console.WriteLine("Failed to index some of the documents: {0}");
            }
        }
Beispiel #3
0
        public async Task Index()
        {
            await using SearchResources resources = await SearchResources.CreateWithEmptyHotelsIndexAsync(this);

            SearchClient client = resources.GetQueryClient();

            try
            {
                #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
            }
            catch (RequestFailedException)
            {
                // Ignore the non-existent merge failure
            }
        }
        public void DeleteCustomerData(CustomerIndex customerToDelete)
        {
            IndexDocumentsBatch <CustomerIndex> batch = IndexDocumentsBatch.Create(IndexDocumentsAction.Delete(customerToDelete));
            IndexDocumentsOptions idxoptions          = new IndexDocumentsOptions {
                ThrowOnAnyError = true
            };

            _qryClient.IndexDocuments(batch, idxoptions);
        }
Beispiel #5
0
        static async Task Main(string[] args)
        {
            var reviewText = "The quality of the pictures are good, but the body is not durable";
            var productId  = "1"; // ID of the product reviewed
            var reviewId   = "1"; // ID of the review used as the search document ID
            var indexName  = "sample-index";

            // Cognitive Search credentials
            var searchKey      = Environment.GetEnvironmentVariable("COGNITIVE_SEARCH_KEY");
            var searchEndpoint = Environment.GetEnvironmentVariable("COGNITIVE_SEARCH_ENDPOINT");
            Uri searchUri      = new Uri(searchEndpoint);
            AzureKeyCredential searchCredential = new AzureKeyCredential(searchKey);

            // Initialize Search Index client
            var searchIndexClient = new SearchIndexClient(searchUri, searchCredential);

            // Create Index
            await CreateIndex(indexName, searchIndexClient);

            // Initialize Search client
            var searchClient = new SearchClient(searchUri, indexName, searchCredential);

            // TA credentials
            var textAnalyticsKey         = Environment.GetEnvironmentVariable("TEXT_ANALYTICS_KEY");
            var textAnalyticsEndpoint    = Environment.GetEnvironmentVariable("TEXT_ANALYTICS_ENDPOINT");
            var textAnalyticsCredentials = new AzureKeyCredential(textAnalyticsKey);
            var textAnalyticsUri         = new Uri(textAnalyticsEndpoint);

            // Initialize TA client
            var textAnalyticsClient = new TextAnalyticsClient(textAnalyticsUri, textAnalyticsCredentials);

            // Enable opinion mining
            var options = new AnalyzeSentimentOptions()
            {
                IncludeOpinionMining = true
            };

            // Call TA analyze sentiment api
            var sentimentResponse = await textAnalyticsClient.AnalyzeSentimentAsync(reviewText, language : "en", options : options);

            // Map to review search document
            Review review = CreateReviewDocument(productId, reviewId, sentimentResponse);

            // Upload document
            var batch = IndexDocumentsBatch.Create(IndexDocumentsAction.Upload(review));

            try
            {
                IndexDocumentsResult result = await searchClient.IndexDocumentsAsync(batch);
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }
        }
        private static async Task AddDataAsync(SearchClient searchClient)
        {
            var translator = new Translator();

            string modelName1       = "external styles are defined withing the element, inside the section of an HTML page";
            string polishModelName1 = await translator.GetTranslatedTextAsync(modelName1);

            string[] keyWords1        = { "CSS", "styles", "metadata", "element", "EF01" };
            string   modelName2       = "To include an external JavaScript file, use the script tag with the attribute src";
            string   polishModelName2 = await translator.GetTranslatedTextAsync(modelName2);

            string[] keyWords2        = { "javascript", "EF02", "script", "tag", "src" };
            string   modelName3       = "Move to the home position";
            string   polishModelName3 = await translator.GetTranslatedTextAsync(modelName3);

            string[] keyWords3        = { "storage", "container", "EF03", "home" };
            string   modelName4       = "removal of stuck magnets restores machine cycle";
            string   polishModelName4 = await translator.GetTranslatedTextAsync(modelName4);

            string[] keyWords4 = { "magnets", "machine", "cycle", "EF04" };

            var batch = IndexDocumentsBatch.Create(
                IndexDocumentsAction.Upload(new SearchModel
            {
                Id         = Guid.NewGuid().ToString(), Name = modelName1, PolishName = polishModelName1,
                KeyPhrases = keyWords1, Updated = new DateTime(2020, 10, 1, 7, 0, 0)
            })
                , IndexDocumentsAction.Upload(new SearchModel
            {
                Id         = Guid.NewGuid().ToString(),
                Name       = modelName2,
                KeyPhrases = keyWords2,
                PolishName = polishModelName2,
                Updated    = new DateTime(2020, 9, 2, 8, 54, 0)
            }), IndexDocumentsAction.Upload(new SearchModel
            {
                Id         = Guid.NewGuid().ToString(),
                Name       = modelName4,
                KeyPhrases = keyWords4,
                PolishName = polishModelName4,
                Updated    = new DateTime(2020, 8, 2, 12, 11, 0)
            }), IndexDocumentsAction.Upload(new SearchModel
            {
                Id         = Guid.NewGuid().ToString(),
                Name       = modelName3,
                KeyPhrases = keyWords3,
                PolishName = polishModelName3,
                Updated    = new DateTime(2020, 7, 2, 21, 21, 0)
            }));

            await searchClient.IndexDocumentsAsync(batch, new IndexDocumentsOptions { ThrowOnAnyError = true });
        }
Beispiel #7
0
        public async Task UploadDocument(AlbumInfoSearchObject document)
        {
            IndexDocumentsBatch <AlbumInfoSearchObject> batch = IndexDocumentsBatch.Create(IndexDocumentsAction.MergeOrUpload(document));

            try
            {
                IndexDocumentsResult result = await searchClient.IndexDocumentsAsync(batch);
            }
            catch (Exception ex)
            {
                throw;
            }
        }
Beispiel #8
0
        private static void UploadDocuments(SearchClient searchClient)
        {
            IndexDocumentsBatch <Hotel> batch = IndexDocumentsBatch.Create(
                IndexDocumentsAction.Upload(
                    new Hotel()
            {
                HotelId            = "1",
                BaseRate           = 199.0,
                Description        = "Best hotel in town",
                DescriptionFr      = "Meilleur hôtel en ville",
                HotelName          = "Fancy Stay",
                Category           = "Luxury",
                Tags               = new[] { "pool", "view", "wifi", "concierge" },
                ParkingIncluded    = false,
                SmokingAllowed     = false,
                LastRenovationDate = new DateTimeOffset(2010, 6, 27, 0, 0, 0, TimeSpan.Zero),
                Rating             = 5,
                Location           = GeographyPoint.Create(47.678581, -122.131577)
            }),
                IndexDocumentsAction.Upload(
                    new Hotel()
            {
                HotelId            = "2",
                BaseRate           = 79.99,
                Description        = "Cheapest hotel in town",
                DescriptionFr      = "Hôtel le moins cher en ville",
                HotelName          = "Roach Motel",
                Category           = "Budget",
                Tags               = new[] { "motel", "budget" },
                ParkingIncluded    = true,
                SmokingAllowed     = true,
                LastRenovationDate = new DateTimeOffset(1982, 4, 28, 0, 0, 0, TimeSpan.Zero),
                Rating             = 1,
                Location           = GeographyPoint.Create(49.678581, -122.131577)
            }));

            try
            {
                IndexDocumentsResult result = searchClient.IndexDocuments(batch);
            }
            catch (Exception)
            {
                // 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}");
            }

            Console.WriteLine("Waiting for documents to be indexed...\n");
            Thread.Sleep(2000);
        }
Beispiel #9
0
        public async Task ExecuteAsync(IndexCommand[] commands,
                                       CancellationToken ct = default)
        {
            var batch = IndexDocumentsBatch.Create <SearchDocument>();

            commands.Foreach(x => CommandFactory.CreateCommands(x, batch.Actions));

            if (batch.Actions.Count == 0)
            {
                return;
            }

            await searchClient.IndexDocumentsAsync(batch, cancellationToken : ct);
        }
Beispiel #10
0
        /// <summary>
        /// Añade o elimina items dentro de azure search.
        /// </summary>
        /// <typeparam name="T">El tipo solo puede ser una entidad soportada dentro de azure search, se validará que cumpla</typeparam>
        /// <param name="elements">elementos a guardar dentro del search</param>
        /// <param name="operationType">Tipo de operación Añadir o borrar</param>
        private void OperationElements <T>(List <T> elements, SearchOperation operationType)
        {
            // validar que sea un elemento de tipo search.
            var indexName = Index;

            // realiza la acción segun el argumento
            var actions = elements.Select(o => operationType == SearchOperation.Add ? IndexDocumentsAction.Upload(o) : IndexDocumentsAction.Delete(o)).ToArray();

            // preparando la ejecución
            var batch = IndexDocumentsBatch.Create(actions);

            // ejecución.
            _search.IndexDocuments(batch);
        }
        private static void IndexDocuments(string indexName, List <string> groups)
        {
            IndexDocumentsBatch <SecuredFiles> batch = IndexDocumentsBatch.Create(
                IndexDocumentsAction.Upload(
                    new SecuredFiles()
            {
                FileId   = "1",
                Name     = "secured_file_a",
                GroupIds = new[] { groups[0] }
            }),
                IndexDocumentsAction.Upload(
                    new SecuredFiles()
            {
                FileId   = "2",
                Name     = "secured_file_b",
                GroupIds = new[] { groups[0] }
            }),
                IndexDocumentsAction.Upload(
                    new SecuredFiles()
            {
                FileId   = "3",
                Name     = "secured_file_c",
                GroupIds = new[] { groups[1] }
            }));

            try
            {
                IndexDocumentsResult result = searchClient.IndexDocuments(batch);
            }
            catch (Exception)
            {
                // 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}");
            }

            Console.WriteLine("Waiting for documents to be indexed...\n");
            Thread.Sleep(2000);
        }
Beispiel #12
0
        public async Task Convenience_None()
        {
            await using SearchResources resources = await SearchResources.CreateWithEmptyIndexAsync <SimpleDocument>(this);

            SearchClient client = resources.GetSearchClient();

            SimpleDocument[] data = SimpleDocument.GetDocuments(3);

            await using SearchIndexingBufferedSender <SimpleDocument> indexer =
                            client.CreateIndexingBufferedSender(
                                new SearchIndexingBufferedSenderOptions <SimpleDocument>());
            AssertNoFailures(indexer);
            IndexDocumentsBatch <SimpleDocument> batch = IndexDocumentsBatch.Create(
                IndexDocumentsAction.Delete <SimpleDocument>(data[0]),
                IndexDocumentsAction.Upload <SimpleDocument>(data[1]),
                IndexDocumentsAction.MergeOrUpload <SimpleDocument>(data[2]));
            await indexer.IndexDocumentsAsync(batch);

            await indexer.FlushAsync();

            await WaitForDocumentCountAsync(resources.GetSearchClient(), 2);
        }
Beispiel #13
0
        public async Task CreateAndPopulateIndexAsync <T>(string indexName, List <T> entries)
        {
            var serviceClient = _factory.SearchIndexClient;

            await DeleteIndexAsync(indexName).ConfigureAwait(false);

            var definition = new SearchIndex(indexName)
            {
                Fields = new FieldBuilder().Build(typeof(T))
            };
            await serviceClient.CreateIndexAsync(definition).ConfigureAwait(false);

            var indexClient = serviceClient.GetSearchClient(indexName);

            var batch = IndexDocumentsBatch.Create <T>();

            _logger.LogInformation("Indexing {Count} entries for Index {IndexName}", entries.Count, indexName);

            for (var counter = 0; counter < entries.Count; counter++)
            {
                batch.Actions.Add(IndexDocumentsAction.Upload(entries[counter]));
                if (counter % 5000 == 0) //it's too big for one batch
                {
                    await indexClient.IndexDocumentsAsync(batch, new Azure.Search.Documents.IndexDocumentsOptions {
                        ThrowOnAnyError = true
                    }).ConfigureAwait(false);

                    batch = IndexDocumentsBatch.Create <T>();
                    _logger.LogInformation("Submitted {Counter} of {Total} for Index {IndexName}", counter, entries.Count, indexName);
                }
            }

            await indexClient.IndexDocumentsAsync(batch, new Azure.Search.Documents.IndexDocumentsOptions {
                ThrowOnAnyError = true
            }).ConfigureAwait(false);

            _logger.LogInformation("Completed index submission for Index {IndexName}", indexName);
        }
Beispiel #14
0
        private static void MergeOrUploadDocuments(SearchClient searchClient)
        {
            IndexDocumentsBatch <Hotel> batch = IndexDocumentsBatch.Create(
                IndexDocumentsAction.MergeOrUpload(
                    new Hotel()
            {
                HotelId = "4",
                Rating  = 1,
                Tags    = new[] { "concierge", "view", "24-hour front desk service" },
            })
                );

            try
            {
                IndexDocumentsResult result = searchClient.IndexDocuments(batch);
            }
            catch (Exception e)
            {
                // If for some reason any documents are dropped during indexing, you can compensate by delaying and
                // retrying. This simple demo just logs the failed document keys and continues.
                Console.WriteLine($"Failed to index some of the documents: {e}");
            }
        }
Beispiel #15
0
        private async Task <Response <IndexDocumentsResult> > IndexProduct(SearchClient searchClient, ProductDocument product)
        {
            var batch = IndexDocumentsBatch.Create(IndexDocumentsAction.Upload(product));

            return(await searchClient.IndexDocumentsAsync(batch));
        }
        static void Main(string[] args)
        {
            string serviceName = "<YOUR-SEARCH-SERVICE-NAME>";
            string indexName   = "hotels-quickstart-v11";
            string apiKey      = "<YOUR-ADMIN-API-KEY>";

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

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

            // Delete index if it exists
            Console.WriteLine("{0}", "Deleting index...\n");
            DeleteIndexIfExists(indexName, idxclient);

            // Define an index schema and create the index
            SearchIndex index = new SearchIndex(indexName)
            {
                Fields =
                {
                    new SimpleField("hotelId",            SearchFieldDataType.String)
                    {
                        IsKey = true,                     IsFilterable= true, IsSortable = true
                    },
                    new SearchableField("hotelName")
                    {
                        IsFilterable = true,              IsSortable = true
                    },
                    new SearchableField("hotelCategory")
                    {
                        IsFilterable = true,              IsSortable = true
                    },
                    new SimpleField("baseRate",           SearchFieldDataType.Int32)
                    {
                        IsFilterable = true,              IsSortable = true
                    },
                    new SimpleField("lastRenovationDate", SearchFieldDataType.DateTimeOffset)
                    {
                        IsFilterable = true,              IsSortable = true
                    }
                }
            };

            Console.WriteLine("{0}", "Creating index...\n");
            idxclient.CreateIndex(index);

            // Load documents (using a subset of fields for brevity)
            IndexDocumentsBatch <Hotel> batch = IndexDocumentsBatch.Create(
                IndexDocumentsAction.Upload(new Hotel {
                Id = "78", Name = "Upload Inn", Category = "hotel", Rate = 279, Updated = new DateTime(2018, 3, 1, 7, 0, 0)
            }),
                IndexDocumentsAction.Upload(new Hotel {
                Id = "54", Name = "Breakpoint by the Sea", Category = "motel", Rate = 162, Updated = new DateTime(2015, 9, 12, 7, 0, 0)
            }),
                IndexDocumentsAction.Upload(new Hotel {
                Id = "39", Name = "Debug Motel", Category = "motel", Rate = 159, Updated = new DateTime(2016, 11, 11, 7, 0, 0)
            }),
                IndexDocumentsAction.Upload(new Hotel {
                Id = "48", Name = "NuGet Hotel", Category = "hotel", Rate = 238, Updated = new DateTime(2016, 5, 30, 7, 0, 0)
            }),
                IndexDocumentsAction.Upload(new Hotel {
                Id = "12", Name = "Renovated Ranch", Category = "motel", Rate = 149, Updated = new DateTime(2020, 1, 24, 7, 0, 0)
            }));

            IndexDocumentsOptions idxoptions = new IndexDocumentsOptions {
                ThrowOnAnyError = true
            };

            Console.WriteLine("{0}", "Loading index...\n");
            qryclient.IndexDocuments(batch, idxoptions);

            // Wait 2 secondsfor indexing to complete before starting queries (for demo and console-app purposes only)
            Console.WriteLine("Waiting for indexing...\n");
            System.Threading.Thread.Sleep(2000);

            // Call the RunQueries method to invoke a series of queries
            Console.WriteLine("Starting queries...\n");
            RunQueries(qryclient);

            // End the program
            Console.WriteLine("{0}", "Complete. Press any key to end this program...\n");
            Console.ReadKey();
        }
Beispiel #17
0
        /// <summary>
        /// Send indexing actions to be processed by the service.
        /// </summary>
        /// <param name="batch">The batch of actions to submit.</param>
        /// <param name="cancellationToken">A cancellation token.</param>
        /// <returns>Whether the submission was throttled.</returns>
        protected override async Task <bool> OnSubmitBatchAsync(IList <PublisherAction <IndexDocumentsAction <T> > > batch, CancellationToken cancellationToken)
        {
            // Bail early if someone sent an empty batch
            if (batch.Count == 0)
            {
                return(false);
            }

            // Notify the action is being sent
            foreach (PublisherAction <IndexDocumentsAction <T> > action in batch)
            {
                await _sender.OnActionSentAsync(action.Document, cancellationToken).ConfigureAwait(false);
            }

            AzureSearchDocumentsEventSource.Instance.BatchSubmitted(_sender.Endpoint.AbsoluteUri, batch.Count);

            // Send the request to the service
            Response <IndexDocumentsResult> response = null;

            try
            {
                response = await _sender.SearchClient.IndexDocumentsAsync(
                    IndexDocumentsBatch.Create(batch.Select(a => a.Document).ToArray()),
                    cancellationToken : cancellationToken)
                           .ConfigureAwait(false);
            }
            // Handle batch level failures
            catch (RequestFailedException ex) when(ex.Status == 413)  // Payload Too Large
            {
                int oldBatchActionCount = BatchActionCount;

                // Split the batch and try with smaller payloads
                // Update 'BatchActionCount' so future submissions can avoid this error.
                BatchActionCount = (int)Math.Floor((double)batch.Count / 2.0);

                AzureSearchDocumentsEventSource.Instance.BatchActionCountUpdated(_sender.Endpoint.AbsoluteUri, oldBatchActionCount, BatchActionCount);

                var smaller = new List <PublisherAction <IndexDocumentsAction <T> > >(batch.Take(BatchActionCount));

                // Add the second half to the retry queue without counting this as a retry attempt
                EnqueueRetry(batch.Skip(BatchActionCount));

                // Try resubmitting with just the smaller half
                await SubmitBatchAsync(smaller, cancellationToken).ConfigureAwait(false);

                return(false);
            }
            catch (Exception ex)
            {
                // Retry the whole batch using the same exception for everything
                foreach (PublisherAction <IndexDocumentsAction <T> > action in batch)
                {
                    await EnqueueOrFailRetryAsync(action, null, ex, cancellationToken).ConfigureAwait(false);
                }

                // Search currently uses 503s for throttling
                return(ex is RequestFailedException failure && failure.Status == 503);
            }

            // Handle individual responses which might be success or failure
            bool throttled = false;

            foreach ((PublisherAction <IndexDocumentsAction <T> > action, IndexingResult result) in
                     AssociateResults(batch, response.Value.Results))
            {
                // Search currently uses 503s for throttling
                throttled |= (result.Status == 503);

                Debug.Assert(action.Key == result.Key);
                if (result.Succeeded)
                {
                    await _sender.OnActionCompletedAsync(
                        action.Document,
                        result,
                        cancellationToken)
                    .ConfigureAwait(false);
                }
                else if (IsRetriable(result.Status))
                {
                    await EnqueueOrFailRetryAsync(
                        action,
                        result,
                        exception : null,
                        cancellationToken)
                    .ConfigureAwait(false);
                }
                else
                {
                    await _sender.OnActionFailedAsync(
                        action.Document,
                        result,
                        exception : null,
                        cancellationToken)
                    .ConfigureAwait(false);
                }
            }
            return(throttled);
        }
Beispiel #18
0
 public async Task BatchCreateAsync <T>(IEnumerable <T> items, IGraphRequestContext graphRequestContext) where T : class
 {
     var client = Get <T>(graphRequestContext);
     var list   = items.Select(IndexDocumentsAction.Upload).ToArray();
     await client.IndexDocumentsAsync(IndexDocumentsBatch.Create(list));
 }
Beispiel #19
0
        public async Task Run([QueueTrigger("%JUMBO_QUEUE_NAME%", Connection = "JUMBO_CONNECTION_STRING")] string searchTerm, ILogger log)
        {
            const string merchantName = "Jumbo";
            var          page         = await BrowserContext.NewPageAsync().ConfigureAwait(false);

            await page.GotoAsync($"https://www.jumbo.com/zoeken?searchTerms={HttpUtility.UrlEncode(searchTerm)}", new PageGotoOptions { Timeout = 0 });

            var productElements = await page.QuerySelectorAllAsync("//*[@analytics-tag='product card']");

            foreach (var productElement in productElements)
            {
                try
                {
                    using Task <IElementHandle?> getProductTitle       = productElement.QuerySelectorAsync("css=[class='title-link']");
                    using Task <IElementHandle?> getPriceUnits         = productElement.QuerySelectorAsync("css=[class='whole']");
                    using Task <IElementHandle?> getPriceFraction      = productElement.QuerySelectorAsync("css=[class='fractional']");
                    using Task <IElementHandle?> getUnitSizeAndMeasure = productElement.QuerySelectorAsync("css=[class='price-per-unit']");
                    using Task <IElementHandle?> getImageSource        = productElement.QuerySelectorAsync("css=[class='image']");
                    using Task <IElementHandle?> getDetailsPageUrl     = productElement.QuerySelectorAsync("css=a:first-child");

                    await Task.WhenAll(getImageSource, getPriceFraction, getUnitSizeAndMeasure, getPriceUnits, getProductTitle, getDetailsPageUrl);

                    IElementHandle?productTitleHandle = await getProductTitle.ConfigureAwait(false);

                    IElementHandle?productPriceUnitsHandle = await getPriceUnits.ConfigureAwait(false);

                    IElementHandle?productPriceFractionHandle = await getPriceFraction.ConfigureAwait(false);

                    IElementHandle?unitSizeAndMeasureHandle = await getUnitSizeAndMeasure.ConfigureAwait(false);

                    IElementHandle?imageElementHandle = await getImageSource.ConfigureAwait(false);

                    string?id = await productElement.GetAttributeAsync("data-product-Id").ConfigureAwait(false);

                    string?title = productTitleHandle is not null ? await productTitleHandle.TextContentAsync().ConfigureAwait(false) : "";

                    string?detailsPageLink = productTitleHandle is not null ? await productTitleHandle.GetAttributeAsync("href").ConfigureAwait(false) : "";

                    string?priceUnits = productPriceUnitsHandle is not null ? await productPriceUnitsHandle.TextContentAsync().ConfigureAwait(false) : "";

                    string?priceFraction = productPriceFractionHandle is not null ? await productPriceFractionHandle.TextContentAsync().ConfigureAwait(false) : "";

                    string?unitSize      = "";
                    string?unitOfMeasure = "";
                    if (unitSizeAndMeasureHandle is not null)
                    {
                        var unitSizeAndMeasure = await unitSizeAndMeasureHandle.TextContentAsync().ConfigureAwait(false);

                        var parts = unitSizeAndMeasure is not null?unitSizeAndMeasure.Split('/') : new[] { "", "" };
                        unitSize      = RemoveSpecialCharacters(parts[0]);
                        unitOfMeasure = RemoveSpecialCharacters(parts[1]);
                    }
                    string?imageSource = await imageElementHandle.GetAttributeAsync("src").ConfigureAwait(false);

                    string?description = "";

                    if (detailsPageLink is not null)
                    {
                        await using var detailsContext = await Browser.NewContextAsync(new BrowserNewContextOptions { }).ConfigureAwait(false);

                        var detailsPage = await detailsContext.NewPageAsync().ConfigureAwait(false);

                        await detailsPage.GotoAsync($"https://www.jumbo.com{detailsPageLink}", new PageGotoOptions { Timeout = 0 });

                        var descriptionElement = await detailsPage.QuerySelectorAsync("css=[analytics-tag='product description collapsible']");

                        description = descriptionElement is not null ? await descriptionElement.TextContentAsync() ?? string.Empty : "";

                        await detailsContext.CloseAsync().ConfigureAwait(false);
                    }

                    var validatedProduct = Product.Create(id, title, description, merchantName, priceUnits, priceFraction, imageSource, unitSize, unitOfMeasure);

                    switch (validatedProduct)
                    {
                    case Valid <Product>(var product):
                        var result = IndexDocumentsAction.MergeOrUpload(product.ToIndexableProduct());
                        await SearchClient.IndexDocumentsAsync(IndexDocumentsBatch.Create(result));

                        break;

                    case Invalid <Product>(var errors):
                        // TODO : proper tracing
                        foreach (var error in errors)
                        {
                            Console.WriteLine(error);
                        }
                        break;

                    default:
                        break;
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.StackTrace);
                }
            }

            await page.CloseAsync();
        }
        // Upload documents in a single Upload request.
        private static void UploadDocuments(SearchClient searchClient)
        {
            IndexDocumentsBatch <Hotel> batch = IndexDocumentsBatch.Create(
                IndexDocumentsAction.Upload(
                    new Hotel()
            {
                HotelId            = "1",
                HotelName          = "Secret Point Motel",
                Description        = "The hotel is ideally located on the main commercial artery of the city in the heart of New York. A few minutes away is Time's Square and the historic centre of the city, as well as other places of interest that make New York one of America's most attractive and cosmopolitan cities.",
                DescriptionFr      = "L'hôtel est idéalement situé sur la principale artère commerciale de la ville en plein cœur de New York. A quelques minutes se trouve la place du temps et le centre historique de la ville, ainsi que d'autres lieux d'intérêt qui font de New York l'une des villes les plus attractives et cosmopolites de l'Amérique.",
                Category           = "Boutique",
                Tags               = new[] { "pool", "air conditioning", "concierge" },
                ParkingIncluded    = false,
                LastRenovationDate = new DateTimeOffset(1970, 1, 18, 0, 0, 0, TimeSpan.Zero),
                Rating             = 3.6,
                Location           = GeographyPoint.Create(40.760586, -73.975403),
                Address            = new Address()
                {
                    StreetAddress = "677 5th Ave",
                    City          = "New York",
                    StateProvince = "NY",
                    PostalCode    = "10022",
                    Country       = "USA"
                },
                Rooms = new Room[]
                {
                    new Room()
                    {
                        Description    = "Budget Room, 1 Queen Bed (Cityside)",
                        DescriptionFr  = "Chambre Économique, 1 grand lit (côté ville)",
                        Type           = "Budget Room",
                        BaseRate       = 96.99,
                        BedOptions     = "1 Queen Bed",
                        SleepsCount    = 2,
                        SmokingAllowed = true,
                        Tags           = new[] { "vcr/dvd" }
                    },
                    new Room()
                    {
                        Description    = "Budget Room, 1 King Bed (Mountain View)",
                        DescriptionFr  = "Chambre Économique, 1 très grand lit (Mountain View)",
                        Type           = "Budget Room",
                        BaseRate       = 80.99,
                        BedOptions     = "1 King Bed",
                        SleepsCount    = 2,
                        SmokingAllowed = true,
                        Tags           = new[] { "vcr/dvd", "jacuzzi tub" }
                    },
                    new Room()
                    {
                        Description    = "Deluxe Room, 2 Double Beds (City View)",
                        DescriptionFr  = "Chambre Deluxe, 2 lits doubles (vue ville)",
                        Type           = "Deluxe Room",
                        BaseRate       = 150.99,
                        BedOptions     = "2 Double Beds",
                        SleepsCount    = 2,
                        SmokingAllowed = false,
                        Tags           = new[] { "suite", "bathroom shower", "coffee maker" }
                    }
                }
            }),
                IndexDocumentsAction.Upload(
                    new Hotel()
            {
                HotelId            = "2",
                HotelName          = "Twin Dome Motel",
                Description        = "The hotel is situated in a  nineteenth century plaza, which has been expanded and renovated to the highest architectural standards to create a modern, functional and first-class hotel in which art and unique historical elements coexist with the most modern comforts.",
                DescriptionFr      = "L'hôtel est situé dans une place du XIXe siècle, qui a été agrandie et rénovée aux plus hautes normes architecturales pour créer un hôtel moderne, fonctionnel et de première classe dans lequel l'art et les éléments historiques uniques coexistent avec le confort le plus moderne.",
                Category           = "Boutique",
                Tags               = new[] { "pool", "free wifi", "concierge" },
                ParkingIncluded    = false,
                LastRenovationDate = new DateTimeOffset(1979, 2, 18, 0, 0, 0, TimeSpan.Zero),
                Rating             = 3.60,
                Location           = GeographyPoint.Create(27.384417, -82.452843),
                Address            = new Address()
                {
                    StreetAddress = "140 University Town Center Dr",
                    City          = "Sarasota",
                    StateProvince = "FL",
                    PostalCode    = "34243",
                    Country       = "USA"
                },
                Rooms = new Room[]
                {
                    new Room()
                    {
                        Description    = "Suite, 2 Double Beds (Mountain View)",
                        DescriptionFr  = "Suite, 2 lits doubles (vue sur la montagne)",
                        Type           = "Suite",
                        BaseRate       = 250.99,
                        BedOptions     = "2 Double Beds",
                        SleepsCount    = 2,
                        SmokingAllowed = false,
                        Tags           = new[] { "Room Tags" }
                    },
                    new Room()
                    {
                        Description    = "Standard Room, 1 Queen Bed (City View)",
                        DescriptionFr  = "Chambre Standard, 1 grand lit (vue ville)",
                        Type           = "Standard Room",
                        BaseRate       = 121.99,
                        BedOptions     = "1 Queen Bed",
                        SleepsCount    = 2,
                        SmokingAllowed = false,
                        Tags           = new[] { "jacuzzi tub" }
                    },
                    new Room()
                    {
                        Description    = "Budget Room, 1 King Bed (Waterfront View)",
                        DescriptionFr  = "Chambre Économique, 1 très grand lit (vue sur le front de mer)",
                        Type           = "Budget Room",
                        BaseRate       = 88.99,
                        BedOptions     = "1 King Bed",
                        SleepsCount    = 2,
                        SmokingAllowed = false,
                        Tags           = new[] { "suite", "tv", "jacuzzi tub" }
                    }
                }
            }),
                IndexDocumentsAction.Upload(
                    new Hotel()
            {
                HotelId            = "3",
                HotelName          = "Triple Landscape Hotel",
                Description        = "The Hotel stands out for its gastronomic excellence under the management of William Dough, who advises on and oversees all of the Hotel’s restaurant services.",
                DescriptionFr      = "L'hôtel est situé dans une place du XIXe siècle, qui a été agrandie et rénovée aux plus hautes normes architecturales pour créer un hôtel moderne, fonctionnel et de première classe dans lequel l'art et les éléments historiques uniques coexistent avec le confort le plus moderne.",
                Category           = "Resort and Spa",
                Tags               = new[] { "air conditioning", "bar", "continental breakfast" },
                ParkingIncluded    = true,
                LastRenovationDate = new DateTimeOffset(2015, 9, 20, 0, 0, 0, TimeSpan.Zero),
                Rating             = 4.80,
                Location           = GeographyPoint.Create(33.84643, -84.362465),
                Address            = new Address()
                {
                    StreetAddress = "3393 Peachtree Rd",
                    City          = "Atlanta",
                    StateProvince = "GA",
                    PostalCode    = "30326",
                    Country       = "USA"
                },
                Rooms = new Room[]
                {
                    new Room()
                    {
                        Description    = "Standard Room, 2 Queen Beds (Amenities)",
                        DescriptionFr  = "Chambre Standard, 2 grands lits (Services)",
                        Type           = "Standard Room",
                        BaseRate       = 101.99,
                        BedOptions     = "2 Queen Beds",
                        SleepsCount    = 4,
                        SmokingAllowed = true,
                        Tags           = new[] { "vcr/dvd", "vcr/dvd" }
                    },
                    new Room()
                    {
                        Description    = "Standard Room, 2 Double Beds (Waterfront View)",
                        DescriptionFr  = "Chambre Standard, 2 lits doubles (vue sur le front de mer)",
                        Type           = "Standard Room",
                        BaseRate       = 106.99,
                        BedOptions     = "2 Double Beds",
                        SleepsCount    = 2,
                        SmokingAllowed = true,
                        Tags           = new[] { "coffee maker" }
                    },
                    new Room()
                    {
                        Description    = "Deluxe Room, 2 Double Beds (Cityside)",
                        DescriptionFr  = "Chambre Deluxe, 2 lits doubles (Cityside)",
                        Type           = "Budget Room",
                        BaseRate       = 180.99,
                        BedOptions     = "2 Double Beds",
                        SleepsCount    = 2,
                        SmokingAllowed = true,
                        Tags           = new[] { "suite" }
                    }
                }
            }));

            try
            {
                IndexDocumentsResult result = searchClient.IndexDocuments(batch);
            }
            catch (Exception)
            {
                // 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}");
            }

            Console.WriteLine("Waiting for documents to be indexed...\n");
            Thread.Sleep(2000);
        }
Beispiel #21
0
        static void Main(string[] args)
        {
            string serviceName = "lynx-searchengine-jsancho";
            string indexName   = "hotels-quickstart-v11";
            string apiKey      = "6C88A8613367F73135756A4CCFE1C24C";

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

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

            // Delete index if it exists
            Console.WriteLine("{0}", "Deleting index...\n");
            DeleteIndexIfExists(indexName, idxclient);

            var searchClientOptions = new SearchClientOptions
            {
                Serializer = new JsonObjectSerializer(
                    new JsonSerializerOptions
                {
                    PropertyNamingPolicy = JsonNamingPolicy.CamelCase
                })
            };

            var builder = new FieldBuilder
            {
                Serializer = searchClientOptions.Serializer
            };

            var index = new SearchIndex(indexName)
            {
                Fields = builder.Build(typeof(Hotel))
            };

            Console.WriteLine("{0}", "Creating index...\n");
            idxclient.CreateIndex(index);

            // Load documents (using a subset of fields for brevity)
            IndexDocumentsBatch <Hotel> batch = IndexDocumentsBatch.Create(
                IndexDocumentsAction.Upload(new Hotel {
                Id = "78", Name = "Upload Inn", Category = "hotel", Rate = 279, Updated = new DateTime(2018, 3, 1, 7, 0, 0)
            }),
                IndexDocumentsAction.Upload(new Hotel {
                Id = "54", Name = "Breakpoint by the Sea", Category = "motel", Rate = 162, Updated = new DateTime(2015, 9, 12, 7, 0, 0)
            }),
                IndexDocumentsAction.Upload(new Hotel {
                Id = "39", Name = "Debug Motel", Category = "motel", Rate = 159, Updated = new DateTime(2016, 11, 11, 7, 0, 0)
            }),
                IndexDocumentsAction.Upload(new Hotel {
                Id = "48", Name = "NuGet Hotel", Category = "hotel", Rate = 238, Updated = new DateTime(2016, 5, 30, 7, 0, 0)
            }),
                IndexDocumentsAction.Upload(new Hotel {
                Id = "12", Name = "Renovated Ranch", Category = "motel", Rate = 149, Updated = new DateTime(2020, 1, 24, 7, 0, 0)
            }));

            IndexDocumentsOptions idxoptions = new IndexDocumentsOptions {
                ThrowOnAnyError = true
            };

            Console.WriteLine("{0}", "Loading index...\n");
            srchclient.IndexDocuments(batch, idxoptions);

            // Wait 2 secondsfor indexing to complete before starting queries (for demo and console-app purposes only)
            Console.WriteLine("Waiting for indexing...\n");
            System.Threading.Thread.Sleep(2000);

            // Call the RunQueries method to invoke a series of queries
            Console.WriteLine("Starting queries...\n");
            RunQueries(srchclient);

            // End the program
            Console.WriteLine("{0}", "Complete. Press any key to end this program...\n");
            Console.ReadKey();
        }
Beispiel #22
0
        public Task IndexAccommodationAsync(Accommodation accommodation)
        {
            var action = IndexDocumentsAction.MergeOrUpload(accommodation);

            return(client.IndexDocumentsAsync(IndexDocumentsBatch.Create(new[] { action })));
        }
Beispiel #23
0
        private static void UploadDocuments(SearchClient searchClient)
        {
            IndexDocumentsBatch <Hotel> batch = IndexDocumentsBatch.Create(
                IndexDocumentsAction.Upload(
                    new Hotel()
            {
                HotelId            = "1",
                HotelName          = "Secret Point Motel",
                Description        = "The hotel is ideally located on the main commercial artery of the city in the heart of New York. A few minutes away is Time's Square and the historic centre of the city, as well as other places of interest that make New York one of America's most attractive and cosmopolitan cities.",
                DescriptionFr      = "L'hôtel est idéalement situé sur la principale artère commerciale de la ville en plein cœur de New York. A quelques minutes se trouve la place du temps et le centre historique de la ville, ainsi que d'autres lieux d'intérêt qui font de New York l'une des villes les plus attractives et cosmopolites de l'Amérique.",
                Category           = "Boutique",
                Tags               = new[] { "pool", "air conditioning", "concierge" },
                ParkingIncluded    = false,
                LastRenovationDate = new DateTimeOffset(1970, 1, 18, 0, 0, 0, TimeSpan.Zero),
                Rating             = 3.6,
                //Address = new Address()
                //{
                //    StreetAddress = "677 5th Ave",
                //    City = "New York",
                //    StateProvince = "NY",
                //    PostalCode = "10022",
                //    Country = "USA"
                //}
            }),
                IndexDocumentsAction.Upload(
                    new Hotel()
            {
                HotelId            = "2",
                HotelName          = "Twin Dome Motel",
                Description        = "The hotel is situated in a  nineteenth century plaza, which has been expanded and renovated to the highest architectural standards to create a modern, functional and first-class hotel in which art and unique historical elements coexist with the most modern comforts.",
                DescriptionFr      = "L'hôtel est situé dans une place du XIXe siècle, qui a été agrandie et rénovée aux plus hautes normes architecturales pour créer un hôtel moderne, fonctionnel et de première classe dans lequel l'art et les éléments historiques uniques coexistent avec le confort le plus moderne.",
                Category           = "Boutique",
                Tags               = new[] { "pool", "free wifi", "concierge" },
                ParkingIncluded    = false,
                LastRenovationDate = new DateTimeOffset(1979, 2, 18, 0, 0, 0, TimeSpan.Zero),
                Rating             = 3.60,
                //Address = new Address()
                //{
                //    StreetAddress = "140 University Town Center Dr",
                //    City = "Sarasota",
                //    StateProvince = "FL",
                //    PostalCode = "34243",
                //    Country = "USA"
                //}
            }),
                IndexDocumentsAction.Upload(
                    new Hotel()
            {
                HotelId            = "3",
                HotelName          = "Triple Landscape Hotel",
                Description        = "The Hotel stands out for its gastronomic excellence under the management of William Dough, who advises on and oversees all of the Hotel’s restaurant services.",
                DescriptionFr      = "L'hôtel est situé dans une place du XIXe siècle, qui a été agrandie et rénovée aux plus hautes normes architecturales pour créer un hôtel moderne, fonctionnel et de première classe dans lequel l'art et les éléments historiques uniques coexistent avec le confort le plus moderne.",
                Category           = "Resort and Spa",
                Tags               = new[] { "air conditioning", "bar", "continental breakfast" },
                ParkingIncluded    = true,
                LastRenovationDate = new DateTimeOffset(2015, 9, 20, 0, 0, 0, TimeSpan.Zero),
                Rating             = 4.80,
                //Address = new Address()
                //{
                //    StreetAddress = "3393 Peachtree Rd",
                //    City = "Atlanta",
                //    StateProvince = "GA",
                //    PostalCode = "30326",
                //    Country = "USA"
                //}
            }),
                IndexDocumentsAction.Upload(
                    new Hotel()
            {
                HotelId            = "4",
                HotelName          = "Sublime Cliff Hotel",
                Description        = "Sublime Cliff Hotel is located in the heart of the historic center of Sublime in an extremely vibrant and lively area within short walking distance to the sites and landmarks of the city and is surrounded by the extraordinary beauty of churches, buildings, shops and monuments. Sublime Cliff is part of a lovingly restored 1800 palace.",
                DescriptionFr      = "Le sublime Cliff Hotel est situé au coeur du centre historique de sublime dans un quartier extrêmement animé et vivant, à courte distance de marche des sites et monuments de la ville et est entouré par l'extraordinaire beauté des églises, des bâtiments, des commerces et Monuments. Sublime Cliff fait partie d'un Palace 1800 restauré avec amour.",
                Category           = "Boutique",
                Tags               = new[] { "concierge", "view", "24-hour front desk service" },
                ParkingIncluded    = true,
                LastRenovationDate = new DateTimeOffset(1960, 2, 06, 0, 0, 0, TimeSpan.Zero),
                Rating             = 4.60,
                //Address = new Address()
                //{
                //    StreetAddress = "7400 San Pedro Ave",
                //    City = "San Antonio",
                //    StateProvince = "TX",
                //    PostalCode = "78216",
                //    Country = "USA"
                //}
            })
                );

            try
            {
                IndexDocumentsResult result = searchClient.IndexDocuments(batch);
            }
            catch (Exception e)
            {
                // If for some reason any documents are dropped during indexing, you can compensate by delaying and
                // retrying. This simple demo just logs the failed document keys and continues.
                Console.WriteLine($"Failed to index some of the documents: {e}");
            }
        }
Beispiel #24
0
        static void Main(string[] args)
        {
            //Setting up db context
            var optionsBuilder = new DbContextOptionsBuilder <BankAppDataContext>();
            var context        = new BankAppDataContext(optionsBuilder.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")).Options);

            string serviceName = Environment.GetEnvironmentVariable("SearchService");
            string indexName   = "customersearch";
            string apiKey      = Environment.GetEnvironmentVariable("SearchKey");

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

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


            // Define an index schema using SearchIndex
            // Create the index using SearchIndexClient
            SearchIndex index = new SearchIndex(indexName)
            {
                Fields =
                {
                    new SimpleField("customerId",    SearchFieldDataType.String)
                    {
                        IsKey = true,                IsSortable= true
                    },
                    new SearchableField("givenName")
                    {
                        IsSortable = true,           IsHidden = true
                    },
                    new SearchableField("surname")
                    {
                        IsSortable = true,           IsHidden = true
                    },
                    new SearchableField("city")
                    {
                        IsSortable = true,           IsHidden = true
                    },
                    new SimpleField("streetAddress", SearchFieldDataType.String)
                    {
                        IsSortable = true,           IsHidden = true
                    },
                    new SimpleField("nationalId",    SearchFieldDataType.String)
                    {
                        IsSortable = true,           IsHidden = true
                    }
                }
            };

            idxclient.CreateIndex(index);

            //Fyller index med data
            var actions = new List <IndexDocumentsAction <CustomerIndex> >();

            foreach (var customers in context.Customers)
            {
                actions.Add(IndexDocumentsAction.Upload(new CustomerIndex
                {
                    CustomerId    = customers.CustomerId.ToString(),
                    City          = customers.City,
                    Givenname     = customers.Givenname,
                    NationalId    = customers.NationalId,
                    Streetaddress = customers.Streetaddress,
                    Surname       = customers.Surname
                }));
            }
            ;

            IndexDocumentsBatch <CustomerIndex> batch = IndexDocumentsBatch.Create(actions.ToArray());

            IndexDocumentsOptions idxoptions = new IndexDocumentsOptions {
                ThrowOnAnyError = true
            };

            qryclient.IndexDocuments(batch, idxoptions);
        }