Using ASP to build a simple search engine

Source: Internet
Author: User
Tags definition html form html page table definition
Search Engine 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 you to implement such a system using Active Server Pages and SQL Server.

There are two "types" 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 would
Hit a database table which ties a 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 a actual text search through each of the files. For example, say the user
Searched for "Microsoft." Your search engine would then look through all of Your HTML files and return the
The URL 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's much easier to write such a system described in Perl (which
This 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 implement the former search engine, the dynamic search engine. For this example
I'll make a table called Articleurl, which would have the following definition:


Articleurl
articleurlid int PK
URL varchar (255)
Title varchar (100)
Description varchar (255)

Now so we ' ve got our table definition, let's look in how we web visitors'll enter their queries.

Search querying
A search engine is rather useless unless queries can be made, and the results are. Let ' s examine
How we'll code the the needed part, the user search requests. All we need are a simple HTML FORM
Which takes input from the user and passes it in to a ASP page. This is a 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= ">"
<P>
<input type=submit>
</FORM>

</BODY>
</HTML>
This, of course, are not a pretty HTML page, but it functionality is there. There are many things which
Could to enhance this page. It is recommended that JavaScript functions being present to make sure
The user is searching something (i.e. don't just clicking Submit when there is no search string).

Now so we have the Query, we need to look at the second phase of the any search engine:retrieving the data
and presenting it to the user. Here's where the real fun begins!

Retrieving the Data and presenting It:
Our ASP page search.asp must do a few steps. The It must parse the FORM variable txtsearchstring. Right
Now, I am assuming this each word in the txtsearchstring separated by a space would be anded together. You
Can alter this (have it ored), or, to make it more professional, can give the user the option of which
The Boolean to put inbetween each spaced word.

Next, search.asp'll need to hit the database table Articleurl and return the data in a user-friendly
Fashion. Also, we'll want to display the results is records at a time, so logic'll need to be
Implemented to handle this as. Let ' s look at some code.


<%

' Connect to Database '
Dim Conn
Set Conn = Server.CreateObject ("ADODB. Connection ")
Conn.Open Application ("Myconnectstring")

' Set these up to your preference
Defaultboolean = "and"
Recordsperpage = 10

' Get our form variable
Dim Strsearch
Strsearch = Request.Form ("txtsearchstring")

' Get my current ID. This let's us know where we are Dim ID
id = request.querystring ("id")

' Set up our SQL Statement
Dim strSQL, Tmpsql
strSQL = "SELECT * from Articleurl WHERE"
Tmpsql = "(Description like")

' OK, we need to parse we have a string here
Dim Pos
Pos = 1
While Pos > 0
Pos = INSTR (1, Strsearch, "")
If Pos = 0 Then
' We have hit the end
Tmpsql = Tmpsql &a



Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.