Example #1
0
        private static void UploadDocuments(ISearchIndexClient indexClient)
        {
            var actions = new List <IndexAction <Team> >();

            foreach (var team in Teams.GetTeams())
            {
                Console.WriteLine($"Uploading team {team.TeamName}");
                actions.Add(IndexAction.Upload(team));
            }

            var batch = IndexBatch.New(actions);

            try
            {
                indexClient.Documents.Index(batch);
            }
            catch (IndexBatchException e)
            {
                Console.WriteLine(
                    "Failed to index some of the documents: {0}",
                    String.Join(", ", e.IndexingResults.Where(r => !r.Succeeded).Select(r => r.Key)));
            }

            Console.WriteLine("Waiting for indexing...\n");
            Thread.Sleep(2000);
        }
Example #2
0
        private static void UploadData()
        {
            var actions = new IndexAction <Customer>[]
            {
                IndexAction.Upload(new Customer()
                {
                    customerid = "1", customername = "John", customeremail = "*****@*****.**"
                }),
                IndexAction.Upload(new Customer()
                {
                    customerid = "2", customername = "Mark", customeremail = "*****@*****.**"
                })
            };

            var batch = IndexBatch.New(actions);

            SearchServiceClient serviceClient = new SearchServiceClient(searchServiceName, new SearchCredentials(adminApiKey));

            ISearchIndexClient indexClient = serviceClient.Indexes.GetClient("customer");

            indexClient.Documents.Index(batch);

            Console.WriteLine("Added Data");

            Console.ReadKey();
        }
Example #3
0
        public void AddToIndex <T>(IEnumerable <T> documents)
        {
            var actions = documents.Select(a => IndexAction.Upload(a));
            var batch   = IndexBatch.New(actions);

            searchIndexClient.Documents.Index(batch);
        }
Example #4
0
        private static void ImportDocument(ISearchIndexClient indexClient)
        {
            var actions = new List <IndexAction <Account> >();

            string line = string.Empty;

            using (var file = new StreamReader("accounts.json"))
            {
                while ((line = file.ReadLine()) != null)
                {
                    JObject json    = JObject.Parse(line);
                    Account account = json.ToObject <Account>();

                    actions.Add(IndexAction.Upload(account));
                }

                file.Close();
            }

            var batch = IndexBatch.New(actions);

            try
            {
                indexClient.Documents.Index(batch);
            }
            catch (Exception)
            {
                throw;
            }
        }
Example #5
0
        public static void UploadDocuments()
        {
            // This will open the JSON file, parse it and upload the documents in a batch
            List <IndexAction> indexOperations = new List <IndexAction>();
            JArray             json            = JArray.Parse(File.ReadAllText(@"contacts.json"));

            foreach (var contact in json)
            {
                //Parse the JSON object (contact)
                var doc = new Document();
                doc.Add("id", contact["id"]);
                doc.Add("name", contact["name"]);
                doc.Add("company", contact["company"]);
                doc.Add("locationsId", contact["locations"].Select(item => item["id"]).ToList());
                doc.Add("locationsDescription", contact["locations"].Select(item => item["description"]).ToList());
                doc.Add("locationsCombined", contact["locations"].Select(item => item["id"] + "||" + item["description"]).ToList());

                indexOperations.Add(IndexAction.Upload(doc));
            }

            try
            {
                indexClient.Documents.Index(new IndexBatch(indexOperations));
            }
            catch (IndexBatchException e)
            {
                // 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}",
                    String.Join(", ", e.IndexingResults.Where(r => !r.Succeeded).Select(r => r.Key)));
            }
        }
