Creating a search page
You use the cfsearch tag to search an
indexed collection. Searching a Verity collection is like a standard
ColdFusion query: both use a dedicated ColdFusion tag that requires
a name attribute for their searches and both return
a query object that contains rows matching the search criteria.
The following table compares the two tags:
cfquery
|
cfsearch
|
Searches a data source
|
Searches a collection
|
Requires a name attribute
|
Requires a name attribute
|
Uses SQL statements to specify search criteria
|
Uses a criteria attribute to specify search
criteria
|
Returns variables keyed to database table
field names
|
Returns a unique set of variables
|
Uses cfoutput to display
query results
|
Uses cfoutput to display
search results
|
Note: You receive an error if you attempt to search
a collection that has not been indexed.
The following are important attributes for the cfsearch tag:
Attribute
|
Description
|
name
|
The name of the search query.
|
collection
|
The name of the collection(s) being searched.
Separate multiple collections with a comma; for example, collection = "sprocket_docs,CodeColl".
|
criteria
|
The search target (can be dynamic).
|
maxrows
|
The maximum number of records returned by
the search. Always specify this attribute to ensure optimal performance
(start with 300 or less, if possible).
|
Each cfsearch returns variables that provide
the following information about the search:
Attribute
|
Description
|
RecordCount
|
The total number of records returned by
the search.
|
CurrentRow
|
The current row of the recordset.
|
RecordsSearched
|
The total number of records in the index
that were searched. If no records are returned in the search, this property
returns a null value.
|
Summary
|
Automatic summary saved by the cfindex tag.
|
Context
|
A context summary that contains the search
terms, highlighted in bold (by default). This is enabled if you
set the contextpassages attribute to a number greater
than zero.
|
Additionally, if you specify the status attribute,
the cfsearch tag returns the status structure,
which contains the information in the following table:
Variable
|
Description
|
found
|
The number of documents that contain the
search criteria.
|
searched
|
The number of documents searched. Corresponds
to the recordsSearched column in the search results.
|
time
|
The number of milliseconds the search took,
as reported by the Verity K2 search service.
|
suggestedQuery
|
An alternative query, as suggested by Verity,
that may produce better results. This often contains corrected spellings
of search terms. Present only when the suggestions tag
attribute criteria is met.
|
Keywords
|
A structure that contains each search term
as a key to an array of up to five possible alternative terms in
order of preference. Present only when the suggestions tag
attribute criteria is met.
|
You can use search form and results pages like the following
examples to search a collection.
Create a search form
Create a ColdFusion page with the following content:
<html>
<head>
<title>Searching a collection</title>
</head>
<body>
<h2>Searching a collection</h2>
<form method="post" action="collection_search_action.cfm">
<p>Enter search term(s) in the box below. You can use AND, OR, NOT, and
parentheses. Surround an exact phrase with quotation marks.</p>
<p><input type="text" name="criteria" size="50" maxLength="50">
</p>
<input type="submit" value="Search">
</form>
</body>
</html>
Save the file as collection_search_form.cfm.
Enter search target words in this form, which ColdFusion passes
as the variable criteria to the action page, which
displays the search results.
Create the results page
Create a ColdFusion page with the following content:
<html>
<head>
<title>Search Results</title>
</head>
<body>
<cfsearch
name = "codecoll_results"
collection = "CodeColl"
criteria = "#Form.criteria#"
contextPassages = "1"
contextBytes = "300"
maxrows = "100">
<h2>Search Results</h2>
<cfoutput>
Your search returned #codecoll_results.RecordCount# file(s).
</cfoutput>
<cfoutput query="codecoll_results">
<p>
File: <a href="#URL#">#Key#</a><br>
Document Title (if any): #Title#<br>
Score: #Score#<br>
Summary: #Summary#<br>
Highlighted Summary: #context#</p>
</cfoutput>
</body>
</html>
Save the file as collection_search_action.cfm.
View collection_search_form.cfm in the web browser.
Enter target words and click Search.
Note: As part of the indexing process, Verity automatically
produces a summary of every document file or every query recordset
that gets indexed. The default summary result set column selects
the best sentences, based on internal rules, up to a maximum of
500 characters. Every
cfsearch operation returns
summary information by default. For more information, see
Using Verity Search Expressions. Alternatively, you can use the
context result set column, which provides a context summary with
highlighted search terms.