When building a website, the boss asked me to implement a search function like Baidu. In the past, ASP implemented fuzzy search and matching queries. no way. Searching the Internet, I found that there are several methods provided on the Internet. One is to build a thesaurus, the other is to use word segmentation components, and the third is what I am talking about, automatic word segmentation technology, but it is not an intelligent word segmentation, it is just Match every other word. The best thing is to build a vocabulary and expand it based on user input, but for small and medium-sized websites, this may not be necessary.
This is the third type of word segmentation search I am talking about. The code is as follows: I won’t go into how to use it.
<%
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
%>