Using ASP to implement the functions of search engines is very convenient, but how to implement intelligent search? After reading this article, you will find that it is actually very simple to implement this function. Let’s follow the editor of the Wrong New Technology Channel to learn more!
Steps to implement ASP intelligent search
The first step is to create a database called db_sample.mdb (this article uses Access2000 number
The database is used as an example), and create a table T_Sample in it. Table T_Sample includes the following fields:
ID automatic numbering
U_Name Text
U_Info Notes
In the second step, we start designing the search page Search.asp. This page includes a form
(Frm_Search), the form includes a text box and a submit button. And put the method of the form
Set the nature to "get" and the action attribute is set to "Search.asp", which means submitting it to the web page itself. The code is as follows
:
Please enter keywords:
Next, we enter the key part of implementing intelligent search.
First, establish a database connection. Add the following code to the beginning of Search.asp:
<%
Dim strProvider,CNN
strProvider="Provider=Microsoft.Jet.OLEDB.4.0;Data Source="
strProvider=strProvider & Server.MapPath("/") &
"/data/db_Sample.mdb" 'Suppose the database is stored in the data directory in the root directory of the home page
Set CNN = Server.CreateObject("ADODB.connection")
CNN.Open strProvider 'Open database connection
%>
Next, judge the data received by the ASP page and search in the database.
<%
Dim S_Key,RST,StrSQL
S_Key = Trim(Request("key")) 'Get the value of the search keyword
If S_Key <>"" then
Set RST=Server.CreateObject("ADODB.RecordSet")
StrSQL=AutoKey(S_Key) 'The custom function AutoKey() is used here, the function
Number is the core of intelligent search
RST.Open StrSQL,CNN,3,2 'Get the searched record
If RST.BOF And RST.EOF Then
%>
No results were found! ! !
<%
Else
%>
Search name is "<%= S_Key %
The item of >" was found in total <%= RST.RecordCount %
> Item:
<%
While Not RST.EOF 'Travel the entire record set to display the searched letters
and set up links
%>
href="info.asp?ID=<%= RST("ID") %>" target="_blank"><%= RST("U_Name")
%>
<%= Left(RST
("U_Info"),150) %> >
<%
RST.MoveNext
Wend
RST.Close
Set RST=Nothing
End If
End If
%>
In the above code, there is a custom function AutoKey, which is the kernel that implements intelligent search
Where the heart is. The code is as follows:
<%
Function AutoKey(strKey)
CONST lngSubKey=2
Dim lngLenKey, strNew1, strNew2, i, strSubKey
'Check the legality of the string, if it is not legal, go to the error page. You can do the error page as needed
Make settings.
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,"'")<>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 'If it is an empty string, go to the error page
Response.Redirect "error.htm"
Case 1 'If the length is 1, no value is set
strNew1=""
strNew2=""
Case Else 'If the length is greater than 1, start from the first character of the string and take the length as
2's substring as query conditions
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
'Get the complete SQL statement
AutoKey="Select * from T_Sample where U_Name like '%" & strKey
& "%' or U_Info like '%" & strKey & "%'" & strNew1 & strNew2
End Function
%>
To achieve intelligent search, the core is to automatically group search keywords. Here we make
The method of looping into substrings with length 2 is used. Why not set the substring length to 1, 3, 4 or something?
This is because if the substring length is less than 2, that is, 1, the function of grouping keywords will be lost, and if the substring length is
If greater than 2, some phrases will be lost. You can change CONST lngSubKey=2 to other numbers and try it
, it is clear which one is better or worse.
Finally, don't forget to turn off the data connection to free up resources.
<%
CNN.Close
Set CNN=Nothing
%>
The above is how to implement intelligent search. For more technical knowledge, please continue to pay attention to the wrong new technology channel!