一、创建搜索表 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();