服务器|索引
使用索引服务器的对象
你可以象使用其它数据库一样使用索引服务器,也就是Index Server,它是IIS4中包含的内容索引引擎。你可以对它进行ADO调用并搜索你的站点,它为你提供了一个很好的web 搜索引擎。
它非常容易使用。你只需要声明对象,然后对引擎进行调用,然后引用你所请求的属性、栏和值。
那么,怎样做以上这些工作呢?
设置提示用户进行输入的表单页面,这个页面将被传递到索引服务器引擎。设置查询引擎的页面并显示结果测试。
下面来看看swynk.com 在左栏搜索工具中使用的代码。
创建表单
这个表单非常简单。这是你的基本HTML表单,增加了一些隐含域,这些域将帮助驱动某些搜索引擎特性。
<form method="get" action="/sitesearch/sitesearch.asp" id=form1 name=form1>
<!-- search text -->
<table width="100%">
<tr width="100%" bgcolor="Black">
<td width="100%">
<font color="White">
<b>Search</b>
</font>
</td>
</tr>
</table>
<input type="text" name="qu" size="20"><br>
<!-- max hits -->
<input type="hidden" name="mh" value=50>
<!-- sort by hits -->
<input type="hidden" name="sd" value="Hitcount">
<!-- allow enumeration -->
<input type="hidden" name="ae" value="1">
<!-- catalog -->
<input type="hidden" name="ct" value="c:\inetpub\www07120\index\">
<input type="submit" value="Find It!" name="Search"><a href="/sitesearch/searchhelp.asp"><font size=-1>Help</a></font>
</form>
如果你将设置的表单与你在这个页面左边看到的搜索相比较,你就能发现域是从那里引入的,“找到了”按钮在哪里显示。以下是域的简要解释:
◆ mh是你想要返回的点击数的最大值。例如,你可能愿意将这个值设为200。将这个值设置得太高会给服务器带来不必要的负担。如果这个值太低就不能返回足够多的有意义的结果。要确定你的站点的最佳值需要花费一些脑筋(这种情况听起来有点象SQL中的"Set rowcount=", 不是吗)。
◆ sd是你想用做分类值的栏的名。在这种情况下,我们是在调出点击值栏--首先我们显示在搜索中点击最多的页面,然后按照降序排列结果设置(D代表降序)--同SQL中的"Order By..."一样,就是打开结果设置的列举。
◆ ct指向在哪里找到了将要使用的目录或索引数据库。这里有一点技巧。当你为某一给定站点打开索引服务器时,你就要指出索引数据库在什么位置。你必须要将这个值指向这个位置,否则索引服务器就会返回一个结果NO。(这是FROM数据库)
随后,当然是调用将要取走这些值并查询服务器的ASP页面。注意,这是在使用表单处理中的GET类型-稍后将对此多做介绍,但是,只要你在使用我们的样本页面,就必须要用到它。
创建ASP页面
在ASP页面上一切都变得非常酷。你用表单中的值来驱动对索引服务器进行查询的对象。
整个过程是这样的:
◆ 打开记录集。
◆ 用标准ADO 方法,一步步地走过记录集。
<%
"Create a Query object, initialize it using
"SetQueryFromURL, and dump the object state
"set the query object
Set objQuery = Server.CreateObject("ixsso.Query")
"get the query properties set from the
"incoming URL (from the form GET operation)
objQuery.SetQueryFromURL(Request.QueryString)
"tell the object what columns to include
objquery.columns="filename,HitCount,vpath,DocTitle,characterization"
"open the recordset, causing the query to be
"executed
set rsQuery = objquery.createrecordset("nonsequential")
"now, if rsquery.eof is not TRUE, then we have results
"to show. If it IS TRUE, no results were found.
"get the page out for the user...
%>
<html>
<head>
</head>
<h1>Search Results</h1>
A maximum of 200 results will be returned, 20 hits per page will be shown. <br><br>
<%
if not rsquery.eof then
Response.Write rsquery.recordcount & " hit(s) were found. "
if rsquery.recordcount >30 then
Response.Write "You may want to refine your query."
end if
Response.Write "<br>"
end if
%>
<%
if not rsquery.eof then
while not rsquery.eof and rowcount >0
if rsquery("doctitle") <>"" then
Response.Write "<p><b><a href="" & rsquery("vpath") & "">" & rsquery("doctitle") & "</a></b><br>"
response.write "<font size=-1>" & rsquery("characterization") & "...</font><Br>"
Response.Write "<font size=- 2>" & rsquery("hitcount") & " hit(s)</font></p>"
end if
rowcount = rowcount - 1
rsquery.movenext
wend
Response.Write "<br><Br>"
%>
<%
else
%>
<p>
对不起,没有发现纪录,如果要查询两个以上的词,使用and或or。
</p>
<%
end if
%>
</body>
</html>
你需要做的第一件事就是建立对索引服务器对象的引用。这是通过使用server.creatobject方法来完成的:
Set objQuery = Server.CreateObject("ixsso.Query")
增加属性
现在objQuery 对象已经建立起来,在建立起引用它的记录集之前要进行配置。当然,你需要做的基本的事情之一就是建立查询--即在数据库中想要查找什么。这在引入的URL信息中进行--有一个方法是从URL中把所有内容提取出来,自动放在对象中: