ASP.NET简单热词统计
程序员文章站
2022-07-09 19:18:56
一、功能介绍 用户在搜索框中输入某个字符,将一周内以这个字符开头的热词联想出来。 二、功能设计: 1、热词明细表 每次搜索的时候,插入一条数据 CREATE TABLE [dbo].[SearchDetails]( [Id] [uniqueidentifier] NOT NULL,--ID [Key ......
一、功能介绍
用户在搜索框中输入某个字符,将一周内以这个字符开头的热词联想出来。
二、功能设计:
1、热词明细表
每次搜索的时候,插入一条数据
create table [dbo].[searchdetails]( [id] [uniqueidentifier] not null,--id [keywords] [nvarchar](255) not null,--搜索内容 [searchdatetime] [datetime] not null,--搜索时间 constraint [pk_searchdetails] primary key clustered ( [id] asc )with (pad_index = off, statistics_norecompute = off, ignore_dup_key = off, allow_row_locks = on, allow_page_locks = on) on [primary] ) on [primary]
2、热词汇总表
使用定时框架quartz每隔一段时间将明细表搜索时间最近一周的数据汇总到热词汇总表当中。
create table [dbo].[searchdetails]( [id] [uniqueidentifier] not null, [keywords] [nvarchar](255) not null, [searchdatetime] [datetime] not null, constraint [pk_searchdetails] primary key clustered ( [id] asc )with (pad_index = off, statistics_norecompute = off, ignore_dup_key = off, allow_row_locks = on, allow_page_locks = on) on [primary] ) on [primary]
3、客户在搜索时使用jqueryui框架中的autocomplete
三、功能开发
创建一个控制台项目完成定时器的一些创建初始化
class program { static void main(string[] args) { 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(); } }
然后实现三个方法
/// <summary> /// 将统计的明细表的数据插入。 /// </summary> /// <returns></returns> public bool insertkeywordsrank() { string sql = "insert into keywordsrank(id,keywords,searchcount) select newid(),keywords,count(*) from searchdetails where datediff(day,searchdetails.searchdatetime,getdate())<=7 group by searchdetails.keywords"; return this.db.database.executesqlcommand(sql) > 0; } /// <summary> /// 删除汇总中的数据。 /// </summary> /// <returns></returns> public bool deleteallkeywordsrank() { string sql = "truncate table keywordsrank"; return this.db.database.executesqlcommand(sql) > 0; } /// <summary> /// 获取热词 /// </summary> /// <param name="term"></param> /// <returns></returns> public list<string> getsearchmsg(string term) { //keywords like term% string sql = "select keywords from keywordsrank where keywords like @term"; return this.db.database.sqlquery<string>(sql, new sqlparameter("@term", term + "%")).tolist(); }
在indexjob中调用
public class indexjob : ijob { public ikeywordsrankservice keywordsrankservice { get; set; } void ijob.execute(jobexecutioncontext context) { keywordsrankservice.deleteallkeywordsrank(); keywordsrankservice.insertkeywordsrank(); } }
在每次搜索的时候插入明细表,这步比较简单我就不贴代码了
然后就是前端了,引用的顺序不要反了,先引用jquery,再引用jqueryui
<script src="~/scripts/jquery-1.7.1.min.js"></script> <script src="~/scripts/jquery-ui-1.8.20.min.js"></script> <link href="~/content/themes/jquery-ui.css" rel="stylesheet" /> </style> .ui-autocomplete-loading { background: white url('/content/images/ui-anim_basic_16x16.gif') right center no-repeat; } </style> <script type="text/javascript"> jquery(function ($) { $(function () { $("#condition").autocomplete({ source: "/search/autocomplete" }); }); }); </script>
<form method="get" action="/search/searchcontent">
<input type="text" name="condition" id="condition" />
<input type="submit" name="btnsearch" value="搜一搜" />
@*<input type="submit" name="btncreate" value="创建索引" />*@
</form>
调试的时候要把控制台程序一直运行
这里其实还有个搜索的功能,我就不说了。
最后贴一下结果
我加了个让线程等待五秒,所以就能看到这个小圈圈了