Implementation of ASP Intelligent Search

zhaozj2021-02-16  164

It is very convenient to use ASP to implement the function of the search engine, but how do I achieve smart search similar to 3721? For example, when entering the "Chinese people" in the search condition box, the keyword "China", "People" and "People" are automatically extracted and search in the database. After reading this article, you can find that this feature is actually so simple. OK, FOLLOW ME!

The first step, we have to build a database called DB_SAMPLE.MDB (this article is used as an example of Access2000 database) and sets table t_sample. Table T_SAMPLE includes the following fields:

ID automatic number

U_NAME text

U_INFO Remarks

In the second step, we start designing search page search.asp. This page includes a form (fRM_SEARCH), including a text box and a submission button. And set the form of the form to "get", the Action property is set to "Search.asp", that is, submit it to the web. code show as below:

Please enter key words:

Below, you have entered a key portion of your intelligent search.

First, establish a database connection. Add the following code at the beginning of Search.asp:

<%

DIM STRPROVIDER, CNN

Strovider = "provider = microsoft.jet.Oledb.4.0; data source ="

Strovider = strprovider & server.mappath ("/") & "/data/db_sample.mdb" assumes that the data inventory is placed in the Data directory under the home page root directory

SET CNN = Server.createObject ("AdoDb.Connection")

CNN.Open StrProvider 'Opens Database Connection

%>

Next, determine 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 <> "" ""

SET RST = Server.createObject ("AdoDb.Recordset")

Strsql = autoKey 'uses a custom function autoKey () here, which is the core of intelligence search.

Rst.Open Strsql, CNN, 3, 2 'get a search after search

IF RST.BOF AND RST.EOF THEN

%>

No results found! ! !

<%

Else

%>

Search name " <% = s_key%> " found, a total of <% = RST (" u_name ")% >

<% = Left (RST ("U_INFO"), 150)%>

<%

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 of achieving intelligent search. code show as below:

<%

Function AutoKey (StrKey)

Const lcomubkey = 2

Dim Lnglenkey, Strnew1, Strnew2, I, StrsubKey

'Detecting the legality of the string, if you don't legal, go to the error page. Error page You can set 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 a space string, go to an error page

Response.Redirect "error.htm"

If the Case 1 'is 1, then no value is set.

Strnew1 = "" "

Strnew2 = "" "

If the length is greater than 1, then from the string first character, the sub-string of the loop length is 2 as a query condition.

For i = 1 to lnglenkey- (LNGSUBKEY-1) strsubkey = mid (strkey, i, lcomubkey)

Strnew1 = strnew1 & "or u_name like '%" & strsubkey & "%'"

Strnew2 = strnew2 & "OR U_INFO LIKE '%" & StrsubKey & "%'"

NEXT

End SELECT

'Get full SQL statement

AutoKey = "SELECT * from t_sample where u_name like '%" & strkey & "%' or u_info like '%" & strkey & "%'" & strnew1 & strnew2

END FUNCTION

%>

To achieve a smart search, its core is to automatically packet search keywords. Here, we use a method of recirculating a substring having a length of 2. Why don't you set a substring length to 1, 3, 4 or others? This is because if the seasse string length is less than 2, it will lose the function of the keyword packet, while the son string is greater than 2, and some phrase is lost. Everyone can change the const lcomubkey = 2 to other numbers to try a try, and the exception is derived.

Finally, don't forget to turn the data connection to release resources.

<%

CNN.Close

SET CNN = Nothing

%>

At this point, this smart search engine has been completed. You can also continue to improve, such as adding paging, highlighting. Ok, don't delay everyone's time, hurry to try it.

转载请注明原文地址:https://www.9cbs.com/read-8348.html

New Post(0)