瀏覽代碼

搜索引擎词库优化

懒得勤快 5 年之前
父節點
當前提交
2a91c174f5

+ 1 - 1
src/Masuit.MyBlogs.Core/Controllers/BaseController.cs

@@ -143,7 +143,7 @@ namespace Masuit.MyBlogs.Core.Controllers
                 ViewBag.menus = MenuService.GetQueryFromCache<MenuDto>(m => m.Status == Status.Available).OrderBy(m => m.Sort).ToList(); //菜单
                 var model = new PageFootViewModel //页脚
                 {
-                    Links = LinksService.GetQueryFromCache<LinksDto>(l => l.Status == Status.Available).OrderByDescending(l => l.Recommend).ThenByDescending(l => l.Weight).ThenByDescending(l => new Random().Next()).Take(30).ToList()
+                    Links = LinksService.GetQueryFromCache<LinksDto>(l => l.Status == Status.Available).OrderByDescending(l => l.Recommend).ThenByDescending(l => l.Weight).Take(30).ToList()
                 };
                 ViewBag.Footer = model;
             }

+ 16 - 0
src/Masuit.MyBlogs.Core/Controllers/PostController.cs

@@ -109,6 +109,14 @@ namespace Masuit.MyBlogs.Core.Controllers
                 case PostLimitMode.AllowRegion:
                     if (!location.Contains(post.Regions.Split(',', StringSplitOptions.RemoveEmptyEntries)) && !CurrentUser.IsAdmin && !VisitorTokenValid && !Request.IsRobot())
                     {
+                        BackgroundJob.Enqueue(() => HangfireBackJob.InterceptLog(new IpIntercepter()
+                        {
+                            IP = ClientIP,
+                            RequestUrl = $"{Request.Host}/{post.Id}",
+                            Time = DateTime.Now,
+                            UserAgent = Request.Headers[HeaderNames.UserAgent],
+                            Remark = "无权限查看该文章"
+                        }));
                         throw new NotFoundException("文章未找到");
                     }
 
@@ -116,6 +124,14 @@ namespace Masuit.MyBlogs.Core.Controllers
                 case PostLimitMode.ForbidRegion:
                     if (location.Contains(post.Regions.Split(',', StringSplitOptions.RemoveEmptyEntries)) && !CurrentUser.IsAdmin && !VisitorTokenValid && !Request.IsRobot())
                     {
+                        BackgroundJob.Enqueue(() => HangfireBackJob.InterceptLog(new IpIntercepter()
+                        {
+                            IP = ClientIP,
+                            RequestUrl = $"{Request.Host}/{post.Id}",
+                            Time = DateTime.Now,
+                            UserAgent = Request.Headers[HeaderNames.UserAgent],
+                            Remark = "无权限查看该文章"
+                        }));
                         throw new NotFoundException("文章未找到");
                     }
 

+ 5 - 0
src/Masuit.MyBlogs.Core/Extensions/Hangfire/HangfireBackJob.cs

@@ -197,6 +197,11 @@ namespace Masuit.MyBlogs.Core.Extensions.Hangfire
                     {
                         link.UpdateTime = DateTime.Now;
                     }
+
+                    if (link.Status == Status.Unavailable)
+                    {
+                        link.Weight -= 1;
+                    }
                 }).Wait();
             });
             _linksService.SaveChanges();

+ 2 - 1
src/Masuit.MyBlogs.Core/Startup.cs

@@ -244,7 +244,8 @@ namespace Masuit.MyBlogs.Core
                 Console.WriteLine("正在导入自定义词库...");
                 double time = HiPerfTimer.Execute(() =>
                 {
-                    var lines = File.ReadAllLines(Path.Combine(env.ContentRootPath, "App_Data", "CustomKeywords.txt"));
+                    var set = ServiceProvider.GetRequiredService<DataContext>().Post.Select(p => $"{p.Title},{p.Label},{p.Keyword}").AsEnumerable().SelectMany(s => s.Split(new[] { ',', ' ', '+', '—' }, StringSplitOptions.RemoveEmptyEntries)).ToHashSet();
+                    var lines = File.ReadAllLines(Path.Combine(env.ContentRootPath, "App_Data", "CustomKeywords.txt")).Union(set);
                     var segmenter = new JiebaSegmenter();
                     foreach (var word in lines)
                     {