ASP智能搜尋的實現

來源:互聯網
上載者:User
用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時,會失去將關鍵



相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.