Exemple #1
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
            }
        }
Exemple #2
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}");
            }
        }
Exemple #4
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));
            }
        }
        static void UploadDocuments(SearchClient srchclient, int FileCount)
        {
            var docCounter = 0;
            var batchJobs  = new List <IndexDocumentsBatch <SemanticScholar> >();
            var batch      = new IndexDocumentsBatch <SemanticScholar>();

            Console.WriteLine("Creating batches for upload...");
            for (var fileNum = 0; fileNum < FileCount; fileNum++)
            {
                var         paddedFileNum = fileNum.ToString().PadLeft(3, '0');
                var         baseFileName  = "s2-corpus-" + paddedFileNum + ".gz";
                var         fileToProcess = Path.Combine(DownloadDir, baseFileName).Replace(".gz", "");
                const Int32 BufferSize    = 128;
                using (var fileStream = File.OpenRead(fileToProcess))
                    using (var streamReader = new StreamReader(fileStream, Encoding.UTF8, true, BufferSize))
                    {
                        String line;

                        while ((line = streamReader.ReadLine()) != null)
                        {
                            docCounter += 1;
                            if (docCounter == DocumentsToUpload)
                            {
                                break;
                            }
                            var ssDoc = JsonConvert.DeserializeObject <SemanticScholar>(line);
                            batch.Actions.Add(IndexDocumentsAction.Upload(ssDoc));
                            if (docCounter % MaxBatchSize == 0)
                            {
                                batchJobs.Add(batch);
                                batch = new IndexDocumentsBatch <SemanticScholar>();
                                if (batchJobs.Count % 100 == 0)
                                {
                                    Console.WriteLine("Created {0} batches...", batchJobs.Count);
                                }
                            }
                        }
                    }

                ParallelBatchApplication(batchJobs, srchclient);

                batchJobs.Clear();
                batch = new IndexDocumentsBatch <SemanticScholar>();

                if (docCounter == DocumentsToUpload)
                {
                    break;
                }
            }

            if (batch.Actions.Count > 0)
            {
                batchJobs.Add(batch);
            }

            ParallelBatchApplication(batchJobs, srchclient);
        }
        public void DeleteCustomerData(CustomerIndex customerToDelete)
        {
            IndexDocumentsBatch <CustomerIndex> batch = IndexDocumentsBatch.Create(IndexDocumentsAction.Delete(customerToDelete));
            IndexDocumentsOptions idxoptions          = new IndexDocumentsOptions {
                ThrowOnAnyError = true
            };

            _qryClient.IndexDocuments(batch, idxoptions);
        }
Exemple #7
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);
            }
        }
Exemple #8
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));
        }
        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 });
        }
Exemple #10
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);
        }
        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);
        }
Exemple #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);
        }
Exemple #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);
        }
        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}");
            }
        }
Exemple #15
0
        public Task IndexAccommodationAsync(Accommodation accommodation)
        {
            var action = IndexDocumentsAction.MergeOrUpload(accommodation);

            return(client.IndexDocumentsAsync(IndexDocumentsBatch.Create(new[] { action })));
        }
        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;
            }
        }
Exemple #17
0
 private static void DeleteEntry(DeleteIndexEntry delete, IList <IndexDocumentsAction <SearchDocument> > batch)
 {
     batch.Add(IndexDocumentsAction.Delete("docId", delete.DocId.ToBase64()));
 }
Exemple #18
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();
        }
Exemple #19
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();
        }
        // 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);
        }
        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}");
            }
        }
Exemple #22
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);
        }
Exemple #23
0
 /// <summary>
 /// Get a key used to identify a given document.
 /// </summary>
 /// <param name="document">The document.</param>
 /// <returns>A key for that document.</returns>
 protected override string GetDocumentKey(IndexDocumentsAction <T> document) =>
 _sender.KeyFieldAccessor(document.Document);
        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();
        }
Exemple #25
0
        private async Task <Response <IndexDocumentsResult> > IndexProduct(SearchClient searchClient, ProductDocument product)
        {
            var batch = IndexDocumentsBatch.Create(IndexDocumentsAction.Upload(product));

            return(await searchClient.IndexDocumentsAsync(batch));
        }
Exemple #26
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);
        }