ASP智能搜索的實現(xiàn)
更新時間:2006年08月06日 00:00:00 作者:
用ASP實現(xiàn)搜索引擎的功能是一件很方便的事,可是,如何實現(xiàn)類似3721的智能搜
索呢?比如,當在搜索條件框內(nèi)輸入“中國人民”時,自動從中提取“中國”、
“人民”等關(guān)鍵字并在數(shù)據(jù)庫內(nèi)進行搜索??赐瓯疚暮?,你就可以發(fā)現(xiàn),這個功
能實現(xiàn)起來竟然是如此的簡單。OK,F(xiàn)ollow Me!
第一步,我們要建立一個名為db_sample.mdb的數(shù)據(jù)庫(本文以Access2000數(shù)
據(jù)庫為例),并在其中建立表T_Sample。表T_Sample包括如下字段:
ID 自動編號
U_Name 文本
U_Info 備注
第二步,我們開始設(shè)計搜索頁面Search.asp。該頁面包括一個表單
(Frm_Search),表單內(nèi)包括一個文本框和一個提交按鈕。并將表單的method屬
性設(shè)為“get” ,action屬性設(shè)為“Search.asp",即提交給網(wǎng)頁自身。代碼如下
:
<!-- Search.asp -->
<form name="frm_Search" method="get" action="Search.asp">
請輸入關(guān)鍵字:
<input type="text" name="key" size="10">
<input type="submit" value="搜索">
</form>
下面,就進入了實現(xiàn)智能搜索的關(guān)鍵部分。
首先,建立數(shù)據(jù)庫連接。在Search.asp的開始處加入如下代碼:
<%
Dim strProvider,CNN
strProvider="Provider=Microsoft.Jet.OLEDB.4.0;Data Source="
strProvider=strProvider & Server.MapPath("\") &
"\data\db_Sample.mdb" '假設(shè)數(shù)據(jù)庫存放在主頁根目錄下的data目錄下
Set CNN = Server.CreateObject("ADODB.connection")
CNN.Open strProvider '打開數(shù)據(jù)庫連接
%>
接下來,判斷 ASP頁所接收到的數(shù)據(jù),并在數(shù)據(jù)庫中進行搜索。
<%
Dim S_Key,RST,StrSQL
S_Key = Trim(Request("key")) '得到搜索關(guān)鍵字的值
If S_Key <>"" then
Set RST=Server.CreateObject("ADODB.RecordSet")
StrSQL=AutoKey(S_Key) '此處使用自定義函數(shù) AutoKey(),該函
數(shù)為實現(xiàn)智能搜索的核心
RST.Open StrSQL,CNN,3,2 '得到搜索后的記錄
If RST.BOF And RST.EOF Then
%>
<font color="#FF0000">未找到任何結(jié)果?。?!</font>
<%
Else
%>
搜索名稱為“<font color="#FF0000"><%= S_Key %
></font>”的項,共找到 <font color="#FF0000"><%= RST.RecordCount %
></font> 項:<p>
<%
While Not RST.EOF '遍歷整個記錄集,顯示搜索到的信
息并設(shè)置鏈接
%>
<!-- 此處可設(shè)為你所需要的鏈接目標 -->
<font style="font: 12pt 宋體"><a
href="info.asp?ID=<%= RST("ID") %>" target="_blank"><%= RST("U_Name")
%></a></font><br>
<!-- 顯示部分詳細內(nèi)容 -->
<font style="font: 9pt 宋體"><%= Left(RST
("U_Info"),150) %></font><p>
<%
RST.MoveNext
Wend
RST.Close
Set RST=Nothing
End If
End If
%>
在上面的代碼中,有一個自定義函數(shù) AutoKey ,該函數(shù)是實現(xiàn)智能搜索的核
心所在。代碼如下:
<%
Function AutoKey(strKey)
CONST lngSubKey=2
Dim lngLenKey, strNew1, strNew2, i, strSubKey
'檢測字符串的合法性,若不合法則轉(zhuǎn)到出錯頁。出錯頁你可以根據(jù)需要
進行設(shè)定。
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 '若為空串,轉(zhuǎn)到出錯頁
Response.Redirect "error.htm"
Case 1 '若長度為1,則不設(shè)任何值
strNew1=""
strNew2=""
Case Else '若長度大于1,則從字符串首字符開始,循環(huán)取長度為
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
%>
要實現(xiàn)智能搜索,其核心就是將搜索關(guān)鍵字進行自動分組。在此處,我們使
用了循環(huán)取長度為2的子串的方法。為什么不將子串長度定為1、3、4或其他呢?
這是因為若子串長度小于2即為1時,會失去將關(guān)鍵字分組的功能,而若子串長度
大于2,則會丟失一些詞組。大家可以將 CONST lngSubKey=2改為其他數(shù)字試一試
,孰優(yōu)孰劣自見分曉。
最后,別忘了將數(shù)據(jù)連接關(guān)閉,以釋放資源。
<%
CNN.Close
Set CNN=Nothing
%>
至此,這個智能搜索引擎已經(jīng)完成了。你還可以將其繼續(xù)完善,比如添加分
頁、突出顯示等功能。好了,不耽誤大家時間了,趕快去試一試吧。 ^_^
索呢?比如,當在搜索條件框內(nèi)輸入“中國人民”時,自動從中提取“中國”、
“人民”等關(guān)鍵字并在數(shù)據(jù)庫內(nèi)進行搜索??赐瓯疚暮?,你就可以發(fā)現(xiàn),這個功
能實現(xiàn)起來竟然是如此的簡單。OK,F(xiàn)ollow Me!
第一步,我們要建立一個名為db_sample.mdb的數(shù)據(jù)庫(本文以Access2000數(shù)
據(jù)庫為例),并在其中建立表T_Sample。表T_Sample包括如下字段:
ID 自動編號
U_Name 文本
U_Info 備注
第二步,我們開始設(shè)計搜索頁面Search.asp。該頁面包括一個表單
(Frm_Search),表單內(nèi)包括一個文本框和一個提交按鈕。并將表單的method屬
性設(shè)為“get” ,action屬性設(shè)為“Search.asp",即提交給網(wǎng)頁自身。代碼如下
:
<!-- Search.asp -->
<form name="frm_Search" method="get" action="Search.asp">
請輸入關(guān)鍵字:
<input type="text" name="key" size="10">
<input type="submit" value="搜索">
</form>
下面,就進入了實現(xiàn)智能搜索的關(guān)鍵部分。
首先,建立數(shù)據(jù)庫連接。在Search.asp的開始處加入如下代碼:
<%
Dim strProvider,CNN
strProvider="Provider=Microsoft.Jet.OLEDB.4.0;Data Source="
strProvider=strProvider & Server.MapPath("\") &
"\data\db_Sample.mdb" '假設(shè)數(shù)據(jù)庫存放在主頁根目錄下的data目錄下
Set CNN = Server.CreateObject("ADODB.connection")
CNN.Open strProvider '打開數(shù)據(jù)庫連接
%>
接下來,判斷 ASP頁所接收到的數(shù)據(jù),并在數(shù)據(jù)庫中進行搜索。
<%
Dim S_Key,RST,StrSQL
S_Key = Trim(Request("key")) '得到搜索關(guān)鍵字的值
If S_Key <>"" then
Set RST=Server.CreateObject("ADODB.RecordSet")
StrSQL=AutoKey(S_Key) '此處使用自定義函數(shù) AutoKey(),該函
數(shù)為實現(xiàn)智能搜索的核心
RST.Open StrSQL,CNN,3,2 '得到搜索后的記錄
If RST.BOF And RST.EOF Then
%>
<font color="#FF0000">未找到任何結(jié)果?。?!</font>
<%
Else
%>
搜索名稱為“<font color="#FF0000"><%= S_Key %
></font>”的項,共找到 <font color="#FF0000"><%= RST.RecordCount %
></font> 項:<p>
<%
While Not RST.EOF '遍歷整個記錄集,顯示搜索到的信
息并設(shè)置鏈接
%>
<!-- 此處可設(shè)為你所需要的鏈接目標 -->
<font style="font: 12pt 宋體"><a
href="info.asp?ID=<%= RST("ID") %>" target="_blank"><%= RST("U_Name")
%></a></font><br>
<!-- 顯示部分詳細內(nèi)容 -->
<font style="font: 9pt 宋體"><%= Left(RST
("U_Info"),150) %></font><p>
<%
RST.MoveNext
Wend
RST.Close
Set RST=Nothing
End If
End If
%>
在上面的代碼中,有一個自定義函數(shù) AutoKey ,該函數(shù)是實現(xiàn)智能搜索的核
心所在。代碼如下:
<%
Function AutoKey(strKey)
CONST lngSubKey=2
Dim lngLenKey, strNew1, strNew2, i, strSubKey
'檢測字符串的合法性,若不合法則轉(zhuǎn)到出錯頁。出錯頁你可以根據(jù)需要
進行設(shè)定。
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 '若為空串,轉(zhuǎn)到出錯頁
Response.Redirect "error.htm"
Case 1 '若長度為1,則不設(shè)任何值
strNew1=""
strNew2=""
Case Else '若長度大于1,則從字符串首字符開始,循環(huán)取長度為
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
%>
要實現(xiàn)智能搜索,其核心就是將搜索關(guān)鍵字進行自動分組。在此處,我們使
用了循環(huán)取長度為2的子串的方法。為什么不將子串長度定為1、3、4或其他呢?
這是因為若子串長度小于2即為1時,會失去將關(guān)鍵字分組的功能,而若子串長度
大于2,則會丟失一些詞組。大家可以將 CONST lngSubKey=2改為其他數(shù)字試一試
,孰優(yōu)孰劣自見分曉。
最后,別忘了將數(shù)據(jù)連接關(guān)閉,以釋放資源。
<%
CNN.Close
Set CNN=Nothing
%>
至此,這個智能搜索引擎已經(jīng)完成了。你還可以將其繼續(xù)完善,比如添加分
頁、突出顯示等功能。好了,不耽誤大家時間了,趕快去試一試吧。 ^_^
相關(guān)文章
access數(shù)據(jù)庫的一些少用操作,ASP,創(chuàng)建數(shù)據(jù)庫文件,創(chuàng)建表,創(chuàng)建字段,ADOX
access數(shù)據(jù)庫的一些少用操作,ASP,創(chuàng)建數(shù)據(jù)庫文件,創(chuàng)建表,創(chuàng)建字段,ADOX...2006-10-10.NET?Core?分布式任務(wù)調(diào)度ScheduleMaster詳解
這篇文章主要介紹了分布式任務(wù)調(diào)度ScheduleMaster,集中任務(wù)調(diào)度系統(tǒng),最簡單的理解ScheduleMaster,就是對不同的系統(tǒng)里面的調(diào)度任務(wù)做統(tǒng)一管理的框架,本文通過圖文實例代碼相結(jié)合給大家介紹的非常詳細,需要的朋友可以參考下2022-05-05ASP.NET Core整合Zipkin鏈路跟蹤的實現(xiàn)方法
這篇文章主要介紹了ASP.NET Core整合Zipkin鏈路跟蹤,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-09-09