標籤:意義 sed tab using string -- context sql term
1、建立搜尋表 ID KeyWords DT 搜尋一次儲存一次,id才用guid提高效率
/// <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; }搜尋資料
2、建立搜尋統計表 ID KeyWords SearchCount 定時任務每天淩晨統計
Quartz.Net 調度架構配置介紹
匯入 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方法
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 類
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); }}IKeyWordsRankService
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+"%")); } }}KeyWordsRankService
Lucene熱詞統計