浏览代码

搜索优化

懒得勤快 4 年之前
父节点
当前提交
f2693a2c25

+ 3 - 16
src/Masuit.MyBlogs.Core/Controllers/SearchController.cs

@@ -1,5 +1,4 @@
-using CacheManager.Core;
-using EFCoreSecondLevelCacheInterceptor;
+using EFCoreSecondLevelCacheInterceptor;
 using Masuit.MyBlogs.Core.Common;
 using Masuit.MyBlogs.Core.Extensions;
 using Masuit.MyBlogs.Core.Infrastructure.Services.Interface;
@@ -27,7 +26,6 @@ namespace Masuit.MyBlogs.Core.Controllers
     public class SearchController : BaseController
     {
         public ISearchDetailsService SearchDetailsService { get; set; }
-        public ICacheManager<string> CacheManager { get; set; }
 
         /// <summary>
         /// 搜索页
@@ -48,16 +46,7 @@ namespace Masuit.MyBlogs.Core.Controllers
 
             ViewBag.PageSize = size;
             ViewBag.Keyword = wd;
-            string key = "Search:" + ClientIP;
-            if (CacheManager.Exists(key) && CacheManager.Get(key) != wd)
-            {
-                var hotSearches = RedisHelper.Get<List<KeywordsRank>>("SearchRank:Week").Take(10).ToList();
-                ViewBag.hotSearches = hotSearches;
-                ViewBag.ErrorMsg = "10秒内只能搜索1次!";
-                return View(new SearchResult<PostDto>());
-            }
-
-            if (!string.IsNullOrWhiteSpace(wd) && !wd.Contains("锟斤拷"))
+            if (!string.IsNullOrWhiteSpace(wd))
             {
                 if (!HttpContext.Session.TryGetValue("search:" + wd, out _) && !HttpContext.Request.IsRobot())
                 {
@@ -73,10 +62,8 @@ namespace Masuit.MyBlogs.Core.Controllers
 
                 var posts = postService.SearchPage(page, size, wd);
                 CheckPermission(posts);
-                if (posts.Total > 1)
+                if (posts.Results.Count > 1)
                 {
-                    CacheManager.AddOrUpdate(key, wd, s => wd);
-                    CacheManager.Expire(key, TimeSpan.FromSeconds(10));
                     ViewBag.Ads = AdsService.GetByWeightedPrice(AdvertiseType.PostList, Request.Location());
                 }
 

+ 1 - 17
src/Masuit.MyBlogs.Core/Infrastructure/Services/PostService.cs

@@ -1,7 +1,6 @@
 using CacheManager.Core;
 using Masuit.LuceneEFCore.SearchEngine;
 using Masuit.LuceneEFCore.SearchEngine.Interfaces;
-using Masuit.MyBlogs.Core.Common;
 using Masuit.MyBlogs.Core.Infrastructure.Repository.Interface;
 using Masuit.MyBlogs.Core.Infrastructure.Services.Interface;
 using Masuit.MyBlogs.Core.Models.DTO;
@@ -57,32 +56,17 @@ namespace Masuit.MyBlogs.Core.Infrastructure.Services
             var entities = searchResult.Results.Where(s => s.Entity.Status == Status.Published).DistinctBy(s => s.Entity.Id).ToList();
             var ids = entities.Select(s => s.Entity.Id).ToArray();
             var dic = GetQuery<PostDto>(p => ids.Contains(p.Id)).ToDictionary(p => p.Id);
-            var posts = entities.Select(s =>
-            {
-                var item = s.Entity.Mapper<PostDto>();
-                if (dic.ContainsKey(item.Id))
-                {
-                    item.CategoryName = dic[item.Id].CategoryName;
-                    item.ModifyDate = dic[item.Id].ModifyDate;
-                    item.CommentCount = dic[item.Id].CommentCount;
-                    item.TotalViewCount = dic[item.Id].TotalViewCount;
-                    item.CategoryId = dic[item.Id].CategoryId;
-                }
-
-                return item;
-            }).ToList();
+            var posts = entities.Where(s => dic.ContainsKey(s.Entity.Id)).Select(s => dic[s.Entity.Id]).ToList();
             var simpleHtmlFormatter = new SimpleHTMLFormatter("<span style='color:red;background-color:yellow;font-size: 1.1em;font-weight:700;'>", "</span>");
             var highlighter = new Highlighter(simpleHtmlFormatter, new Segment()) { FragmentSize = 200 };
             var keywords = Searcher.CutKeywords(keyword);
             HighlightSegment(posts, keywords, highlighter);
-
             var result = new SearchResult<PostDto>()
             {
                 Results = posts,
                 Elapsed = searchResult.Elapsed,
                 Total = searchResult.TotalHits
             };
-
             _cacheManager.Add(cacheKey, result);
             _cacheManager.Expire(cacheKey, TimeSpan.FromHours(1));
             return result;

+ 2 - 2
src/Masuit.MyBlogs.Core/Masuit.MyBlogs.Core.csproj

@@ -46,7 +46,7 @@
         <PackageReference Include="Microsoft.AspNetCore.Mvc.NewtonsoftJson" Version="5.0.8" />
         <PackageReference Include="Microsoft.EntityFrameworkCore.Proxies" Version="5.0.8" />
         <PackageReference Include="Microsoft.Extensions.Http.Polly" Version="5.0.1" />
-        <PackageReference Include="Microsoft.Graph" Version="4.0.0" />
+        <PackageReference Include="Microsoft.Graph" Version="4.1.0" />
         <PackageReference Include="Microsoft.Graph.Auth" Version="1.0.0-preview.6" />
         <PackageReference Include="MiniProfiler.AspNetCore.Mvc" Version="4.2.22" />
         <PackageReference Include="OpenXmlPowerTools-NetStandard" Version="4.4.21" />
@@ -57,7 +57,7 @@
         <PackageReference Include="System.Linq.Dynamic.Core" Version="1.2.12" />
         <PackageReference Include="TimeZoneConverter" Version="3.5.0" />
         <PackageReference Include="WilderMinds.RssSyndication" Version="1.7.0" />
-        <PackageReference Include="Z.EntityFramework.Plus.EFCore" Version="5.2.5" />
+        <PackageReference Include="Z.EntityFramework.Plus.EFCore" Version="5.2.6" />
     </ItemGroup>
     <ItemGroup>
         <Content Update="appsettings.json">

+ 0 - 2
src/Masuit.MyBlogs.Core/Views/Dashboard/Index.cshtml

@@ -22,7 +22,6 @@
     <link href="https://cdn.staticfile.org/angular-loading-bar/0.9.0/loading-bar.min.css" rel="stylesheet">
     <link href="https://cdn.staticfile.org/limonte-sweetalert2/6.6.9/sweetalert2.min.css" rel="stylesheet" async defer>
     <link href="https://cdn.staticfile.org/notie/4.3.1/notie.min.css" rel="stylesheet">
-    <link href="https://cdn.staticfile.org/izitoast/1.4.0/css/iziToast.min.css" rel="stylesheet">
     <link href="https://cdn.staticfile.org/node-waves/0.7.6/waves.min.css" rel="stylesheet">
     <link href="https://cdn.bootcdn.net/ajax/libs/ng-table/1.0.0/ng-table.css" rel="stylesheet" async defer>
     <link href="~/Assets/layui/css/layui.min.css" rel="stylesheet" />
@@ -55,7 +54,6 @@
     <script src="https://cdn.staticfile.org/linq.js/2.2.0.2/linq.min.js"></script>
     <script src="https://cdn.staticfile.org/angular-ui-tree/2.22.6/angular-ui-tree.min.js"></script>
     <script src="https://cdn.staticfile.org/jquery.form/4.2.2/jquery.form.min.js"></script>
-    <script src="https://cdn.staticfile.org/izitoast/1.4.0/js/iziToast.min.js"></script>
     <script src="https://cdn.staticfile.org/node-waves/0.7.6/waves.min.js"></script>
     <script src="https://cdn.bootcdn.net/ajax/libs/ng-table/1.0.0/ng-table.js"></script>
     <script src="~/Scripts/layer/layer.js"></script>

+ 1 - 8
src/Masuit.MyBlogs.Core/Views/Search/Search.cshtml

@@ -22,7 +22,6 @@
             <div class="col-sm-12">
                 @if (!string.IsNullOrEmpty(ViewBag.Keyword))
                 {
-                    <h3>为您找到约 @Model.Total 条<span class="text-navy">“@ViewBag.Keyword”</span>匹配或相关的搜索结果。</h3>
                     <small>搜索用时(@Model.Elapsed 毫秒)</small>
                 }
                 <div class="search-form">
@@ -33,7 +32,6 @@
                                 <button type="button" class="sr-only btn btn-default dropdown-toggle" data-toggle="dropdown" style="display: none"><span class="caret"></span></button>
                                 <ul class="dropdown-menu dropdown-menu-right" role="menu"></ul>
                                 <button class="btn btn-lg btn-primary" type="submit">搜索</button>
-                                <a class="baidu btn btn-lg btn-info">百度一下</a>
                             </div>
                         </div>
                     </form>
@@ -163,9 +161,4 @@
             </div>
         </div>
     </div>
-</div>
-<script>
-    $(".baidu").on("click", function(e) {
-		window.location.href = `https://www.baidu.com/[email protected]&[email protected]&ct=2097152`;
-	});
-</script>
+</div>

+ 1 - 1
src/Masuit.MyBlogs.Core/Views/Shared/_Layout.cshtml

@@ -266,7 +266,7 @@
             </ul>
         </nav>
         <div id="cd-search" class="cd-search">
-            <form method="get" asp-controller="Search">
+            <form method="get" asp-controller="Search" asp-action="Search">
                 <input type="search" name="wd" placeholder="请在此处输入您想要的搜索的关键词,支持部分指令:intitle,content,如:intitle:会声会影 content:懒得勤快,指令支持组合">
             </form>
         </div>

+ 4 - 53
src/Masuit.MyBlogs.Core/wwwroot/ng-views/controllers/main.js

@@ -1,23 +1,6 @@
 myApp.controller('main', ["$timeout", "$state", "$scope", "$http", function($timeout, $state, $scope, $http) {
 	$scope.post = {};
 	Waves.init();
-	iziToast.settings({
-		timeout:15000,
-		// position: 'center',
-		// imageWidth: 50,
-		pauseOnHover:true,
-		// resetOnHover: true,
-		close:true,
-		progressBar:true,
-		// layout: 1,
-		// balloon: true,
-		// target: '.target',
-		// icon: 'material-icons',
-		// iconText: 'face',
-		// animateInside: false,
-		// transitionIn: 'flipInX',
-		// transitionOut: 'flipOutX',
-	});
 	localStorage.setItem('ma-layout-status', 1);
 	if(/Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent)) {
 		angular.element('html').addClass('ismobile');
@@ -232,7 +215,7 @@
 
 	this.CheckLogin = $scope.CheckLogin;
 
-	function getmsgs(show) {
+	function getmsgs() {
 		fetch("/dashboard/getmessages",{
             headers:{
                 'Accept': 'application/json',
@@ -242,40 +225,16 @@
             return response.json();
         }).then(function(res) {
             $scope.Msgs = res.Data;
-			if(res.Data.post.length > 0&&show) {
-				iziToast.info({
-					title:'待审核文章',
-					message:'有' + res.Data.post.length + '篇新文章待审核哦!',
-					position:'topRight',
-					transitionIn:'bounceInRight',
-				});
-			}
-			if(res.Data.msgs.length > 0&&show) {
-				iziToast.info({
-					title:'待审核留言',
-					message:'有' + res.Data.msgs.length + '条新留言待审核哦!',
-					position:'topRight',
-					transitionIn:'bounceInRight',
-				});
-			}
-			if(res.Data.comments.length > 0&&show) {
-				iziToast.info({
-					title:'待审核文章评论',
-					message:'有' + res.Data.comments.length + '条新文章评论待审核哦!',
-					position:'topRight',
-					transitionIn:'bounceInRight',
-				});
-			}
 			$scope.$apply();
         }).catch(function(e) {
             console.log("Oops, error");
         });
         if (($scope.InternalMsgs||[]).length==0) {
-			getUnreadMsgs(true);
+			getUnreadMsgs();
         }
     }
 
-    function getUnreadMsgs(show) {
+    function getUnreadMsgs() {
         fetch("/msg/GetUnreadMsgs",{
                 headers:{
                     'Accept': 'application/json',
@@ -285,20 +244,12 @@
                 return response.json();
             }).then(function(res) {
                 $scope.InternalMsgs = res.Data;
-			    if($scope.InternalMsgs.length > 0&&show) {
-				    iziToast.info({
-					    title:'未读消息',
-					    message:'有' + $scope.InternalMsgs.length + '条未读消息!',
-					    position:'topRight',
-					    transitionIn:'bounceInRight',
-				    });
-			    }
 			    $scope.$apply();
             }).catch(function(e) {
                 console.log("Oops, error");
             });
     }
-	getmsgs(true);
+	getmsgs();
 	setInterval(getmsgs,5000);
 	$scope.read = function(id) {
 		$http.post("/msg/read", {