ASP智能搜索的实现

ASP智能搜索的实现

    用ASP实现搜索引擎的功能是一件很方便的事,可是,如何实现类似3721的智能搜索呢?比如,当在搜索条件框内输入“中国人民”时,自动从中提取“中国”、“人民”等关键字并在数据库内进行搜索。看完本文后,你就可以发现,这个功能实现起来竟然是如此的简单。OK,Follow Me!
    第一步,我们要建立一个名为db_sample.mdb的数据库(本文以Access2000数据库为例),并在其中建立表T_Sample。表T_Sample包括如下字段:
        ID            自动编号
        U_Name    文本
        U_Info      备注
    第二步,我们开始设计搜索页面Search.asp。该页面包括一个表单(Frm_Search),表单内包括一个文本框和一个提交按钮。并将表单的method属性设为“get” ,action属性设为“Search.asp",即提交给网页自身。代码如下:
    <!-- Search.asp -->
    <form name="frm_Search" method="get" action="Search.asp">
       请输入关键字:
       <input type="text" name="key" size="10">
       <input type="submit" value="搜索">
    </form>
    下面,就进入了实现智能搜索的关键部分。
    首先,建立数据库连接。在Search.asp的开始处加入如下代码:
    <%
    Dim strProvider,CNN
        strProvider="Provider=Microsoft.Jet.OLEDB.4.0;Data Source="
        strProvider=strProvider & Server.MapPath("/") & "/data/db_Sample.mdb"  '假设数据库存放在主页根目录下的data目录下
        Set CNN = Server.CreateObject("ADODB.connection")
        CNN.Open strProvider  '打开数据库连接
    %>
    接下来,判断 ASP页所接收到的数据,并在数据库中进行搜索。
    <%
    Dim S_Key,RST,StrSQL
        S_Key = Trim(Request("key"))   '得到搜索关键字的值
        If S_Key <>"" then
            Set RST=Server.CreateObject("ADODB.RecordSet")
            StrSQL=AutoKey(S_Key)  '此处使用自定义函数 AutoKey(),该函数为实现智能搜索的核心
            RST.Open StrSQL,CNN,3,2  '得到搜索后的记录
  
            If RST.BOF And RST.EOF Then
    %>
                <font color="#FF0000">未找到任何结果!!!</font>
    <%
                Else
    %>
                    搜索名称为“<font color="#FF0000"><%= S_Key %></font>”的项,共找到 <font color="#FF0000"><%= RST.RecordCount %></font> 项:<p>
    <%
                    While Not RST.EOF   '遍历整个记录集,显示搜索到的信息并设置链接
    %>
                        <!-- 此处可设为你所需要的链接目标 -->
                        <font style="font: 12pt 宋体"><a href="info.asp?ID=<%= RST("ID") %>" target="_blank"><%= RST("U_Name") %></a></font><br> 
                        <!-- 显示部分详细内容 -->
                        <font style="font: 9pt 宋体"><%= Left(RST("U_Info"),150) %></font><p> 
    <%
                        RST.MoveNext
                    Wend
                    RST.Close
                    Set RST=Nothing
            End If
        End If
    %>
    在上面的代码中,有一个自定义函数 AutoKey ,该函数是实现智能搜索的核心所在。代码如下:
    <%
    Function AutoKey(strKey)
        CONST lngSubKey=2
        Dim lngLenKey, strNew1, strNew2, i, strSubKey
 
        '检测字符串的合法性,若不合法则转到出错页。出错页你可以根据需要进行设定。
        if InStr(strKey,"=")<>0 or InStr(strKey,"`")<>0 or InStr(strKey,"'")<>0 or InStr(strKey," ")<>0 or InStr(strKey," ")<>0 or InStr(strKey,"'")<>0 or InStr(strKey,chr(34))<>0 or InStr(strKey,"/")<>0 or InStr(strKey,",")<>0 or InStr(strKey,"<")<>0 or InStr(strKey,">")<>0 then
            Response.Redirect "error.htm"
        End If
 
        lngLenKey=Len(strKey)
        Select Case lngLenKey
            Case 0   '若为空串,转到出错页
                Response.Redirect "error.htm" 
            Case 1   '若长度为1,则不设任何值
                strNew1=""
                strNew2=""
            Case Else  '若长度大于1,则从字符串首字符开始,循环取长度为2的子字符串作为查询条件
                For i=1 To lngLenKey-(lngSubKey-1)
                    strSubKey=Mid(strKey,i,lngSubKey)
                    strNew1=strNew1 & " or U_Name like '%" & strSubKey & "%'"
                    strNew2=strNew2 & " or U_Info like '%" & strSubKey & "%'"
                Next
        End Select
 
        '得到完整的SQL语句
        AutoKey="Select * from T_Sample where U_Name like '%" & strKey & "%' or U_Info like '%" & strKey & "%'" & strNew1 & strNew2
 
    End Function
    %>
    要实现智能搜索,其核心就是将搜索关键字进行自动分组。在此处,我们使用了循环取长度为2的子串的方法。为什么不将子串长度定为1、3、4或其他呢?这是因为若子串长度小于2即为1时,会失去将关键字分组的功能,而若子串长度大于2,则会丢失一些词组。大家可以将 CONST lngSubKey=2改为其他数字试一试,孰优孰劣自见分晓。
    最后,别忘了将数据连接关闭,以释放资源。
    <%
        CNN.Close
        Set CNN=Nothing
    %>
    至此,这个智能搜索引擎已经完成了。你还可以将其继续完善,比如添加分页、突出显示等功能。好了,不耽误大家时间了,赶快去试一试吧。 ^_^

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
ASP.NET 搜索功能的实现可以分为以下几个步骤: 1. 创建搜索表单:在 ASP.NET 页面中,创建一个表单,用户可以在其中输入搜索关键词。 2. 处理搜索请求:在服务器端,使用 ASP.NET 的表单处理功能,获取用户输入的搜索关键词。 3. 查询数据库:使用 SQL 查询语句,在数据库中查找与搜索关键词匹配的记录。 4. 显示搜索结果:将查询结果显示在 ASP.NET 页面上,可以使用 GridView 或 Repeater 等控件。 下面是一个示例代码: ``` <asp:Content ID="Content1" ContentPlaceHolderID="MainContent" runat="server"> <h2>搜索</h2> <asp:TextBox ID="txtSearch" runat="server"></asp:TextBox> <asp:Button ID="btnSearch" runat="server" Text="搜索" OnClick="btnSearch_Click" /> <hr /> <asp:GridView ID="gvSearchResult" runat="server" AutoGenerateColumns="true" /> </asp:Content> public partial class Search : System.Web.UI.Page { protected void btnSearch_Click(object sender, EventArgs e) { string keyword = txtSearch.Text.Trim(); string connectionString = "Data Source=your_server;Initial Catalog=your_database;User ID=your_username;Password=your_password;"; string query = "SELECT * FROM your_table WHERE your_column LIKE '%" + keyword + "%';"; SqlConnection connection = new SqlConnection(connectionString); SqlCommand command = new SqlCommand(query, connection); SqlDataAdapter adapter = new SqlDataAdapter(command); DataTable dataTable = new DataTable(); adapter.Fill(dataTable); gvSearchResult.DataSource = dataTable; gvSearchResult.DataBind(); } } ``` 注意,这只是一个简单的示例,并没有考虑安全性和性能问题,实际应用中需要进行更多的优化和改进。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值