瀏覽代碼

搜索统计优化

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

+ 2 - 2
src/Masuit.MyBlogs.Core/Controllers/SearchController.cs

@@ -61,7 +61,7 @@ namespace Masuit.MyBlogs.Core.Controllers
                 {
                     SearchDetailsService.AddEntity(new SearchDetails
                     {
-                        KeyWords = wd,
+                        Keywords = wd,
                         SearchTime = DateTime.Now,
                         IP = ClientIP
                     });
@@ -98,7 +98,7 @@ namespace Masuit.MyBlogs.Core.Controllers
         [MyAuthorize, HttpPost, ResponseCache(Duration = 600, VaryByQueryKeys = new[] { "page", "size", "search" }, VaryByHeader = "Cookie")]
         public ActionResult SearchList(int page = 1, int size = 10, string search = "")
         {
-            var where = string.IsNullOrEmpty(search) ? (Expression<Func<SearchDetails, bool>>)(s => true) : s => s.KeyWords.Contains(search);
+            var where = string.IsNullOrEmpty(search) ? (Expression<Func<SearchDetails, bool>>)(s => true) : s => s.Keywords.Contains(search);
             var list = SearchDetailsService.GetPages<DateTime, SearchDetailsDto>(page, size, out int total, where, s => s.SearchTime, false).ToList();
             var pageCount = Math.Ceiling(total * 1.0 / size).ToInt32();
             return PageResult(list, pageCount, total);

+ 0 - 2
src/Masuit.MyBlogs.Core/Infrastructure/DataContext.cs

@@ -46,7 +46,6 @@ namespace Masuit.MyBlogs.Core.Infrastructure
             });
             modelBuilder.Entity<SeminarPostHistoryVersion>().Property(s => s.SeminarId).HasColumnName("Seminar_Id");
             modelBuilder.Entity<SeminarPostHistoryVersion>().Property(s => s.PostHistoryVersionId).HasColumnName("PostHistoryVersion_Id");
-            modelBuilder.Entity<SearchRank>().HasNoKey().HasNoDiscriminator();
         }
 
         public override int SaveChanges()
@@ -81,7 +80,6 @@ namespace Masuit.MyBlogs.Core.Infrastructure
 
         public virtual DbSet<PostMergeRequest> PostMergeRequests { get; set; }
         public virtual DbSet<Advertisement> Advertisements { get; set; }
-        public virtual DbSet<SearchRank> SearchRanks { get; set; }
     }
 
     /// <summary>

+ 6 - 2
src/Masuit.MyBlogs.Core/Infrastructure/Repository/SearchDetailsRepository.cs

@@ -1,6 +1,5 @@
 using Masuit.MyBlogs.Core.Infrastructure.Repository.Interface;
 using Masuit.MyBlogs.Core.Models.Entity;
-using Microsoft.EntityFrameworkCore;
 using System;
 using System.Collections.Generic;
 using System.Linq;
@@ -20,9 +19,14 @@ namespace Masuit.MyBlogs.Core.Infrastructure.Repository
             return t;
         }
 
+        /// <summary>
+        /// 热词统计
+        /// </summary>
+        /// <param name="start"></param>
+        /// <returns></returns>
         public List<SearchRank> GetRanks(DateTime start)
         {
-            return DataContext.SearchRanks.FromSqlRaw($"SELECT KeyWords Keywords,COUNT(*) Count from (SELECT IP,KeyWords FROM `searchdetails` WHERE SearchTime>'{start:yyyy-MM-dd}' GROUP BY IP,KeyWords) as t GROUP BY KeyWords ORDER BY COUNT(1) DESC LIMIT 30").ToList();
+            return DataContext.SearchDetails.Where(s => s.SearchTime > start).Select(s => new { s.IP, s.Keywords }).Distinct().GroupBy(s => s.Keywords).Select(g => new SearchRank { Keywords = g.Key, Count = g.Count() }).OrderByDescending(s => s.Count).Take(30).ToList();
         }
     }
 }

+ 1 - 1
src/Masuit.MyBlogs.Core/Models/Command/SearchDetailsCommand.cs

@@ -17,7 +17,7 @@ namespace Masuit.MyBlogs.Core.Models.Command
         /// 关键词
         /// </summary>
         [Required(ErrorMessage = "关键词不能为空"), MaxLength(64, ErrorMessage = "关键词最大允许64个字符")]
-        public string KeyWords { get; set; }
+        public string Keywords { get; set; }
 
         /// <summary>
         /// 搜索时间

+ 1 - 1
src/Masuit.MyBlogs.Core/Models/DTO/SearchDetailsDto.cs

@@ -15,7 +15,7 @@ namespace Masuit.MyBlogs.Core.Models.DTO
         /// <summary>
         /// 关键词
         /// </summary>
-        public string KeyWords { get; set; }
+        public string Keywords { get; set; }
 
         /// <summary>
         /// 搜索时间

+ 1 - 1
src/Masuit.MyBlogs.Core/Models/Entity/SearchDetails.cs

@@ -22,7 +22,7 @@ namespace Masuit.MyBlogs.Core.Models.Entity
         /// 关键词
         /// </summary>
         [Required(ErrorMessage = "关键词不能为空")]
-        public string KeyWords { get; set; }
+        public string Keywords { get; set; }
 
         /// <summary>
         /// 搜索时间

+ 1 - 1
src/Masuit.MyBlogs.Core/appsettings.json

@@ -1,7 +1,7 @@
 {
     "Logging": {
         "LogLevel": {
-            "Default": "Error"
+            "Default": "Information"
         }
     },
     "AllowedHosts": "*",

+ 1 - 1
src/Masuit.MyBlogs.Core/wwwroot/ng-views/views/analysis/search.html

@@ -15,7 +15,7 @@
     <table ng-table="list.tableParams" id="interview" class="table table-bordered table-hover table-condensed" ng-form="list.tableForm" disable-filter="list.isAdding" tracked-table="list.tableTracker">
         <tr ng-repeat="row in $data" ng-form="rowForm" tracked-table-row="row">
             <td title="'关键词'">
-                <a ng-href="/s/{{row.KeyWords}}" target="_blank">{{row.KeyWords}}</a>
+                <a ng-href="/s/{{row.Keywords}}" target="_blank">{{row.Keywords}}</a>
             </td>
             <td title="'搜索时间'">
                 {{row.SearchTime|date:'yyyy-MM-dd HH:mm:ss'}}