Example #6
0
        public static void UploadDocuments(ISearchIndexClient indexClient, string fileId, string summary, List <string> keyPhrases)
        {
            // This is really inefficient as I should be batching the uploads
            List <IndexAction> indexOperations = new List <IndexAction>();
            var doc = new Document();

            doc.Add("fileId", fileId);
            doc.Add("summary", summary);
            doc.Add("keyPhrases", keyPhrases);
            indexOperations.Add(IndexAction.Upload(doc));

            try
            {
                indexClient.Documents.Index(new IndexBatch(indexOperations));
            }
            catch (IndexBatchException e)
            {
                // 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}",
                    String.Join(", ", e.IndexingResults.Where(r => !r.Succeeded).Select(r => r.Key)));
            }
        }
        private static void ImportData(SearchServiceClient serviceClient)
        {
            List <RentingStats> rentingStats = new List <RentingStats>();

            using (var streamReader = new StreamReader(dataFileName))
                using (var csvReader = new CsvReader(streamReader))
                {
                    rentingStats = csvReader.GetRecords <RentingStats>().ToList();
                }

            var actions = new List <IndexAction <RentingStats> >();

            foreach (var rentingStat in rentingStats)
            {
                actions.Add(IndexAction.Upload(rentingStat));
            }
            var batch = IndexBatch.New(actions);

            try
            {
                ISearchIndexClient indexClient = serviceClient.Indexes.GetClient(indexName);
                indexClient.Documents.Index(batch);
            }
            catch (Exception e)
            {
                Console.WriteLine(e.ToString());
            }
        }
Example #8
0
        public ActionResult Create(Player player)
        {
            if (string.IsNullOrEmpty(player.Name))
            {
                log.Error("Player name shouldn't be empty");
                throw new ArgumentException("Player name shouldn't be empty");
            }
            player.Id = Guid.NewGuid();
            db.Players.Add(player);
            db.SaveChanges();

            var actions = new IndexAction <Player>[]
            {
                IndexAction.Upload(player)
            };
            var batch = IndexBatch.New(actions);

            try
            {
                _soccerIndex.Documents.Index(batch);
            }
            catch (IndexBatchException e)
            {
                // 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}",
                    String.Join(", ", e.IndexingResults.Where(r => !r.Succeeded).Select(r => r.Key)));
            }

            return(RedirectToAction("Index"));
        }
Example #9
0
        private void ImportData(ISearchIndexClient indexClient)
        {
            var actions = new List <IndexAction <Account> >();

            string line;

            using (System.IO.StreamReader file = new System.IO.StreamReader("accounts.json"))
            {
                while ((line = file.ReadLine()) != null)
                {
                    JObject json    = JObject.Parse(line);
                    Account account = json.ToObject <Account>();
                    actions.Add(IndexAction.Upload(account));
                }
                file.Close();
            }
            var batch = IndexBatch.New(actions);

            try
            {
                indexClient.Documents.Index(batch);
            }
            catch (IndexBatchException ex)
            {
                _logger.Error(ex, ex.Message);
                throw ex;
            }
        }
Example #10
0
        public static async Task Run([BlobTrigger("properties/{name}", Connection = "StorageConnection")] Stream myBlob, string name, TraceWriter log)
        {
            var searchServiceName = ConfigurationManager.AppSettings["SearchServiceName"];
            var adminApiKey       = ConfigurationManager.AppSettings["SearchServiceAdminApiKey"];
            var serviceClient     = new SearchServiceClient(searchServiceName, new SearchCredentials(adminApiKey));

            var blogPost = GetBlogPost(myBlob);

            blogPost.filePath = $"{ConfigurationManager.AppSettings["FilePathBeginning"]}{name}";

            var action = new[]
            {
                IndexAction.Upload(blogPost)
            };

            var batch       = IndexBatch.New(action);
            var indexClient = serviceClient.Indexes.GetClient(ConfigurationManager.AppSettings["SearchIndexName"]);

            try
            {
                await indexClient.Documents.IndexAsync(batch);
            }
            catch (IndexBatchException e)
            {
                log.Error("Error: ", e);
                throw;
            }
        }
Example #11
0
        public static AzureSearchIndexResult IndexContentBatch(this SearchServiceClient serviceClient, string indexName, IEnumerable <Document> contents)
        {
            var result  = new AzureSearchIndexResult();
            var actions = new List <IndexAction>();

            foreach (var content in contents)
            {
                actions.Add(IndexAction.Upload(content));
            }

            var batch       = IndexBatch.New(actions);
            var indexClient = serviceClient.Indexes.GetClient(indexName);

            try
            {
                indexClient.Documents.Index(batch);
            }
            catch (IndexBatchException e)
            {
                // 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.
                var error =
                    "Failed to index some of the documents: {0}" + String.Join(", ", e.IndexingResults.Where(r => !r.Succeeded).Select(r => r.Key));

                result.Success = false;
                result.Message = error;

                return(result);
            }

            result.Success = true;
            return(result);
        }
