欢迎光临
我们一直在努力

用ASP建立一个简单的搜索引擎

建站超值云服务器,限时71元/月

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 users 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

sites 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 weve got our table definition, lets 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. lets 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 well 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. lets 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 our current id. this lets 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 our string here

dim pos

pos = 1

while pos > 0

pos = instr(1, strsearch," ")

if pos = 0 then

we have hit the end

tmpsql = tmpsql & "%" & strsearch & "%)"

else

tmpsql = tmpsql & "%" & mid(strsearch,1,pos) & "% " & defaultboolean & " description like "

strsearch = mid(strsearch,pos+1,len(strsearch))

end if

wend

now, weve got to make sure we only get the right records

strsql = strsql & tmpsql & " and articleurlid > " & id

strsql = strsql & " order by id" important!

make our recordset variable and get the results

dim rsresults

set rsresults = server.createobject("adodb.recordset")

get the right number of records per page

rsresults.maxrecords = recordsperpage

set our recordset properties (include adovbs.inc for the constant definitions!)

rsresults.cursortype = adforwardonly

get our data

rsresults.open strsql

ok, weve got the data, lets display it in html

first, though, lets get the total number of records

dim rstotalrecords

strsql = "select count(*) from articleurl where " & tmpsql

set rstotalrecords = conn.execute(strsql)

we also need the max id value for our search dim rsmaxid

>strsql = "select max(articleurlid) from articleurl where " & tmpsql

set rsmaxid = conn.execute(strsql)

%>

<html>

<body>

<% if rsresults.eof then no matches found

%>

no matches found! try broadening your search criteria.<p>

<a href="searchstart.htm">return to search</a>

<% else

dim icurrentid

while not rsresults.eof

icurrentid = rsresults("articleurlid") %>

<a href="<%=rsresults("url")%>"> <%=rsresults("title")%></a>

<%=rsresults("description")%>

<% rsresults.movenext

wend %>

<p>

<%=rstotalrecords(0)%> found!<br>

<% if icurrentid < rsmaxid(0) then %>

<!– we have at least another record… –>< br><form method=postaction="search.asp?id=<%=icurrentid%>">

<input type=hidden name="txtsearchstring" value="<%=request.form("strsearchstring")%>">

<input type=submit value="next">

</form>

<% end if

end if end if for .eof clause above %>

</body>

</html>

note: please forgive me if there are many errors or typos. i wrote this code while writing this article.

it has not been fully tested. in theory it should work. more important than running source code are the

ideas behind the code. source code is a mere transformation of ideas into something a computer can

understand. if you truly understand the ideas, the code should write itself.

hopefully you can understand what this code is doing. this file, search.asp, will be called the first time

a search is executed and each time the user wants to view the next n records. to start out, the file gets

the search string and the current id. the current id is an important value, it tells this page which

records weve already seen. the sql searches for records who have an articleurlid greater than the passed

in id. to start off, we pass in an id of 0, so all records (assuming articleurlid was set as an identity

(1,n))) will be included.

next we parse out our search string into a string variable called tmpsql. if the user searched on "magnum

p i", tmpsql would contain (description like %magnum% and description like %p% and description like %

i%). we then add to our where clause articleurlid > id, where id is the id we pass into search.asp.

next, we create an instance of an ado recordset object, and set the maxrecords property to n, where n is

the number of rows we want to display per page. this will only return n records to our recordset object.

finally we get the total number of records which match our search criteria and the maximum id which

matches our criteria. we need the maximum id to determine if we are currently on the last recordset. once

we have all of this data we are ready to display our information.

we start out by seeing if we have any information in the first place! youll not the if rsresults.eof

then. if no records are found then we inform the user that we could find no results and provide a link

back to the searchstart.htm page from which they came. if, however, rsresults is not empty, we iterate

through the recordset. we then check to see if our last articleurlid is less than the maximum id. if it

is, then we know we have at least one more record to show, so we display the "next" button which will

display the next n records.

areas for improvement:

as im sure you can note, this search engine solution leaves a lot to be desired as far as functionality

goes when we compare it to standard internet search engines. for example, there is no back button, only a

forward. also, you cannot do any complex boolean searches, such as: "microsoft and active server pages

and not (vbscript or jscript)". these can both be accomplished, though!

personally, i have written a parser which accepted complex boolean searches similar to the one shown above

and transformed it into a sql where clause. to implement a back feature, i would recommend a dynamic array

(or stack). you would need to put this in a session-level variable. each time the user hits next, you will

want to push the request.querystring("id") onto the stack. when they hit the "back" button youll want to

pop the last id off the stack and pass it as id to search.asp.

conclusion:

in this article weve examined how to implement a simplistic dynamic search engine using active server

pages and sql server. while the model implemented in this article is not exactly "feature-ful," it does

search, and presents the basic ideas behind a search engine. without major modifications, this system

could be transformed into a very impressive, professional looking search engine.

happy programming!

赞(0)
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com 特别注意:本站所有转载文章言论不代表本站观点! 本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。未经允许不得转载:IDC资讯中心 » 用ASP建立一个简单的搜索引擎
分享到: 更多 (0)