The following code can be used for a basic search results page. It should be saved as SearchResults.asp.
The first part of the search results page initialises variables and constants:
sSearchString = Request.Form("query")
Const SEARCH_CATALOG = "catalog_name"
The search string was posted from the SearchForm.asp search form page, and the word or phrase to be searched for are extracted from the query item in the Request.Form collection.
The SEARCH_CATALOG constant is also defined. This name will vary so you will have to change it. If your site is hosted with a hosting company then they will usually set up a catalog on the Index Server for you, then let you know the name of your search catalog. If you are using your own web server, then you should be able to determine the catalog name from the Index Server Management Console. Describing how to set up and use Index Server catalogs is beyond the scope of this article, but further information is available in the IIS 4.0 reference guide (try http://localhost/iishelp/).
The next part of the search results page initialises the Index Server Query COM component which enables the search to be performed:
Set oQuery = Server.CreateObject("IXSSO.Query")
oQuery.Catalog = SEARCH_CATALOG
oQuery.Query = "@all " & sSearchString & _
" AND NOT #path *_* AND NOT #path *downloads* " & _
" AND NOT #path *images* AND NOT #filename *.class " & _
"AND NOT #filename *.asa AND NOT #filename *.css AND NOT #filename *postinfo.html"
oQuery.MaxRecords = 200
oQuery.SortBy = "rank[d]"
oQuery.Columns = "DocAuthor, vpath, doctitle, FileName, Path, " & _
"Write, Size, Rank, Create, Characterization, DocCategory"
Set oRS = oQuery.CreateRecordSet("nonsequential")
Further details of the Query object’s methods and properties are to be found in the IIS 4.0 online documentation. The properties of the object set in the sample code above are as follows:
- Catalog: The name of the search catalog to be searched.
- Query: The query to be made. Note that the query in this example is comprised of the search string, plus a list of file and folder exclusions. It is important to remember that Index Server indexes content by using the file system, and therefore is able to index files you’d rather not allow users to search from the web. Examples include global.asa files, FrontPage configuration files (these folders have underscores in their names) and files such as Java class files and Cascading Style Sheets.
- MaxRecords: This property specifies the maximum number of search results that should be returned.
- SortBy: This specifies which column name the search results should sorted by. The usual setting for this is rank[d], i.e. sort results in descending order according to their similarilty to the search string.
- Columns: A list of column properties that should be returned in the search results. These will be discussed in further detail later on.
Finally, an ADO RecordSet is created from the records found for this search. The neat thing about Index Server is that the returned RecordSet of search results can be used in an almost identical fashion to RecordSets returned from databases. A list of results is, therefore, displayed simply by looping through this RecordSet and displaying fields from the RecordSet:
If oRS.EOF Then
Response.Write "No pages were found for the query " & sSearchString & ""
Do While Not oRS.EOF
Response.write "FileName: " & oRS("FileName") & "
Response.write "doctitle: " & oRS("doctitle") & "
Response.write "Size: " & oRS("Size") & "
Response.write "Create: " & oRS("Create") & "
Response.write "Write: " & oRS("Write") & "
Response.write "Characterization: " & oRS("Characterization") & "
This code loops through the records corresponding to the matching documents found and displays some of the properties of each document. This is where the columns property of the Index Server Query are used: these specify the column names that are returned for each record. In the sample code above, FileName refers to the record’s name on disk.
|Unfortunately the FileName property only contains the file's name in lower case. This can lead to problems if you are building a cross-platform search solution with operating systems that use case sensitive filenames (e.g. Unix and Linux).|
doctitle corresponds to the document’s title (i.e. <title> tag if the document is HTML). Size is the size in bytes of the file on disk. Create is the date and time the document was created, whereas Write is the date and time the document was modified. Finally, Characterization is a summary of the document. The summary corresponds to the Description meta tag in HTML files, so it is worthwhile putting this tag into documents. If the Description tag isn’t present, Index Server will display the first one or two sentences from the document.
Finally at the bottom of the page the objects are released:
Set oRS = nothing
Set oQuery = nothing
An example screenshot of the search results page is below as described above, the page displays the FileName, doctitle, Size, Create and Characterization for each matching document:
As you can see the page is fairly basic, so a few cosmetic improvements would be required if the page was to be used on a production website. Further suggestions for improving the display of search results are in the article "More about Searching Index Server With ASP".