Example #12
0
        public async Task IndexAsync(params string[] packageIds)
        {
            if (packageIds == null)
            {
                throw new ArgumentNullException(nameof(packageIds));
            }

            var actions      = new List <IndexAction <PackageDocument> >();
            var packageIdSet = new HashSet <string>(packageIds, StringComparer.OrdinalIgnoreCase);

            if (packageIdSet.Count > MaxBatchSize)
            {
                throw new ArgumentException($"Cannot index more than {MaxBatchSize} packages at once");
            }

            _logger.LogInformation("Indexing {PackageCount} packages...", packageIdSet.Count);

            foreach (var packageId in packageIdSet)
            {
                foreach (var document in await BuildDocumentsAsync(packageId))
                {
                    actions.Add(IndexAction.Upload(document));
                }
            }

            var batch = IndexBatch.New(actions);

            // TODO: Add retry on IndexBatchException
            // See: https://docs.microsoft.com/en-us/azure/search/search-import-data-dotnet#import-data-to-the-index
            await _indexClient.Documents.IndexAsync(batch);

            _logger.LogInformation("Indexed {PackageCount} packages", packageIdSet.Count);
        }
Example #13
0
        private async Task ImportDataAsync()
        {
            var data = await File.ReadAllLinesAsync(options.HotelFileName).ConfigureAwait(false);

            var hotels = data.Skip(1).Select(r =>
            {
                var columns = r.Split("\t");
                return(new Hotel
                {
                    HotelId = columns[0],
                    HotelName = columns[1],
                    Description = columns[2],
                    DescriptionFr = columns[3],
                    Category = columns[4],
                    Tags = columns[5].Split(",").ToImmutableArray(),
                    ParkingIncluded = columns[6] == "0" ? false : true,
                    SmokingAllowed = columns[7] == "0" ? false : true,
                    LastRenovationDate = Convert.ToDateTime(columns[8], CultureInfo.InvariantCulture),
                    BaseRate = Convert.ToDouble(columns[9], CultureInfo.InvariantCulture),
                    Rating = Convert.ToInt32(Convert.ToDouble(columns[10], CultureInfo.InvariantCulture))
                });
            }).ToList();

            var actions = hotels.Select(h => IndexAction.Upload(h)).ToList();

            var batch = IndexBatch.New(actions);

            using (var client = CreateSearchServiceClient())
            {
                await client.Indexes.GetClient(options.IndexName).Documents.IndexAsync(batch).ConfigureAwait(false);
            }

            logger.LogInformation("Imported {@Count} hotels", hotels.Count);
        }
Example #14
0
 private static async Task WriteToSiAsync(IEnumerable <Book> listBook)
 {
     SearchServiceClient serviceClient = new SearchServiceClient("librarysearchservice", new SearchCredentials("<api-key>"));
     var indexClient = serviceClient.Indexes.GetClient("booksindex");
     var act         = listBook.Select(x => IndexAction.Upload <Book>(x));
     await indexClient.Documents.IndexAsync(IndexBatch.New(act));
 }
Example #15
0
        private void ImportDocuments(ISearchIndexClient indexClient)
        {
            var actions = new IndexAction <RealState>[]
            {
                IndexAction.Upload(
                    new RealState()
                {
                    ListingId   = "1",
                    Name        = "Madrid",
                    Beds        = 2,
                    Baths       = "1",
                    Description = "Meilleur hôtel en ville",
                    Status      = "Available",
                    Type        = "House",
                    City        = "Bern",
                    Price       = 1200
                }),
                IndexAction.Upload(
                    new RealState()
                {
                    ListingId   = "2",
                    Name        = "Real",
                    Beds        = 3,
                    Baths       = "2",
                    Description = "Hôtel le moins cher en ville",
                    Status      = "Hired",
                    Type        = "Room",
                    City        = "Madrid",
                    Price       = 1600
                }),
                IndexAction.MergeOrUpload(
                    new RealState()
                {
                    ListingId   = "3",
                    Name        = "Empire",
                    Beds        = 3,
                    Baths       = "2",
                    Description = "Mansion Le Blank",
                    Status      = "Sold",
                    Type        = "Castle",
                    City        = "London",
                    Price       = 1000
                }),
                //IndexAction.Delete(new RealState() { ListingId = "6" })
            };
            var batch = IndexBatch.New(actions);

            try
            {
                indexClient.Documents.Index(batch);
            }
            catch (IndexBatchException e)
            {
                Console.WriteLine("Failed to index some of the documents: {0}", String.Join(", ", e.IndexingResults.Where(r => !r.Succeeded).Select(r => r.Key)));
            }
            Console.WriteLine("Waiting for documents to be indexed...\n");
            Thread.Sleep(2000);
        }
