Loading...
 

Greg`s Tech blog

Searching with Index Service

Tuesday 10 of February, 2009
Sorry I'm so late to this party, but...

We have an intranet site that uses Frontpage's search webbot. It stopped working and because I didn't want to learn how it did work, I decided to implement the search using Windows Index service and .asp pages. Here's how I did it

The problem:
  • Documents are separated into to high level groups, In house and home workers. Separate catalogs exists for these document trees
  • Searches must be restricted to documents in the tree
  • Documents consist of a collection of .htm files contained within a particular subdirectory within the tree
  • a search from a given document should only return hits from that document

Solution:
  • Create a search form to be added to the index page of each document
  • Have the form post to a results.asp page within the same directory as the document
  • Use the server variable SCRIPT_NAME to capture the directory of the current document
  • Use the directory to limit the scope of the search
  • Use an include statement in results.asp to call the real search script


The search form

Note: This is an html file stripped of html. Sorry for the confusion, but this is a well know problem and you can find the info other places.

form action="results.asp" method=post
Search text:
input type=text name="searchstring" size="50" maxlength="100" value=" "
Ex: 'meter percent' will return documents with either meter or percent
Ex: 'meter & percent' will return documents with both meter and percent
button type=submit>Submit
button type=reset>Clear Form
/form

Results.asp
(Properly formatted html file with this in the body)
#include virtual="/_Search/search.asp"

The Search.asp file
Again, any html is stripped. In particular you have to provide the table definition for the response.write statements. contact me if you need details.

' search.asp - Greg Martin - feb 2009
' uses index service to provide search function for Reedman
' 
' search.asp is meant to be 'included' in results.asp.  In fact, it 
' relies on the fact that it is called through results.asp located in the directory of the document that need to be searched 
' so that the current directory can be read from the "SCRIPT_NAME server variable
'
' This section sets the various configuration variables

Dim formscope, pagesize, maxrecords, searchstring
dim catalogtosearch, searchrankorder, origsearch
dim q, filepath, CRLF, rs
Dim lsPath, arPath, CurPath


formscope="/"
pagesize = 500
maxrecords=500
searchstring=request.form("searchstring")

' we have two catalogs - one for homeworkers and one for Prod
' here we select the catalog based on the location fo the results page
if instr(Request.ServerVariables("SCRIPT_NAME"),"homeworker") > 0 then
	catalogtosearch="eHW_Search"
	response.write "Searching eHomeworker catalog"
else
	catalogtosearch="Prod_Search"
	response.write "Searching Production catalog"
end if

searchrankorder="rank[d]"
origsearch=searchstring
CRLF = Chr(13) & Chr(10)

if trim(searchstring) = "" then

   response.write "No search terms entered"

else
	
	' this code effectively removes the filename and leaves the path
	' it also replaces / with \ so the regex search works
	lsPath = Request.ServerVariables("SCRIPT_NAME")
	arPath = Split(lsPath, "/")
	arPath(UBound(arPath,1)) = ""
	CurPath = Join(arPath, "\")
	
	'This section performs the query
	
	set q=server.createobject("ixsso.query")
	
	' this query restricts results to docs in the filepath using regex & #path
	filepath= "*" & CurPath & "*"
	q.query=searchstring & " & #path " & filepath
	
	q.catalog=catalogtosearch
	q.sortby=searchrankorder
	q.columns="doctitle, filename, size, write, rank, directory, vpath, path"
	q.maxrecords=maxrecords
	
	
	'Displays the results
	set rs=q.createrecordset("nonsequential")
	rs.pagesize=pagesize
	response.write"<p>Your search for <b>" & origsearch & "</b> produced "
	
	if rs.recordcount=0 then response.write "no results"
	if rs.recordcount=1 then response.write "1 result: "
	if rs.recordcount>1 then response.write(rs.recordcount) & " results: </p>"
	
	response.write "I cannot post this code"
	
	
	do while not rs.EOF
	   response.write 	"I cannot post this code
	   rs.movenext
	loop
	
	response.write ""
	
end if

set rs=nothing
set q=nothing
set util=nothing