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.
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.
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...
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.
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.
Now that we have a somewhere to store our search index we now need to populate it with data. To do this we use the cfindex tag. In this case I am populating the index using the title and description of my blog entries.
Note how I specify title and description for the body as I want to use the description and blog titles only as valid search data. However I could have easily added the blog content itself or any available datapoints.
SELECT blogID,blogTitle,blogDescription
FROM blog
WHERE published = 1
When we use the action="update" attribute of the cfindex tag the key value determines if data should be over ridden or appended. This means that we can run a second update on the same index to add in new data and the old data will not be changed unless there is an existing record with the same key value in which case the old data would be over written.
We can also populate our index from static files or cfml templates. To do this we specify the path to the directory containing our static pages.
I have not tried mixing the two methods of indexing data but I believe that it is possible. (someone please correct me if I am wrong.)
Now that we have indexed data lets run a search.
So there you have it. The only thing left to do is build a search form to submit to the cfsearch tag and format results nicely.
CFCollection, CFIndex, CFSearch
CFCollection, CFIndex, CFSearch