Example #16
0
        private static void UploadDoc(Cliente data, ISearchIndexClient indexClient)
        {
            //Criando o documento a ser indexado
            var actions = new IndexAction <Cliente>[] { IndexAction.Upload(data) };
            var batch   = IndexBatch.New(actions);

            //Atualizado o indice com o novo documento
            indexClient.Documents.Index(batch);
        }
 public async Task CreateAsync <T>(T item, IGraphRequestContext graphRequestContext) where T : class
 {
     var client = Get <T>(graphRequestContext);
     var idx    = IndexAction.Upload(item);
     await client.Documents.IndexAsync(new IndexBatch <T>(
                                           new List <IndexAction <T> > {
         idx
     }));
 }
Example #18
0
        public async Task DoesNotEnqueueChangesForNoIndexActions()
        {
            _config.AzureSearchBatchSize = 2;
            _producer
            .Setup(x => x.ProduceWorkAsync(It.IsAny <ConcurrentBag <NewPackageRegistration> >(), It.IsAny <CancellationToken>()))
            .ReturnsAsync(() => _initialAuxiliaryData)
            .Callback <ConcurrentBag <NewPackageRegistration>, CancellationToken>((w, _) =>
            {
                w.Add(new NewPackageRegistration("A", 0, new[] { "Microsoft", "EntityFramework" }, new Package[0], false));
                w.Add(new NewPackageRegistration("B", 0, new[] { "nuget" }, new Package[0], false));
                w.Add(new NewPackageRegistration("C", 0, new[] { "aspnet" }, new Package[0], false));
            });

            // Return empty index action for ID "B". This package ID will not be pushed to Azure Search but will appear
            // in the initial owners data file.
            _builder
            .Setup(x => x.AddNewPackageRegistration(It.Is <NewPackageRegistration>(y => y.PackageId != "B")))
            .Returns <NewPackageRegistration>(x => new IndexActions(
                                                  new List <IndexAction <KeyedDocument> > {
                IndexAction.Upload(new KeyedDocument {
                    Key = x.PackageId
                })
            },
                                                  new List <IndexAction <KeyedDocument> >(),
                                                  new ResultAndAccessCondition <VersionListData>(
                                                      new VersionListData(new Dictionary <string, VersionPropertiesData>()),
                                                      AccessConditionWrapper.GenerateEmptyCondition())));
            _builder
            .Setup(x => x.AddNewPackageRegistration(It.Is <NewPackageRegistration>(y => y.PackageId == "B")))
            .Returns <NewPackageRegistration>(x => new IndexActions(
                                                  new List <IndexAction <KeyedDocument> >(),
                                                  new List <IndexAction <KeyedDocument> >(),
                                                  new ResultAndAccessCondition <VersionListData>(
                                                      new VersionListData(new Dictionary <string, VersionPropertiesData>()),
                                                      AccessConditionWrapper.GenerateEmptyCondition())));

            var enqueuedIndexActions = new List <KeyValuePair <string, IndexActions> >();

            _batchPusher
            .Setup(x => x.EnqueueIndexActions(It.IsAny <string>(), It.IsAny <IndexActions>()))
            .Callback <string, IndexActions>((id, actions) =>
            {
                enqueuedIndexActions.Add(KeyValuePair.Create(id, actions));
            });

            await _target.ExecuteAsync();

            Assert.Equal(2, enqueuedIndexActions.Count);
            var keys = enqueuedIndexActions
                       .Select(x => x.Key)
                       .OrderBy(x => x)
                       .ToArray();

            Assert.Equal(
                new[] { "A", "C" },
                keys);
        }
        public async Task Add(SearchEntityModel document)
        {
            var batch = IndexBatch.New(new IndexAction <SearchEntityModel>[] {
                IndexAction.Upload(document)
            });



            await _indexClient.Documents.IndexAsync(batch);
        }
