Home wilkeo>JavaScript    
  Search Engine Base code focusing on a Query Parser

(Search Engine Query Parser) this is a very hard application to cover in a short Tutorial, but I will cover one of two main issues. A Search Engine does two things. It receives Input from the Client Browser and parses it into word groups, removes spaces, and other string functions. Then it evaluates any Boolean rules it finds in the . The second part of the picture is when the Search Engine does the search (Easy) and presents the Data back to the Client Browser in format that is easy to read. Lets take a look at the Query Parsing...

Issues:This is an advanced article assuming you have a very good grasp on ASP and a fair handle on Database Queries. It is written as a Function for simplicity of implementation.

Logic Flow:

Get data from Client Browser called <search_text>
<form action="search_results.asp" method="post">
The Search Text will be transmitted in the form of an HTML Post.
<input type="text" name="SearchText">
We need a Submit Button to initiate the Data Transfer
<input type="submit" name="submit" value="submit">
</form>
The ASP page receives this data with:
SearchText = Request("SearchText")
Now comes the hard part, how do you run a SQL query with an input of "cameras and "flash photography"
You cant just feed that in a SQL query with a few wild cards, you must first parse it two ways.
1. Separate into Word Groups
2. Process Boolean Operators. i.e. AND, OR, +, etc.

Number_Of_Words_All = Cdbl ( UBound (Word_All, 1)) + 1
Dim Word()
C = C + 1
ReDim Preserve Word(C)
Dim C
C = 1
For I = 0 to Number_Of_Words_All - 1
Select Case Instr (1, Word_All(I), """" , 1)
Case 1 'If there is a Quote in this word it must be part of a word group
If InStrRev (Word_All(I), """" , -1, 1) > 1 Then Quote_Hold = 1 'Flag more words.
Word_All(I) = Replace (Word_All(I), """" , "" , 1, -1, 1) 'Remove the quotes
Word(C) = Word(C) & Word_All(I) & " " 'Add the Current word to the Output Array "word"
'If there was not a Quote then the next word will be on its own
If Quote_Hold <> 1 Then Quote = "Open"
Quote_Hold = 0
'This case is asking if the Quote was at the end of the word
Case Len (Word_All(I))
Word_All(I) = Replace (Word_All(I), """" , "" , 1, -1, 1) 'Remove the quotes
Word(C) = Word(C) & Word_All(I) 'Add the Current word to the Output Array "word"
'If it was the last word in a Open Quote, close the quote
Quote = "Closed"
Case Else
'Handle Quotes found in your search string
Select Case Quote
Case "Open"
Word(C) = Word(C) & Word_All(I) & " "
'Response.Write "Quote-Open Word(" & C & ") =" & Word(C) & "<br>"
Case Else
Word(C) = Word_All(I)
'Response.Write "Regular Word(" & C & ") =" & Word(C) & "<br>"
C = C + 1
ReDim Preserve Word(C)
End Select
End Select
Next
'Generate SQL Query dynamically
Number_Of_Words = Cdbl ( UBound (Word, 1))
AND_OR = "OR"
For I = 1 to Number_Of_Words
Word(I) = Trim (Word(I))
If Word(I) <> "" Then
'Response.Write "I=" & I & "<br>"
sQ = sQ & "(Country Like '%" & Word(I) & "%' OR CompanyName Like '%" & Word(I) & "%') "
If Number_Of_Words > 0 and I < Number_Of_Words AND Word(I + 1) <> "" Then
Select Case Word(I+1)
Case "And" , "and" , "AND" , "+"
sQ = sQ & " AND"
Not_Like = ""
AND_OR = "OR"
I = I + 1
Case "Or", "or", "OR", "-"
sQ = sQ & " OR"
Not_Like = ""
AND_OR = "OR"
I = I + 1
Case "Not", "not", "NOT", "<>"
sQ = sQ & " AND"
Not_Like = "NOT "
AND_OR = "AND"
I = I + 1
Case Else
sQ = sQ & " OR"
AND_OR = "OR"
Not_Like = ""
End Select
End If 'and Word(I) <> ""
End If 'Number Of Words > 0
Next
End If
'Return SQL to Function
Parse_Query = sQ
End Function
%>

<%
'Create DSN Less connection to Access Database
'Create DBConnection Object
Set DBConnection = Server. CreateObject ( "adodb.connection" )
DSN = "DRIVER={Microsoft Access Driver (*.mdb)}; "
DSN = DSN & "DBQ=" & Server.Mappath("fpdb/nwind.mdb")
DSN = DSN & ";PWD=aspsamples"
DBConnection.Open DSN
Response.Write "Database Open <br>"
If Request( "SearchText" ) <> "" Then
'Create a Query
SQL = "SELECT * FROM Customers WHERE CustomerID <> '' " & Parse_Query(Request( "SearchText" ))
Response.Write "SQL=" & SQL & "<br>"
'Get a Record Set
Set RS = DBConnection.Execute(SQL)
Select Case RS.eof
Case False
RSArray = RS.getrows
'RSArray(Field, Record)
Number_Of_Fields = Cdbl ( UBound (RSArray, 1))
Number_Of_Records = Cdbl ( UBound (RSArray, 2))
%>
<table border = "1" width = "300" >

<%
For R = 0 to Number_Of_Records
Response.Write "<tr>"
For F = 0 to Number_Of_Fields
Response.Write "<td> " & RSArray(F, R) & "</td> "
Next
Response.Write "</tr>"
Next
Response.Write "</table>"
Case True
Response.Write "No Records were found"
End Select
'Destroy Record Set (RS)
Set RS = Nothing
Response.Write "<br>Database Closed <br>"
End If
%>

<form action = ' <%=Request.ServerVariables("URL")%> ' method = "POST" >
<p> Please enter your SEARCH criteria: <input type = "SearchText" size = "20" name = "SearchText" >
ie Mexico or ASPSamples </p>
<p><input type = "submit" value = "Submit" ></p>
</form>