cfcollection, cfindex, and cfsearch - A Simple Tutorial

cfsearch is one of the most helpful and powerful ColdFusion functions that many developers seem to ignore. Here is a basic, step-by-step explanation of how to use cfcollection, cfindex, and cfsearch to create powerful search functions for our websites.

Why cfsearch and not SQL search?
Basic SQL searches are fine for searches using a specific keyword, such as an employer’s name or a phone number. However, if you want to use multiple keywords to search through a large collection of text with multiple fields, or you want to search through multiple documents like a search engines does, database searches can get a bit complicated. We need to create full-text indexes and all, but we still may not be able to search through a file saved outside the database. In this kind of situation, you can create a true search engine with cfsearch––and the process is relatively quick and easy.

Start with a cfcollection tag by providing a new name. Then tell ColdFusion where to save internal data for our new search collection.

   1: <cfcollection action= "Create" collection="MyCollection" path= "c:\mysite\">

This process creates a new collection named “MyCollection,” and it also creates the folder [c:\mysite\MyCollection]. If you want to delete the collection, do not delete the folder ColdFusion created––use this code instead. It unregister the collection from the server.

   1: <cfcollection action= "Delete" collection="MyCollection" path= "c:\mysite\">

The collection name you use must be unique. So before you create a new collection, run a quick search to make sure the name is not already in use. This is the easiest way to do it.

   1: <cfcollection action= "list" name="collectionlist">
   2: <cfdump var="#collectionlist#">
Show/Hide Line Numbers . Full Screen . Plain

Next, we need to add data to our new collection using a cfindex tag. We can use a cfindex over queries and/or files and folders in our hard drive. Lets add query a to a cfindex first. First create the query, and then add it to the cfindex.

   1: <cfquery name="MyQuery" datasource="datasource">
   2:  select pageID, PageText, PageDescription from MyPages
   3: </cfquery>
   4: <cfindex collection= " MyCollection " action = "update" type= "custom" body= " PageText,PageDescription " query= " MyQuery " key= "pageID ">
Show/Hide Line Numbers . Full Screen . Plain

cfindex analyzes the query results from the query [MyQuery]. It also creates a searchable index of its own inside the collection folder that we created using cfcollection.

As you can see, I passed two query columns to the attribute [body]. You can pass any number of query columns to the [body]. Be sure to separate the query columns with commas.  This is the content that ColdFusion is going to index for our search engine. In other words, these columns will be available for searching.

We can run cfindex multiple times for different queries on the same collection and then add more records to our collection. When we do that, ColdFusion identifies each record by the primary key we provide. If cfindex receives a record with a same primary key for the second time, it will overwrite the existing record. If it is a new record, cfindex creates a new key.

We can also index a folder in our hard drive for searching. Let’s assume our web pages are stored inside a folder name [c:\mysite\myWebPages]. The following will create an index over that folder and make the folder searchable.

   1: <cfindex collection="MyCollection" action="refresh" key="c:\mysite\myWebPages" type="path" urlpath="c:\mysite\myWebPages" extensions=".cfm,.html">

Now we are ready to use cfsearch on our new collection. It’s very simple. For example, if I want to search for the keyword “fetch me a stick,”

   1: <cfsearch name="searchResults" collection=" MyCollection" criteria=" fetch me a stick ">
   2: <cfdump var="#searchResults#">
Show/Hide Line Numbers . Full Screen . Plain

This will return a query named [searchResults] that contains, of course, the search results. If no record is found, it will return zero records. 

Those are the basics folks. Happy searching!

7 Comments :
SUNITA RANI
Monday 31 March 2014 11:16 AM
SIR G MERE KO MAIL MELLA HAI KI AAP KA LONE AC NO 4260CD01700765 HAI KI EMI LAGNE HAI 2 APRIL KO MERA KOI LONE NAHI HAI CALL KARE 9888230302 par
Brian
Monday 17 March 2014 10:37 AM
The question I can't seem to find an answer to is whether or not you have to run this collection/index each time you search or only once (not including any updates needed in the future)?
arthur
Tuesday 09 October 2012 01:42 PM
Is it always c:\mysite?
Tuesday 09 October 2012 06:56 PM
No. It is any folder you like.
Thursday 12 July 2012 03:54 AM
Thanks! Super helpful, quick tutorial.
Tuesday 08 February 2011 10:35 PM
It’s extremely easy. When you create the cfindex (4th code block above) , add your “link” to the KEY attribute.(Or you can add the primary key value of your record and generate the link with that). When you run the CFSEARCH, it actually output the KEY field back to you, alone with the small-description. That mean if the page link is your KEY, you get the link back in the KEY. If the primary key is your KEY, you get that back.
nich
Tuesday 08 February 2011 05:39 PM
Hi,
Thank you for the tut. Would like to find out, how do you link the search results?
Thank you.