By Scott Mitchell
As a web site grows, finding content on the site becomes increasingly difficult. To combat the difficulty
of finding relevant information on a large site, many developers turn to writing a search engine for their
site. This article discusses how to implement such a system using Active Server Pages and SQL Server.
There are two "types" of search engines. Both take a search string from the user to begin, but what,
exactly, they search differs. A completely dynamic search engine for a completely dynamic web site will
hit a database table which ties an article URL to the articles description. The database can then compare
the user's search request to the descriptions of the available articles and return the relevant URLs.
Another approach is to do an actual text search through each of the files. For example, say that the user
searched for "Microsoft." Your search engine would then look through all of your HTML files and return the
URLs of those which had the word "Microsoft" somewhere in the document. Such a system is used for this web
site's search engine. In my opinion, it is much easier to write such a system described in Perl (which
this system is written in), than in Active Server Pages; however, it is quite possible to write a text-
finding search system in ASP.
In this article I plan to implement the former search engine, the dynamic search engine. For this example
I will make a table called ArticleURL, which will have the following definition:
ArticleURL
ArticleURLID int PK
URL varchar(255)
Title varchar(100)
Description varchar(255)
Now that we've got our table definition, let's look at how our web visitors will enter their queries.
Search Querying
A search engine is rather useless unless queries can be made, and the results are returned. Let's examine
how we will code the first needed part, the user search requests. All we will need is a simple HTML FORM
which takes input from the user and passes it on to an ASP page. Here is an example of a file we'll call
SearchStart.htm:
<HTML>
<BODY>
<FORM METHOD=POST ACTION="Search.asp&ID=0">
> Search for: <INPUT TYPE=TEXT NAME="txtSearchString" SIZE="50">
<P>
<INPUT TYPE=SUBMIT>
</FORM>
</BODY>
</HTML>
This, of course, is not a pretty HTML page, but its functionality is there. There are many things which
could be done to enhance this page. It is recommended that JavaScript functions be present to make sure
the user is searching something (i.e. not just clicking Submit when there is no search string).
Now that we have the Query, we need to look at the second phase of any search engine: retrieving the data
and presenting it to the user. Here is where the real fun begins!
Retrieving the Data and Presenting It:
Our ASP page Search.asp must do a few steps. First, it must parse the FORM variable txtSearchString. Right
now, I am assuming that each word in the txtSearchString separated by a space will be ANDed together. You
can alter this (have it ORed), or, to make it more professional, you can give the user the option of which
boolean to put inbetween each spaced word.
Next, Search.asp will need to hit the database table ArticleURL and return the data in a user-friendly
fashion. Also, we will want to display the results only 10 records at a time, so logic will need to be
implemented to handle this as well. Let's look at some code.
<%
'Connect to Database
Dim Conn
Set Conn = Server.CreateObject("ADODB.Connection")
Conn.Open Application("MyConnectString")
'Set these