public Task Handle(NewsUpdateOrCreateCommand message, IMessageHandlerContext context) { IElasticsearchMappingResolver elasticsearchMappingResolver = new ElasticsearchMappingResolver(); using (var elasticsearchContext = new ElasticsearchContext(ConfigurationManager.AppSettings["ElasticServer"], elasticsearchMappingResolver)) { FullInfoNews newNews = new FullInfoNews { ApplicationUserId = message.ApplicationUserId, Body = message.Body, CategoryId = message.CategoryId, CategoryName = message.CategoryName, CreatedDate = message.CreatedDate, Title = message.Title, UserName = message.UserName, Id = message.Id, Description = message.Description, Published = message.Published }; elasticsearchContext.AddUpdateDocument(newNews, newNews.Id); elasticsearchContext.SaveChanges(); } return(Task.CompletedTask); }
public async Task SaveDocumentToElasticsearch() { IElasticsearchMappingResolver elasticsearchMappingResolver = new ElasticsearchMappingResolver(); using (var elasticsearchContext = new ElasticsearchContext(ConfigurationManager.AppSettings["ElasticServer"], elasticsearchMappingResolver)) { List <NewsDto> news = _newsService.GetAll().Where(a => a.Published).ToList(); foreach (NewsDto item in news) { FullInfoNews newNews = new FullInfoNews { ApplicationUserId = item.ApplicationUserId, Body = item.Body, CategoryId = item.CategoryId, CategoryName = (await _categoryService.GetById(item.CategoryId)).Name, CreatedDate = item.CreatedDate, Title = item.Title, UserName = await _userService.GetUserNameById(item.ApplicationUserId), Id = item.Id, Description = item.Description, Published = item.Published }; elasticsearchContext.AddUpdateDocument(newNews, newNews.Id); elasticsearchContext.SaveChanges(); } List <CategoryDto> categories = _categoryService.GetAll().Where(a => a.Active).ToList(); foreach (CategoryDto item in categories) { CategoryForElastic newCategory = new CategoryForElastic { Id = item.Id, Name = item.Name, CountNews = _categoryService.NewsCountByCategory(item.Id) }; elasticsearchContext.AddUpdateDocument(newCategory, newCategory.Id); elasticsearchContext.SaveChanges(); } } }