一、創建搜索表 ID KeyWords DT 搜索一次保存一次,id才用guid提升效率spring
/// <summary> /// 搜索數據 /// </summary> /// <returns></returns> private List<ViewSarchContentModel> SearchBookContent() { string indexPath = @"C:\lucenedir"; List<string> kw =Common.WebCommon.GetPanGuWord(Request["txtContent"]); FSDirectory directory = FSDirectory.Open(new DirectoryInfo(indexPath), new NoLockFactory()); IndexReader reader = IndexReader.Open(directory, true); IndexSearcher searcher = new IndexSearcher(reader); //搜索條件 PhraseQuery query = new PhraseQuery(); foreach (string word in kw)//先用空格,讓用戶去分詞,空格分隔的就是詞「計算機 專業」 { query.Add(new Term("Content", word)); } //query.Add(new Term("body","語言"));--能夠添加查詢條件,二者是add關係.順序沒有關係. //query.Add(new Term("body", "大學生")); //query.Add(new Term("body", kw));//body中含有kw的文章 query.SetSlop(100);//多個查詢條件的詞之間的最大距離.在文章中相隔太遠 也就無心義.(例如 「大學生」這個查詢條件和"簡歷"這個查詢條件之間若是間隔的詞太多也就沒有意義了。) //TopScoreDocCollector是盛放查詢結果的容器 TopScoreDocCollector collector = TopScoreDocCollector.create(1000, true); searcher.Search(query, null, collector);//根據query查詢條件進行查詢,查詢結果放入collector容器 ScoreDoc[] docs = collector.TopDocs(0, collector.GetTotalHits()).scoreDocs;//獲得全部查詢結果中的文檔,GetTotalHits():表示總條數 TopDocs(300, 20);//表示獲得300(從300開始),到320(結束)的文檔內容. //能夠用來實現分頁功能 List<ViewSarchContentModel> list = new List<ViewSarchContentModel>(); for (int i = 0; i < docs.Length; i++) { ViewSarchContentModel viewModel = new ViewSarchContentModel(); // //搜索ScoreDoc[]只能得到文檔的id,這樣不會把查詢結果的Document一次性加載到內存中。下降了內存壓力,須要得到文檔的詳細內容的時候經過searcher.Doc來根據文檔id來得到文檔的詳細內容對象Document. int docId = docs[i].doc;//獲得查詢結果文檔的id(Lucene內部分配的id) Document doc = searcher.Doc(docId);//找到文檔id對應的文檔詳細信息 viewModel.Id = doc.Get("Id"); viewModel.Title = doc.Get("Title"); viewModel.Content =Common.WebCommon.CreateHightLight(Request["txtContent"], doc.Get("Content"));//搜索內容關鍵字高亮顯示 list.Add(viewModel); } SearchDetails searchDetail = new SearchDetails(); searchDetail.Id = Guid.NewGuid(); searchDetail.KeyWords = Request["txtContent"]; searchDetail.SearchDateTime = DateTime.Now; SearchDetailsService.AddEntity(searchDetail); return list; }
二、創建搜索統計表 ID KeyWords SearchCount 定時任務天天凌晨統計sql
Quartz.Net 調度框架配置介紹app
導入 Quartz.dll 和 Common.Logging.dll文件,而spring.net已經引用了Common.Logging.dll,因此要保證兩個用的版本一致框架
public class IndexJob:IJob { IBLL.IKeyWordsRankService bll = new BLL.KeyWordsRankService(); public void Execute(JobExecutionContext context) { bll.DeleteAllKeyWords(); bll.InsertKeyWords(); } }
在DBSESSION加入執行SQL方法ide
public int ExecuteSql(string sql,params System.Data.SqlClient.SqlParameter[] pars) { return Db.Database.ExecuteSqlCommand(sql, pars); } public List<T> ExecuteSelectQuery<T>(string sql, params System.Data.SqlClient.SqlParameter[] pars) { return Db.Database.SqlQuery<T>(sql, pars).ToList(); }
創建業務KeyWordsRankService 類ui
using CZBK.ItcastOA.Model; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace CZBK.ItcastOA.IBLL { public partial interface IKeyWordsRankService:IBaseService<KeyWordsRank> { bool DeleteAllKeyWords(); bool InsertKeyWords(); List<string> GetSearchWord(string str); } }
using CZBK.ItcastOA.IBLL; using CZBK.ItcastOA.Model; using System; using System.Collections.Generic; using System.Data.SqlClient; using System.Linq; using System.Text; using System.Threading.Tasks; namespace CZBK.ItcastOA.BLL { public partial class KeyWordsRankService : BaseService<KeyWordsRank>, IKeyWordsRankService { /// <summary> /// 刪除彙總表中的數據 /// </summary> /// <returns></returns> public bool DeleteAllKeyWords() { string sql = "truncate table KeyWordsRank"; return this.GetCurrentDbSession.ExecuteSql(sql)>0; } /// <summary> /// 向彙總表中插入數據 /// </summary> /// <returns></returns> public bool InsertKeyWords() { string sql = "insert into KeyWordsRank(Id,KeyWords,SearchCount) select newid(),KeyWords,count(*) from SearchDetails where DateDiff(day,SearchDateTime,getdate())<=7 group by KeyWords"; return this.GetCurrentDbSession.ExecuteSql(sql)>0; } /// <summary> /// 返回熱詞 /// </summary> /// <param name="str"></param> /// <returns></returns> public List<string> GetSearchWord(string str) { string sql = "select KeyWords from KeyWordsRank where KeyWords like @msg"; return this.GetCurrentDbSession.ExecuteSelectQuery<string>(sql, new SqlParameter("@msg",str+"%")); } } }
IScheduler sched; ISchedulerFactory sf = new StdSchedulerFactory(); sched = sf.GetScheduler(); JobDetail job = new JobDetail("job1", "group1", typeof(IndexJob));//IndexJob爲實現了IJob接口的類 DateTime ts = TriggerUtils.GetNextGivenSecondDate(null, 5);//5秒後開始第一次運行 TimeSpan interval = TimeSpan.FromSeconds(5);//每隔1小時執行一次 Trigger trigger = new SimpleTrigger("trigger1", "group1", "job1", "group1", ts, null, SimpleTrigger.RepeatIndefinitely, interval);//每若干小時運行一次,小時間隔由appsettings中的IndexIntervalHour參數指定 sched.AddJob(job, true); sched.ScheduleJob(trigger); sched.Start(); Console.ReadKey();