It is very convenient to use ASP to implement search engine functions, but how to implement intelligent search similar to 3721? For example, when you enter "Chinese people" in the search criteria box, keywords such as "China" and "people" are automatically extracted and searched in the database. After reading this article, you will find that this function is so simple to implement.
The first step is to create a database named db_sample.mdb (this article uses the Access2000 database as an example) and create the table T_Sample in it. Table T_Sample includes the following fields:
ID Automatic number U_Name Text U_Info Remarks
In the second step, we start designing the search page Search.asp. This page includes a form (Frm_Search), which includes a text box and a submit button. And set the method attribute of the form to "get" and the action attribute to "Search.asp", that is, submit it to the web page itself. The code is as follows:
Here is the code snippet:
<!-- Search.asp -->
<form name="frm_Search" method="get" action="Search.asp">
Please enter keywords:
<input type="text" name="key" size="10">
<input type="submit" value="search">
</form>
Next, we enter the key part of realizing intelligent search.
First, establish a database connection. Add the following code at the beginning of Search.asp:
Here is the code snippet:
<%
Dim strProvider,CNN
strProvider="Provider=Microsoft.Jet.OLEDB.4.0;Data Source="
strProvider=strProvider & Server.MapPath("") & "datadb_Sample.mdb" Assume that the database is stored in the data directory under the root directory of the homepage
Set CNN = Server.CreateObject("ADODB.connection")
CNN.Open strProvider opens the database connection
%>
Next, determine the data received by the ASP page and search it in the database.
Here is the code snippet:
<font color="#FF0000">No results found! ! ! </font>
<%
Else
%>
Search for items named "<font color="#FF0000"><%= S_Key %></font>", and a total of <font color="#FF0000"><%= RST.RecordCount %></font> is found Item: <p>
<%
While Not RST.EOF traverses the entire record set, displays the searched information and sets the link
%>
<!-- This can be set as the link target you need-->
<font style="font: 12pt 宋体"><a href="info.asp?ID=<%= RST("ID") %>" target="_blank"><%= RST("U_Name") % ></a></font>
<!--Show some details-->
<font style="font: 9pt 宋体"><%= Left(RST("U_Info"),150) %></font><p>
<%
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 core of realizing intelligent search. The code is as follows:
Here is the code snippet:
<%
Function AutoKey(strKey)
CONST lngSubKey=2
Dim lngLenKey, strNew1, strNew2, i, strSubKey
'Check the legality of the string, if not, go to the error page. You can set the error page according to your needs.
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 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 loop through the substring with length 2 as the query condition
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 realize intelligent search, the core is to automatically group search keywords. Here, we use the method of looping through a substring of length 2. Why not set the substring length to 1, 3, 4 or whatever? This is because if the substring length is less than 2, which is 1, the function of grouping keywords will be lost, and if the substring length is greater than 2, some phrases will be lost. You can try changing CONST lngSubKey=2 to other numbers and you will see for yourself which one is better.
Finally, don't forget to close the data connection to release resources.
Here is the code snippet:
<%
CNN.Close
Set CNN=Nothing
%>
At this point, this intelligent search engine has been completed. You can also continue to improve it, such as adding pagination, highlighting and other functions. Okay, I won’t waste everyone’s time, let’s go and give it a try.