Example #20
0
        private static async Task LoadDocumentsAsync(
            ISearchIndexClient indexClient,
            Post[] posts)
        {
            var actions = from p in posts
                          select IndexAction.Upload(p);

            var batch  = IndexBatch.New(actions);
            var result = await indexClient.Documents.IndexAsync(batch);
        }
        public void DoIndex(ISearchService searchService, ISettingsManager settingsManager, IPostService postService,
                            IConfig config, ITopicService topicService, IErrorLog errorLog)
        {
            var topic = searchService.GetNextTopicForIndexing();

            if (topic != null)
            {
                var serviceClient = new SearchServiceClient(config.SearchUrl, new SearchCredentials(config.SearchKey));
                if (!serviceClient.Indexes.Exists(IndexName))
                {
                    CreateIndex(serviceClient);
                }

                var posts       = postService.GetPosts(topic, false).ToArray();
                var parsedPosts = posts.Select(x =>
                {
                    var parsedText = _textParsingService.ClientHtmlToForumCode(x.FullText);
                    parsedText     = _textParsingService.RemoveForumCode(parsedText);
                    return(parsedText);
                }).ToArray();
                var searchTopic = new SearchTopic
                {
                    TopicID       = topic.TopicID.ToString(),
                    ForumID       = topic.ForumID,
                    Title         = topic.Title,
                    LastPostTime  = topic.LastPostTime,
                    StartedByName = topic.StartedByName,
                    Replies       = topic.ReplyCount,
                    Views         = topic.ViewCount,
                    IsClosed      = topic.IsClosed,
                    IsPinned      = topic.IsPinned,
                    UrlName       = topic.UrlName,
                    LastPostName  = topic.LastPostName,
                    Posts         = parsedPosts
                };

                var actions =
                    new IndexAction <SearchTopic>[]
                {
                    IndexAction.Upload(searchTopic)
                };
                try
                {
                    var serviceIndexClient = serviceClient.Indexes.GetClient(IndexName);
                    var batch = IndexBatch.New(actions);
                    serviceIndexClient.Documents.Index(batch);
                    searchService.MarkTopicAsIndexed(topic);
                }
                catch (Exception exc)
                {
                    errorLog.Log(exc, ErrorSeverity.Error);
                    topicService.MarkTopicForIndexing(topic.TopicID);
                }
            }
        }
Example #22
0
        public void DoIndex(int topicID, string tenantID)
        {
            var topic = _topicService.Get(topicID);

            if (topic != null)
            {
                var serviceClient = new SearchServiceClient(_config.SearchUrl, new SearchCredentials(_config.SearchKey));
                if (!serviceClient.Indexes.Exists(IndexName))
                {
                    CreateIndex(serviceClient);
                }

                var posts       = _postService.GetPosts(topic, false).ToArray();
                var parsedPosts = posts.Select(x =>
                {
                    var parsedText = _textParsingService.ClientHtmlToForumCode(x.FullText);
                    parsedText     = _textParsingService.RemoveForumCode(parsedText);
                    return(parsedText);
                }).ToArray();
                var searchTopic = new SearchTopic
                {
                    TopicID       = topic.TopicID.ToString(),
                    ForumID       = topic.ForumID,
                    Title         = topic.Title,
                    LastPostTime  = topic.LastPostTime,
                    StartedByName = topic.StartedByName,
                    Replies       = topic.ReplyCount,
                    Views         = topic.ViewCount,
                    IsClosed      = topic.IsClosed,
                    IsPinned      = topic.IsPinned,
                    UrlName       = topic.UrlName,
                    LastPostName  = topic.LastPostName,
                    Posts         = parsedPosts,
                    TenantID      = tenantID
                };

                var actions =
                    new[]
                {
                    IndexAction.Upload(searchTopic)
                };
                try
                {
                    var serviceIndexClient = serviceClient.Indexes.GetClient(IndexName);
                    var batch = IndexBatch.New(actions);
                    serviceIndexClient.Documents.Index(batch);
                }
                catch (Exception exc)
                {
                    _errorLog.Log(exc, ErrorSeverity.Error);
                    _topicService.QueueTopicForIndexing(topic.TopicID);
                }
            }
        }
Example #23
0
        private static void UploadDoc(List <Cliente> data, ISearchIndexClient indexClient)
        {
            //Criando o documento a ser indexado
            var actions = new List <IndexAction <Cliente> >();

            data.ForEach(d => actions.Add(IndexAction.Upload(d)));
            var batch = IndexBatch.New(actions);

            //Atualizando o indice com o novo documento
            indexClient.Documents.Index(batch);
        }
