It is very convenient to use ASP to implement search engine functions. However, how to implement Smart Search similar to 3721
What about suo? For example, if you enter "Chinese people" in the Search Condition box, the system automatically extracts "China ",
Search for keywords such as "people" in the database. After reading this article, you can find that
It is so easy to implement. OK, Follow Me!
Step 1: Create a database named db_sample.mdb.
Database), and create the T_Sample table. Table T_Sample includes the following fields:
ID automatic number
U_Name text
U_Info remarks
Step 2: design the Search page Search. asp. This page contains a form
(Frm_Search). The form contains a text box and a submit button. And the form method is
Is set to "get", and the action attribute is set to "Search. asp", that is, submitted to the webpage itself. The Code is as follows:
:
<! -- Search. asp -->
<Form name = "frm_Search" method = "get" action = "Search. asp">
Enter keywords:
<Input type = "text" name = "key" size = "10">
<Input type = "submit" value = "Search">
</Form>
Next, we enter the key part for implementing intelligent search.
First, establish a database connection. Add the following code at 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" 'assume that the data inventory is stored in the data directory under the root directory of the home page.
Set CNN = Server. CreateObject ("ADODB. connection ")
CNN. Open strProvider Open the 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") 'to get the search keyword Value
If S_Key <> "" then
Set RST = Server. CreateObject ("ADODB. RecordSet ")
StrSQL = AutoKey (S_Key) 'Use the UDF AutoKey ().
Number of cores for Smart Search
RST. Open StrSQL, CNN, 3, 2 'get the record after search
If RST. BOF And RST. EOF Then
%>
<Font color = "# FF0000"> no results found !!! </Font>
<%
Else
%>
The search name is "<font color =" # FF0000 "> <% = S_Key %
> </Font> ", <font color =" # FF0000 "> <% = RST. RecordCount %
> </Font> item: <p>
<%
While Not RST. EOF 'traverses the entire record set and displays the searched messages
Information and set links
%>
<! -- This can be set as the link target you need -->
<Font style = "font: 12pt "> <
Href = "info. asp? ID = <% = RST ("ID") %> "target =" _ blank "> <% = RST (" U_Name ")
%> </A> </font> <br>
<! -- 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 code above, there is a custom function AutoKey, which is the core for Smart Search.
Heart. The Code is as follows:
<%
Function AutoKey (strKey)
CONST lngSubKey = 2
Dim lngLenKey, strNew1, strNew2, I, strSubKey
'Check the validity of the string. If not, go to the error page. Error page as needed
.
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, it starts from the first character of the string, and the Loop Length is
The substring of 2 is used 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
%>
The core of Smart Search is to automatically group search keywords. Here, we make
This method uses a substring of 2 in a loop. Why not set the length of a substring to 1, 3, 4, or another?
This is because if the length of a substring is less than 2, that is, 1, the function of grouping keywords is lost.
If the value is greater than 2, some phrases are lost. You can change CONST lngSubKey = 2 to another number.
, Which is better than your own opinion.
Finally, do not forget to close the data connection to release the resource.
<%
CNN. Close
Set CNN = Nothing
%>
So far, this intelligent search engine has been completed. You can continue to improve it, such as adding points
Page, highlight, and other functions. Well, it won't delay everyone's time. Please try it now. Pai_^