Ejemplo n.º 1
0
        public async Task AddOrUpdateBlogAsync(BlogEventArgs b)
        {
            using (var scope = _scopeFactory.CreateScope())
            {
                var util = scope.ServiceProvider.GetService <ContextlessBlogUtil>();
                var tags = b.Tags;
                if (tags == null)
                {
                    var tagUtil = scope.ServiceProvider.GetService <TagUtil>();
                    tags = await tagUtil.GetTagsInBlogAsync(b.Model.BlogID);
                }
                var result = await _client.IndexAsync(BlogIndexed.FromBlogTag(b.Model, tags.Select(t => t.TagName), util.GetPostCount(b.Model)), i => i.Refresh(Elasticsearch.Net.Refresh.True));

                if (!result.IsValid)
                {
                    _logger.LogError(result.DebugInformation);
                }
            }
        }
Ejemplo n.º 2
0
        public async Task <IEnumerable <Blog> > GetRecommendationAsync(Blog blog, IEnumerable <string> tags, int count)
        {
            if (!IsValid())
            {
                return(Enumerable.Empty <Blog>());
            }
            var result = await _client.SearchAsync <BlogIndexed>(
                s => s.Query(q => q.Bool(b => b.Should(
                                             bs => bs.MoreLikeThis(mlt =>
                                                                   mlt.Like(l => l.Document(d => d.Document(BlogIndexed.FromBlogTag(blog, tags, 0)).Id(null)))
                                                                   .MinTermFrequency(1).Fields(new string[] { "title", "tags" })),
                                             bs => bs.Term("categoryId", blog.CategoryID))
                                         .MustNot(
                                             bs => bs.Ids(i => i.Values(blog.BlogID)),
                                             bs => bs.Term("isApproved", false)))).Size(count),
                _httpContext.RequestAborted);

            if (result.IsValid)
            {
                return(result.Documents.Select(d => d.ToBlog()));
            }
            return(Enumerable.Empty <Blog>());
        }