博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Lucene热词统计
阅读量:6877 次
发布时间:2019-06-26

本文共 6014 字,大约阅读时间需要 20 分钟。

1、建立搜索表 ID KeyWords DT 搜索一次保存一次,id才用guid提高效率

///         /// 搜索数据        ///         /// 
private List
SearchBookContent() { string indexPath = @"C:\lucenedir"; List
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
list = new List
(); 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
ExecuteSelectQuery
(string sql, params System.Data.SqlClient.SqlParameter[] pars) { return Db.Database.SqlQuery
(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
{ bool DeleteAllKeyWords(); bool InsertKeyWords(); List
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
, IKeyWordsRankService { ///
/// 删除汇总表中的数据 /// ///
public bool DeleteAllKeyWords() { string sql = "truncate table KeyWordsRank"; return this.GetCurrentDbSession.ExecuteSql(sql)>0; } ///
/// 向汇总表中插入数据 /// ///
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; } ///
/// 返回热词 /// ///
///
public List
GetSearchWord(string str) { string sql = "select KeyWords from KeyWordsRank where KeyWords like @msg"; return this.GetCurrentDbSession.ExecuteSelectQuery
(sql, new SqlParameter("@msg",str+"%")); } }}
KeyWordsRankService

 

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();

 

转载于:https://www.cnblogs.com/ecollab/p/6165193.html

你可能感兴趣的文章
grep文本搜索命令+正则表达式搜索详解
查看>>
事件、流程和长期运行的服务:工作流自动化的现代解决方案
查看>>
Linux_5day------------使用crontab调度命令
查看>>
istio 各组件概念个人理解
查看>>
南下找工作
查看>>
actionscript3.0 Socket通信实例文章
查看>>
ZooKeeper和CAP理论及一致性原则
查看>>
LDAP Admin:开源的Windows LDAP管理工具
查看>>
yii 学习笔记十六、数据分页
查看>>
Keepalived+Lvs---初级安装
查看>>
我的友情链接
查看>>
MVC与MVP模式设计
查看>>
Linux虚拟机里面安装VMware Tools增强工具
查看>>
iOS 获取手机型号,系统版本
查看>>
yii上传图片、yii上传文件、yii控件activeFileField使用
查看>>
DevExpress:条形码显示控件BarCodeControl
查看>>
Eclipse中用于CodeTemplate的变量总结
查看>>
一个技术高手的博客,看了不要错过
查看>>
Cocos2d-x--Box2D使用GLES-Render.h渲染查看刚体
查看>>
图片阴影效果和影子效果
查看>>