Example #24
0
        private IndexBatch <T> UploadIndexBatch <T>(IEnumerable <T> data) where T : class
        {
            var indexAtions = new List <IndexAction <T> >();

            foreach (var item in data)
            {
                indexAtions.Add(IndexAction.Upload(item));
            }

            return(IndexBatch.New(indexAtions));
        }
Example #25
0
 private static IndexAction <IndexablePub> BuildIndexablePub(Pub pub)
 {
     try
     {
         return(IndexAction.Upload(new IndexablePub(pub)));
     }
     catch (Exception exception)
     {
         Console.Out.WriteLine($"Failed to convert: {pub.Name} - Error: {exception}");
         return(null);
     }
 }
Example #26
0
        public async Task PushesToIndexesUsingMaximumBatchSize()
        {
            _config.AzureSearchBatchSize = 2;
            _producer
            .Setup(x => x.ProduceWorkAsync(It.IsAny <ConcurrentBag <NewPackageRegistration> >(), It.IsAny <CancellationToken>()))
            .ReturnsAsync(() => _initialAuxiliaryData)
            .Callback <ConcurrentBag <NewPackageRegistration>, CancellationToken>((w, _) =>
            {
                w.Add(new NewPackageRegistration("A", 0, new string[0], new Package[0], false));
                w.Add(new NewPackageRegistration("B", 0, new string[0], new Package[0], false));
                w.Add(new NewPackageRegistration("C", 0, new string[0], new Package[0], false));
                w.Add(new NewPackageRegistration("D", 0, new string[0], new Package[0], false));
                w.Add(new NewPackageRegistration("E", 0, new string[0], new Package[0], false));
            });
            _builder
            .Setup(x => x.AddNewPackageRegistration(It.IsAny <NewPackageRegistration>()))
            .Returns <NewPackageRegistration>(x => new IndexActions(
                                                  new List <IndexAction <KeyedDocument> > {
                IndexAction.Upload(new KeyedDocument {
                    Key = x.PackageId
                })
            },
                                                  new List <IndexAction <KeyedDocument> >(),
                                                  new ResultAndAccessCondition <VersionListData>(
                                                      new VersionListData(new Dictionary <string, VersionPropertiesData>()),
                                                      AccessConditionWrapper.GenerateEmptyCondition())));

            var enqueuedIndexActions = new List <KeyValuePair <string, IndexActions> >();

            _batchPusher
            .Setup(x => x.EnqueueIndexActions(It.IsAny <string>(), It.IsAny <IndexActions>()))
            .Callback <string, IndexActions>((id, actions) =>
            {
                enqueuedIndexActions.Add(KeyValuePair.Create(id, actions));
            });

            await _target.ExecuteAsync();

            Assert.Equal(5, enqueuedIndexActions.Count);
            var keys = enqueuedIndexActions
                       .Select(x => x.Key)
                       .OrderBy(x => x)
                       .ToArray();

            Assert.Equal(
                new[] { "A", "B", "C", "D", "E" },
                keys);

            _batchPusher.Verify(x => x.TryPushFullBatchesAsync(), Times.Exactly(5));
            _batchPusher.Verify(x => x.TryFinishAsync(), Times.Once);
        }
