Exemplo n.º 1
0
        private static void UpsertTextEntry(UpsertIndexEntry upsert, IList <IndexDocumentsAction <SearchDocument> > batch)
        {
            var geoField  = string.Empty;
            var geoObject = (object)null;

            if (upsert.GeoObjects != null)
            {
                foreach (var(key, value) in upsert.GeoObjects)
                {
                    if (value is Point point)
                    {
                        geoField  = key;
                        geoObject = new
                        {
                            type        = "Point",
                            coordinates = new[]
                            {
                                point.Coordinates.Longitude,
                                point.Coordinates.Latitude
                            }
                        };
                        break;
                    }
                }
            }

            if (upsert.Texts != null || geoObject != null)
            {
                var document = new SearchDocument
                {
                    ["docId"]          = upsert.DocId.ToBase64(),
                    ["appId"]          = upsert.AppId.Id.ToString(),
                    ["appName"]        = upsert.AppId.Name,
                    ["contentId"]      = upsert.ContentId.ToString(),
                    ["schemaId"]       = upsert.SchemaId.Id.ToString(),
                    ["schemaName"]     = upsert.SchemaId.Name,
                    ["serveAll"]       = upsert.ServeAll,
                    ["servePublished"] = upsert.ServePublished,
                    ["geoField"]       = geoField,
                    ["geoObject"]      = geoObject
                };

                foreach (var(key, value) in upsert.Texts)
                {
                    var text = value;

                    var languageCode = AzureIndexDefinition.GetFieldName(key);

                    if (document.TryGetValue(languageCode, out var existing))
                    {
                        text = $"{existing} {value}";
                    }

                    document[languageCode] = text;
                }

                batch.Add(IndexDocumentsAction.MergeOrUpload(document));
            }
        }
Exemplo n.º 2
0
        private static void UpdateEntry(UpdateIndexEntry update, IList <IndexDocumentsAction <SearchDocument> > batch)
        {
            var document = new SearchDocument
            {
                ["docId"]          = update.DocId.ToBase64(),
                ["serveAll"]       = update.ServeAll,
                ["servePublished"] = update.ServePublished,
            };

            batch.Add(IndexDocumentsAction.MergeOrUpload(document));
        }
Exemplo n.º 3
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);
        }
Exemplo n.º 4
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}");
            }
        }
Exemplo n.º 5
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();
        }
Exemplo n.º 6
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;
            }
        }
Exemplo n.º 7
0
        public Task IndexAccommodationAsync(Accommodation accommodation)
        {
            var action = IndexDocumentsAction.MergeOrUpload(accommodation);

            return(client.IndexDocumentsAsync(IndexDocumentsBatch.Create(new[] { action })));
        }