Warning: This blog entry was written two or more years ago. Therefore, it may contain broken links, out-dated or misleading content, or information that is just plain wrong. Please read on with caution.
Anyone who follows my blog will notice that I recently added a search function to my site. I did this after I found myself having to open mysql to do a search for a particular entry I created a few months ago. To create the site search I utilized ColdFusion's cfsearch tag.
Whats Wrong With SQL
SQL searches are a useful tool and I have written many custom search system that leverage SQL for given a highly customizable, filterable data. However SQL is limited in that it can become very tricky to implement if you want to search multiple test datapoints for multiple keywords. This is where cfsearch comes into its own.
Depending on which cfml engine you are running on you will have difference indexing engines to choose from. If you are on Adobe CF 7/8 you will use the verity search engine. CF9 you can choose between Verity/SOLR and CF10 will be SOLR. Railo runs solr.
The reason I make reference to the search engine is that the version of verity which ships with Adobe CF is limited to collections of 250k in size.
Create a collection with CFCollection
The first thing we need to do when setting up a site search is to create a collection. A collection is a file set on the server which contains all the indexed data for a datasource(s). This is where we will pull our search results from.
To create and manage an index we use the "<cfcollection>" tag. This tag takes several attributes but the ones we are interested in are...
- Action: create,delete,list,map,optimize and categorylist(verity only)
- Path: The physical path on the server to where we want to store our collection.
- Collection: The name we want to give our collection
- Engine: "verity or solr" if using CF9. Ignored if using Railo.
As a good habit when I create a new collection I first check if a collection of the same name already exists. In this example I am going to create a collection of all my published blog entries.
<!--- Set the path to where we store our collection--->
<!--- Get a list of existing collections --->
<cfcollection action="list" name="collectionList">
<!--- Convert the collections name column to a value list --->
<cfset collectionList = valueList(collectionList.name)>
<!--- If a collection with that name does not already exist --->
<cfif NOT listFind(collectionList,"publishedBlogs")>
<!--- Create the collection --->
Note: Do not delete a collection using a file delete as it will still be registered. Instead use the action="delete" attribute for the cfcollection tag.