Example #27
0
        /// <summary>
        /// Upload document to Azure search service
        /// </summary>
        public static async Task UploadDocumentToSearchService <TDocument>(string indexName, TDocument document)
            where TDocument : SearchDocument
        {
            ISearchIndexClient searchIndexClient = SearchService.Indexes.GetClient(indexName);

            IndexAction <TDocument>[] actions = new IndexAction <TDocument>[]
            {
                IndexAction.Upload(document)
            };

            IndexBatch <TDocument> batch = IndexBatch.New(actions);

            await searchIndexClient.Documents.IndexAsync(batch);
        }
        protected override async Task ProcessMessage(ProductCreated product, string messageId, Message.SystemPropertiesCollection systemProperties, IDictionary <string, object> userProperties, CancellationToken cancellationToken)
        {
            var serviceName = this._configuration["AzureSearchIndex:ServiceName"];
            var serviceKey  = this._configuration["AzureSearchIndex:ServiceAdminKey"];
            var indexName   = this._configuration["AzureSearchIndex:CatalogIndex"];

            var serviceClient = new SearchServiceClient(serviceName, new SearchCredentials(serviceKey));
            var indexClient   = serviceClient.Indexes.GetClient(indexName);

            var actions = new IndexAction <CatalogDataIndex>[]
            {
                IndexAction.Upload(
                    new CatalogDataIndex
                {
                    Id           = product.ProductId.ToString(),
                    Name         = product.Name,
                    Description  = product.Description,
                    Brand        = product.BrandId,
                    BrandName    = product.BrandName,
                    Price        = Convert.ToDouble(product.BasePrice),
                    SpecialPrice = Convert.ToDouble(product.SpecialPrice),
                    Stock        = product.Stock,
                    Store        = product.SellerId,
                    StoreName    = product.SellerName,
                }
                    ),
            };

            var batch = IndexBatch.New(actions);

            try
            {
                await indexClient.Documents.IndexAsync(batch);
            }
            catch (IndexBatchException e)
            {
                // When a service is under load, indexing might fail for some documents in the batch.
                // Depending on your application, you can compensate by delaying and retrying.
                // For this simple demo, we just log the failed document keys and continue.
                this._logger.LogError(
                    "Failed to index some of the documents: {0}",
                    String.Join(", ", e.IndexingResults.Where(r => !r.Succeeded).Select(r => r.Key)));
            }
            catch (Exception e)
            {
                this._logger.LogError(e.ToString());
            }
        }
        private static void IndexDocuments(string indexName, List <string> groups)
        {
            var actions = new IndexAction <SecuredFiles>[]
            {
                IndexAction.Upload(
                    new SecuredFiles()
                {
                    FileId   = "1",
                    Name     = "secured_file_a",
                    GroupIds = new[] { groups[0] }
                }),
                IndexAction.Upload(
                    new SecuredFiles()
                {
                    FileId   = "2",
                    Name     = "secured_file_b",
                    GroupIds = new[] { groups[0] }
                }),
                IndexAction.Upload(
                    new SecuredFiles()
                {
                    FileId   = "3",
                    Name     = "secured_file_c",
                    GroupIds = new[] { groups[1] }
                })
            };

            var batch = IndexBatch.New(actions);

            try
            {
                _indexClient.Documents.Index(batch);
            }
            catch (IndexBatchException e)
            {
                // 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}",
                    String.Join(", ", e.IndexingResults.Where(r => !r.Succeeded).Select(r => r.Key)));
            }

            Console.WriteLine("Waiting for documents to be indexed...\n");
            Thread.Sleep(2000);
        }
Example #30
0
        private static void ImportData(SearchServiceClient serviceClient)
        {
            var hotelsText = File.ReadAllLines(hotelFileName);
            var hotels     = new List <Hotel>();

            for (int i = 1; i < hotelsText.Length; i++)
            {
                var hotelText        = hotelsText[i];
                var hotelTextColumns = hotelText.Split("\t");
                hotels.Add(
                    new Hotel()
                {
                    HotelId            = hotelTextColumns[0],
                    HotelName          = hotelTextColumns[1],
                    Description        = hotelTextColumns[2],
                    DescriptionFr      = hotelTextColumns[3],
                    Category           = hotelTextColumns[4],
                    Tags               = hotelTextColumns[5].Split(","),
                    ParkingIncluded    = hotelTextColumns[6] == "0" ? false : true,
                    SmokingAllowed     = hotelTextColumns[7] == "0" ? false : true,
                    LastRenovationDate = Convert.ToDateTime(hotelTextColumns[8]),
                    BaseRate           = Convert.ToDouble(hotelTextColumns[9]),
                    Rating             = (int)Convert.ToDouble(hotelTextColumns[10])
                });
            } // no error checking because demo code

            var actions = new List <IndexAction <Hotel> >();

            foreach (var hotel in hotels)
            {
                actions.Add(IndexAction.Upload(hotel));
            }

            var batch = IndexBatch.New(actions);

            try
            {
                ISearchIndexClient indexClient = serviceClient.Indexes.GetClient("hotels");
                indexClient.Documents.Index(batch);
            }
            catch (Exception e)
            {
                Console.WriteLine(e.ToString());
            }
        }