Displaying Search Results
Displaying Your Search Results
Now that you have set your query properties, you can call the Query
object's CreateRecordSet function. This returns an ADO Recordset
object containing details of those documents that meet your search criteria,
if any, thus:
Set objRecordSet
= objQuery.CreateRecordSet("sequential")
The string parameter passed to CreateRecordSet defines the cursor
type to create. This can be either "sequential" or "nonsequential" and
defines whether you can move backwards through the Resultset. For a
paged result effect you would need to specify "sequential" in order
to navigate backwards and forwards when the user selects the previous/next
page.
Remember, however, that the columns collection returned matches the
list of column names specified in the Query object's columns
property. If there are no matching documents, objRecordSet will
either be nothing or EOF will be true. This is also true if a
runtime error occurs while trying to create the search results.
If the search criteria were too complex then the Recordset EOF
and BOF properties will be True and the Query object's
QueryIncomplete property will be True.
Determining The Total Number of Records Found
To determine the total number of records found you need to examine the
RowCount and RowLimitExceeded properties of the RecordSet.
RowLimitExeceeded is set to True if the maximum number
of records found has been reached. You cannot navigate past this number:
<%
varTotalRecords = objRecordSet.Properties("RowCount")
If objRecordSet.Properties("RowLimitExceeded")
Then
varSummaryHTML = "Over " & varTotalRecords & " records found."
Else
varSummaryHTML = varTotalRecords & " records found."
End If
%>
Creating a Search Results Table
You can create a standard search results table that outputs the document's
URL, title, brief description and rank for each record in objRecordSet.
The Rank column returns a number between 0 and 1000 to define
the best matches. The higher the rank; the better the match. In our
example we use a number of guru images from 1 to 5. Five gurus means
an higher match. In order to calculate the number of gurus needed we
divide the rank by 200.
The MimeType column defines the actual type of document found.
In the case of Microsoft Office documents you may want to show the appropriate
application icon. For news group posting you will want to prefix the
URL with 'news://'
You will often find that a document does not have a title so the DocTitle
column will be blank. In order to give the user a friendly title it
is recommended that you output a default message such as "No Title"
in these circumstances.
To create the results table you need to output a standard <TABLE>
tag followed by a table row <TR> for each record and a table def
<TD> for each output column, thus:
<%
Response.Write "<table>"
Do
Response.Write "<tr>" & _
" <td>" & varRecordNum
& ".</td>" & _
" <td><b>" &
varDocTitle & "</b></td>" & _
" <td>" & _
objUtil.TruncateToWhiteSpace(objRecordSet("Description"),
250) & _
" </td> & _
" <td>URL: " & varURL
& "</td>" & _
"</tr>"
objRecordSet.MoveNext
varRecordNum = varRecordNum + 1
Loop Until objRecordSet.EOF Or varRecordNum
> varTotalRecords
Response.Write "</table>"
%>
We can now make use of another part of the MSSearch component,
the Util object. This contains various language and formatting
functions that you will find useful. In our case we use the TruncateToWhiteSpace
function. It truncates the input string to the specified number of characters
and removes line feeds and such like. This is exactly what we need when
displaying a summary of the description of the page.
As with the Query object you need to use the Server.CreateObject
function:
Set objUtil
= Server.CreateObject("MSSearch.util")
Creating A Search Results Table With A Next Button
You may want to limit the number of records per page to a more usable
number such as 10 and then allow the user to navigate to the next page.
You can implement this by navigating to the end of the Recordset and
examining its MoreRows property. If this is True and your
counted record number is less than the total records found then you
can specify the Query object's StartHit property. This
tells the Query object which record number to start retrieving
its next batch of records from. You must have navigated to the end of
the Recordset before you can retrieve the correct value for the Recordset's
NextStartHit property.
The best approach is to create a hyperlink that calls the same search
page but appends the objQuery.QueryToURL, search criteria and
current record number to the QueryString thus:
<%
If objRecordSet.Properties("MoreRows")
And varRecordNum -1 < varTotalRecords Then
objQuery.StartHit = objRecordSet.Properties("NextStartHit")
Response.Write "<tr><td colspan=""3"" align=""center"">"
& _
"<a href=""sitesearch.asp?"
& _
objQuery.QueryToURL & _
"&DisplayText=" & Server.URLEncode(varSearchText)
& _
"&RecordNum=" & varRecordNum
& _
""">" & cMoreResultsLinkText
& "" & vbNewLine & _
"</td></tr>"
End If
%>
You will now need to alter the way in which you set the Query
object's Query property. If your Next Page hyperlink points to
this page, (SiteSearch.asp) you can compare the http_referer
and url Request object's ServerVariables to see if this is the
same page.
If this is the case, then you can use the Query object's SetQueryFromURL
method as an alternative to set your query parameters. SetQueryFromURL
has a number of standard QueryString values that correspond to setting
individual properties:
| Value |
Description |
| ae |
Used by the AllowEnumeration property. If set to a
nonzero digit, enumeration is allowed. |
| ct |
Used by the Catalog property. |
| di |
Used by the Dialect property. |
| gd |
GroupBy descending. Used by the GroupBy property. |
| gr |
Used by the GroupBy property. |
| mh |
Used by the MaxRecords property. |
| qu |
Full text of query. Used by the Query property. |
| so |
Used by the SortBy property. |
| sd |
Sort descending. Used by the SortBy property. |
For example, passing the QueryString "qu=@size>1000&so=rank[d]&mh=100"
to SetQueryFromURL is equivalent to the following code:
<%
objQuery.Query = "@size > 1000"
objQuery.SortBy = "rank[d]"
objQuery.MaxRecords = 100
%>
In this query we want to see all documents that are over 1000 bytes
in size, sorted by rank with 100 records per pass.
The following section of code checks the QueryString to see if there
is a DisplayText value. If not, it will use SetQueryFromURL to
set the query:
<%
If Request.QueryString("DisplayText")
= "" Then
varSearchText = Request.Form("txtCriteria")
objQuery.Query = varSearchText
Else
varSearchText = Request.QueryString("DisplayText")
objQuery.SetQueryFromURL Request.QueryString
End If
%>