Posted in ColdFusion | Posted on 04-07-2007 | 3,184 views
This morning I posted a quick example of using AJAX with a server side search. I mentioned that one of the reasons to do so would be to employ more powerful searching technologies, like Verity. So I've put my money where my mouth is (money, is someone getting money?) and built a quick demo.
To see this new version in action, go here:
http://ray.camdenfamily.com/demos/sprysearch/index2.cfm
I made two changes here. First - the back is using Verity. I won't bother showing the code for that as all I did was make a collection of my blog entries (as of this morning) and provided a quick search interface to it. Right off the bat this gives me all the power of Verity from my search interface. I can search for "coldfusion and lost" and Verity will automatically handle the AND.
I also made use of the Suggestions feature of Verity. This asks Verity to provide suggestions for better searches. To return the suggestion, I modified my XML result. My earlier result looked like so:
2 <result> stuff </result>
3 <result> more stuff </result>
4</results>
I took the suggestion result and added it to my XML:
2 <result> stuff </result>
3 <result> more stuff </result>
4</results>
So how then do I use this in Spry? Spry will take your XML and turn it into a nice dataset. But I need the XML. If you remember from the last example, I had a function running when the data loaded. I added some new code to this function:
2var xmldom = dataSet.getDocument();
3//convert to simple b
4var xmlob = Spry.XML.documentToObject(xmldom);
5//get suggestion
6var suggestion = xmlob.results["@suggestion"];
7if(total == 0 && suggestion != $("searchterms").value) {
8 results.innerHTML += "<br />Try searching for " + suggestion;
9}
First I get the XML Document from the dataset. Next I convert this into an object. The documentToObject function is a Spry utility that makes it easier to work with XML data. It turns into a simple object that is much easier to work with, as you can see where I grab the suggestion attribute. I then do a quick sanity check - did I get no results and is the suggestion actually different? If both are true, then I print out the suggestion.
Be sure to view source on the demo for the full code. One quick note - Verity will throw an error if you don't pass in a nice search string. There is a nice UDF, verityClean, at CFLib that would have taken care of this. If you use Verity, be sure you use this function, or something like it.


One minor annoyance: When you type a search term and hit the Enter key the page refreshes with no results. You could change that onClick to a onsubmit in the form tag and have it "return false" after the AJAX function call.
It might help others to know that
$("searchterms") and $("resultText")
Should be changed to
Spry.$("searchterms") and Spry.$("resultText")
I'm sure the info is in the docs but just thought it might spare some a headache.
Peter
[Add Comment] [Subscribe to Comments]