Collection Examples  
 
 

The following code examples demonstrate a very basic approach to populating and searching a collection of documents and a collection of data from a ColdFusion query. To run the example, save the code examples into the indicated file names all in the same directory. Then open start.cfm to run the example.

 
 
  Example: Choose the collection  
 

This first sample, start.cfm, provides a simple starting point for populating a collection. You can use the code as is, with a few minor changes. Note that for this sample to work, you'll first need to create two collections: one called DBINDEX and the other, DOCS.

Start.cfm
<HTML>
<HEAD>
    <TITLE>Verity Samples</TITLE>
</HEAD>

<H2>Pick which index you want to build</H2>
<P>Select the collection you want to populate:</P>

<FORM METHOD="POST" ACTION="index.cfm">
    <INPUT TYPE=radio NAME=collection VALUE=DBINDEX checked>
        Message table from CF 4.0 Examples<BR>
    <INPUT TYPE=radio NAME=collection VALUE=DOCINDEX>
        Documents in the Web server root directory tree<BR><BR>
    <INPUT TYPE=SUBMIT NAME=doindex VALUE="Populate">
</FORM>

<H3>Skip populating the collection, go right 
to <A HREF="search.cfm"> searching a collection</A></H3>
</BODY>
</HTML>
 
 
  Example: Populate the collection  
 

Now populate the collection you chose:

Index.cfm
<HTML>
<HEAD>
    <TITLE>Verity Custom Tag Tests</TITLE>
</HEAD>

<CFQUERY NAME="Messages"
    DATASOURCE="CF 4.0 Examples">
    SELECT *
        FROM Messages
</CFQUERY>

<BODY>
<CFIF #form.collection# IS "DBINDEX">
    <CFINDEX COLLECTION="DBINDEX"
        ACTION="UPDATE"
        TYPE="CUSTOM"
        BODY="Body"
        KEY="Message_ID"
        TITLE="UserName"
        QUERY="Messages">
<CFELSE>
    <CFINDEX COLLECTION="DOCS"
        TYPE="PATH"
    KEY="c:\inetpub\wwwroot"
    URLPATH="http://127.0.0.1/"
    EXTENSIONS=".htm, .html, .cfm, .cfml"
    RECURSE="YES">
</CFIF>

<H2>Collection Successfully Generated</H2>
<H3><A HREF="search.cfm">Search the collection</A></H3>

</BODY>
</HTML>
 
 
  Example: Search the collection  
 

With the collections populated, you can now offer a form for searching a collection:

Search.cfm

<HTML>
<HEAD>
    <TITLE>CFINDEX and CFSEARCH samples</TITLE>
</HEAD>

<BODY>
<H2>Search</H2>
<P><B>Select search type:</B></P>
<FORM METHOD="POST" ACTION="vfp_search.cfm"><P>
    <INPUT TYPE=radio
        NAME=type
        VALUE=Simple checked> Simple<BR>
    <INPUT TYPE=radio
        NAME=type
        VALUE=Explicit> Explicit<P>
<P><B>Select data source:</B></P>
    <INPUT TYPE=radio
        NAME=source
        VALUE=DBINDEX checked>
Messages Table<BR>
    <INPUT TYPE=radio
        NAME=source
        VALUE=DOCS>
<P>Web doc root directory</P>
<P>Search string:</P>
    <INPUT TYPE=text 
        NAME=searchstring SIZE=50><P>
    <INPUT TYPE=SUBMIT
        NAME=search1 
        VALUE="Search">
    <INPUT TYPE=reset 
        VALUE="Reset">
</FORM>

</BODY>
</HTML>
 
 
  Example: Present the search results  
 

The following example processes the search results.

vfp_search.cfm
<HTML>
<HEAD>
    <TITLE>Search output template</TITLE>
</HEAD>

<CFIF #form.source# IS "DBINDEX">
    <CFSET #type#="messages">
<CFELSE>
    <CFSET #type#="files">
</CFIF>

<BODY>

<CFSEARCH NAME="Search1"
    COLLECTION="#form.source#"
    FORM TYPE="#form.type#"
    CRITERIA="#form.searchstring#">

<H2>Search Results</H2>

<CFOUTPUT>
    #Search1.RecordCount# #type# found out of
    #Search1.RecordsSearched# #type# searched.
</CFOUTPUT>

<HR NOSHADE>

<CFIF #form.source# IS "DBINDEX">
    <CFOUTPUT QUERY="Search1">
        <A HREF="cf_tag_verity_detail.cfm?ID=#Search1.KEY#">
        Message #Search1.KEY# posted by #Search1.TITLE#</A><BR>
        Score = #Search1.SCORE#<BR>
    </CFOUTPUT>
<CFELSE>
    <CFOUTPUT QUERY="Search1">
        <A HREF="#Search1.URL#">#Search1.Title#</A><BR>
    </CFOUTPUT>
</CFIF>

<HR NOSHADE>
</BODY>
</HTML>
 
 
  Example: Output from searching the DBINDEX collection  
 

This application page presents each individual message.

cf_tag_verity_detail.cfm

<CFQUERY NAME="MessageDetail"
    DATASOURCE="CF 4.0 Examples">
    SELECT UserName, Subject, Body
        FROM Messages
        WHERE Message_ID = #ID#
</CFQUERY>

<CFOUTPUT QUERY="MessageDetail">
<PRE>
    Message #URL.ID# <BR>
    Posted by: #MessageDetail.UserName# <BR>
    Subject: #MessageDetail.Subject# <BR>
    #MessageDetail.Body#
</PRE>
</CFOUTPUT>


 
 
BackUp LevelNext
 
 

allaire     AllaireDoc@allaire.com
    Copyright © 1998, Allaire Corporation. All rights reserved.