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 #2
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);
                }
            }
        }