Using Verity, you can search data returned by a query--such as a database record set--as if it were a collection of documents stored on your web server. Using Verity to search makes implementing a search interface much easier, as well as letting users more easily find information contained in database files. A database can direct the indexing process, by using different values for the type attribute of the cfindex tag. There are also several reasons and procedures for indexing the results of database and other queries.
When indexing record sets generated from a query (using the cfquery, cfldap, or cfpop tag), cfindex creates indexes based on the type attribute and its set value:
Type |
Attribute values |
---|---|
File |
The key attribute is the name of a column in the query that contains a full filename (including path). |
Path |
The key attribute is the name of a column in the query that contains a directory pathname. |
Custom |
The key attribute specifies a column name that can contain anything you want. In this case, the body attribute is required, and is a comma-delimited list of the names of the columns that contain the text data that is to be indexed. |
The cfindex tag treats all collections the same, whether they originate from a database recordset, or if they are a collection of documents stored within your website's root folder.
Indexing the results of a query is similar to indexing physical files located on your website, with the added step that you must write a query that retrieves the data to search. The following are the steps to perform a Verity search on record sets returned from a query:
You should use Verity to search databases in the following cases:
Unlike indexing documents stored on your web server, indexing information contained in a database requires an additional step--you must first write a query (using the cfquery, cfldap, or cfpop tag) that retrieves the data you want to let your users search. You then pass the information retrieved by the query to a cfindex tag, which indexes the data.
When indexing data with the cfindex tag, you must specify which column of the query represents the filename, which column represents the document title, and which column (or columns) represents the document's body (the information that you want to make searchable).
When indexing a recordset retrieved from a database, the cfindex tag uses the following attributes that correspond to the data source:
Attribute |
Description |
---|---|
key |
Primary key column of the data source table. |
title |
Specifies a query column name. |
body |
Columns that you want to search for the index. |
type |
If set to custom, this attribute specifies the columns that you want to index. If set to file or path, this is a column that contains either a directory path and filename, or a directory path that contains the documents to be indexed. |
Using the cfindex tag to index tabular data is similar to indexing documents, with the exception that you refer to column names from the generated record set in the body attribute. In the following example, the type attribute is set to custom, specifying that the cfindex tag index the contents of the record set columns Emp_ID, FirstName, LastName, and Salary, which are identified using the body attribute. The Emp_ID column is listed as the key attribute, making it the primary key for the record set.
Search and display the query results
You can index an individual file that uses a query by retrieving a table row whose contents are a filename. In this case, the key specifies the column that contains the complete filename. The file is indexed using the cfindex tag as if it were a document under the web server root folder.
In the following example, the cfindex tag's type attribute has been set to file, and the specified key is the name of the column that contains the full path to the file and the filename.
<cfquery name="getEmps" datasource="cfdocexamples"> SELECT * FROM EMPLOYEE WHERE EMP_ID = 1 </cfquery> <cfindex query="getEmps" collection="CodeColl" action="Update" type="file" key="Contract_File" title="Contract_File" body="Emp_ID,FirstName,LastName,Contract_File">
<!--- Output the record set.---> <p>Your collection now includes the following items:</p> <cfoutput query="getEmps"> <p>#Emp_ID# #FirstName# #LastName# #Contract_File#</p> </cfoutput> <cfsearch collection="#Form.collname#" name="getEmps" criteria="#Form.Criteria#" maxrows = "100"> <!--- Output the filename contained in the record set. ---> <cfoutput> Your search returned #getEmps.RecordCount# file(s). </cfoutput> <cfoutput query="getEmps"> <p><table> <tr><td>Title: </td><td>#Title#</td></tr> <tr><td>Score: </td><td>#Score#</td></tr> <tr><td>Key: </td><td>#Key#</td></tr> <tr><td>Summary: </td><td>#Summary#</td></tr> <tr><td>Custom 1:</td><td>#Custom1#</td></tr> <tr><td>Column list: </td><td>#ColumnList#</td></tr> </table></p> </cfoutput>
You can index a directory path to a document (or collection of documents) using a query by retrieving a row whose contents are a full directory path name. In this case, the key specifies the column that contains the complete directory path. Documents located in the directory path are indexed using the cfindex tag as if they were under the web server root folder.
In this example, the type attribute is set to path, and the key attribute is assigned the column name Project_Docs. The Project_Docs column contains directory paths, which Verity indexes as if they were specified as a fixed path pointing to a collection of documents without the use of a query.
Index a directory path within a query
The ColdFusion cfindex tag indexes the contents of the specified directory path.
Search and display the directory path
The widespread use of the Lightweight Directory Access Protocol (LDAP) to build searchable directory structures, internally and across the web, gives you opportunities to add value to the sites that you create. You can index contact information or other data from an LDAP-accessible server and let users search it.
When creating an index from an LDAP query, remember the following considerations:
In the following example, the search criterion is records with a telephone number in the 617 area code. Generally, LDAP servers use the Distinguished Name (dn) attribute as the unique identifier for each record so that attribute is used as the key value for the index.
<!--- Run the LDAP query. ---> <cfldap name="OrgList" server="myserver" action="query" attributes="o, telephonenumber, dn, mail" scope="onelevel" filter="(|(O=a*) (O=b*))" sort="o" start="c=US"> <!--- Output query record set. ---> <cfoutput query="OrgList"> DN: #dn# <br> O: #o# <br> TELEPHONENUMBER: #telephonenumber# <br> MAIL: #mail# <br> =============================<br> </cfoutput> <!--- Index the record set. ---> <cfindex action="update" collection="ldap_query" key="dn" type="custom" title="o" query="OrgList" body="telephonenumber"> <!--- Search the collection. ---> <!--- Use the wildcard * to contain the search string. ---> <cfsearch collection="ldap_query" name="s_ldap" criteria="*617*" maxrows = "100"> <!--- Output returned records. ---> <cfoutput query="s_ldap"> #Key#, #Title#, #Body# <br> </cfoutput>
The contents of mail servers are generally volatile; specifically, the message number is reset as messages are added and deleted. To avoid mismatches between the unique message number identifiers on the server and in the Verity collection, you must re-index the collection before processing a search.
As with the other query types, you must provide a unique value for the key attribute and enter the data fields to index in the body attribute.
The following example updates the pop_query collection with the current mail for user1, and searches and returns the message number and subject line for all messages that contain the word action:
<!--- Run POP query. ---> <cfpop action="getall" name="p_messages" server="mail.company.com" userName="user1" password="user1"> <!--- Output POP query record set. ---> <cfoutput query="p_messages"> #messagenumber# <br> #from# <br> #to# <br> #subject# <br> #body# <br> <hr> </cfoutput> <!--- Index record set. ---> <cfindex action="refresh" collection="pop_query" key="messagenumber" type="custom" title="subject" query="p_messages" body="body"> <!--- Search messages for the word "action". ---> <cfsearch collection="pop_query" name="s_messages" criteria="action" maxrows = "100"> <!--- Output search record set. ---> <cfoutput query="s_messages"> #key#, #title# <br> </cfoutput>