随笔 - 44  文章 - 19  评论 - 22  阅读 - 18万

初探站内搜索(下)

建立好Index数据库之后,我们就可以开始进行检索了。首先我们在sql2005中建立一个关键字索引表SearchLog表

字段如下:在SearchDateTime上建立一个索引,因为该字段需要经常查询,这是优化数据库最基本的哦。

这样我们在VS2008中添加一个强类型DataSet,把表拖到DataSet视图上生成强类型类,接着添加SQLHelper类来访问数据库。
数据访问层:
SQLHelper.cs代码如下:这里我们只需要返回个DataTable,如果有需要的话自己可以再天加点方法进去
复制代码
1 public class SQLHelper
2 {
3 public static string connstr = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
4
5 public static DataTable ExecuteDataTable(string sql,params SqlParameter[] parameters)
6 {
7
8 using (SqlConnection conn = new SqlConnection(connstr))
9 {
10 conn.Open();
11 using (SqlCommand cmd = conn.CreateCommand())
12 {
13 cmd.CommandText = sql;
14 cmd.Parameters.AddRange(parameters);
15 DataSet dataset = new DataSet();
16 using (SqlDataAdapter adapter = new SqlDataAdapter(cmd))
17 {
18 adapter.Fill(dataset);
19 }
20 return dataset.Tables[0];
21 }
22 }
23 }
24
25 }
复制代码

最后在SearchSuggestion.ashx一般处理程序中实现对用户在浏览器端输入关键字改变之后的AJAX请求:

 

复制代码
1 public class SearchSuggestion : IHttpHandler
2 {
3
4 public void ProcessRequest(HttpContext context)
5 {//服务器返回搜索建议词汇的时候将词汇以字符串数组的形式(JSon)返回给浏览器
6   context.Response.ContentType = "text/plain";
7 string term=context .Request ["term"];//通过httpwatch看出我们从客户端页面传过来的参数是term
8 //string[] strs = { "baidu", "google", "sina" };
9  
10 DataTable table = SQLHelper.ExecuteDataTable("select distinct KeyWord from SearchLog where KeyWord like @word", new SqlParameter("word", "%" + term + "%"));
11 List<string> list = new List<string>();
12 for (int i = 0; i < table.Rows.Count; i++)
13 {
14 string word = Convert.ToString(table.Rows[i][0]);
15 list.Add(word);
16 }
17
18 JavaScriptSerializer jss = new JavaScriptSerializer();
19 context.Response.Write(jss.Serialize(list));
20 }
21
22 public bool IsReusable
23 {
24 get
25 {
26 return false;
27 }
28 }
29 }
复制代码

这样客户端浏览器的JQueryUI就实现了点击自动补全功能然后查选该热门关键词。有关前台调用JQuery的代码在上一篇日志有。

最后在web.config中设置我们要从哪个服务器下载的网页 

<appSettings>
    <add key="SiteURL" value="http://localhost:8081/" />   
  </appSettings>

 

OK~至此我的站内搜索学习算告一段落了

谢谢各位~

 
posted on   013  阅读(495)  评论(2编辑  收藏  举报
编辑推荐:
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
阅读排行:
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· Docker 太简单,K8s 太复杂?w7panel 让容器管理更轻松!
< 2010年10月 >
26 27 28 29 30 1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
31 1 2 3 4 5 6

点击右上角即可分享
微信分享提示