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.
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.
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.
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.
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.
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,”
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!