Creating an Index Server Search and Result Page


Microsoft Index server provides an easy mechanism for providing searching on your Web site. Index Server provides a set of COM objects that you can use to easily access content on your Web site.

Before starting with creating a search page you should check to make sure that you have the NT Option Pack installed and that the indexing service is installed and up and running. Next check your Web site for Indexing options which you can find in the Microsoft Management Console - Go into the Directory tab for each virtual directory of the server and make sure you enable or disable the 'Index this directory' checkbox as needed. I suggest you disable it for any directories you will never have searchable content - no need to waste search and indexing resources on content you'll never use.

The next step is to create the query page. I'm going to keep this real simple and added a link to my home page like this:

<FORM action='search_simple.asp' id='frmSearch' 
           method=GET name=search_simple.asp>
  Search Site<br>
  <INPUT id=txtQuery name=Query size='20'>
</FORM>

The next step is to actually create the search page that retrieves the output based on the query submitted. To do so we'll use the Index Server COM objects in an ASP page (note you can use these objects anywhere including in a Web Connection script or code page).

The actual query code I use looks as follows in the <Head> section of the document (search_simple.asp):

<%
lcErrorMsg = ''
Set rs = RunQuery(Request.QueryString('query').Item(),Request.QueryString('virtual').item())
if TypeName(rs) <> 'Recordset'  then
   lcErrorMsg = 'Sorry your query returned no matching documents.<br>Please try another query.'
end if

function RunQuery(lcQuery,lcVirtual) 


if TypeName(lcVirtual) <> 'String' then
	lcVirtual = '/'
end if  
if TypeName(lcQuery) <> 'String'  then
	Set RunQuery = nothing
    Exit Function
end if
    
Set oQ=Server.CreateObject('ixsso.Query')
oQ.query = lcQuery
oQ.columns = 'DocTitle,vpath,hitcount,size,write,characterization,rank'
oQ.SortBy = 'hitcount[d]'

Set oU=Server.Createobject('ixsso.util')

'*** Add only a few directories for searching
oU.AddScopeToquery oQ,'/','shallow'
oU.AddScopeToquery oQ,'/presentations','deep'
oU.AddScopeToquery oQ,'/rick','deep'
oU.AddScopeToQuery oQ,'/wwhelp','deep'
oU.AddScopeToQuery oQ,'/wckb','deep'

Set rs=oQ.CreateRecordset('nonsequential')

if TypeName(rs) <> 'Recordset' then
   Set RunQuery = nothing
   exit function
end if   

if rs.RecordCount < 1 then
	Set RunQuery = nothing
else   
	' Return result RecordSet
	Set RunQuery = rs
end if	

End Function
%>

This code basically submits the query and returns an ADO recordset if results were found. The code starts with some global code that calls the RunQuery function which in turn returns the recordset.

The index server objects are fairly easy to work with - you basically specify your query, which fields you want to receive (you can check the Index Server MMC Snap in for field names - I couldn't find them documented elsewhere though), and which virtual directories you want to include in the search. The CreateRecordset method of the query object then returns the ADO recordset with the requested data.

Note that I have an error message variable defined - I use this as flag to tell me that the query failed either due to a real failure or due to the fact that it didn't match. In that case an error message is generated into lcErrorMsg.

<% If LEN(lcErrorMsg) > 0 then %>
   <p>
   <b class='header'>   <%= lcErrorMsg %>   </b>
<% Response.End
   end if
%>

Some conditional code in the ASP body checks for the error message and if found displays it and exits. If no error is found, the remainder of the page proceeds to display the data.

<span class='header' style='color:DarkRed'>
Your query returned <%= rs.recordcount %> documents...</span><p>

<blockquote>
<table width='90%'  class='body'>

<% do while not rs.eof %>

<tr>
  <td><b><%= rs.Fields('DocTitle')%></b></td>
</tr>
<tr>
  <td><a href='<%= rs.Fields('vPath')%>'><%= rs.fields('vPath')%></td>
</tr>
<tr>
  <td><%= rs.Fields('characterization') %><br>
       <b>Hits: <%= rs.Fields('hitcount')%> 
       Last updated on: <%= rs.Fields('write') %></b>
<hr></td>
</tr>

<%rs.MoveNext
  loop%>

</table>


Last Updated: 08/08/99