Raymond Camden's Blog Rss

Using AJAX and Server Side Search (2)

8

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:

view plain print about
1<results>
2 <result> stuff </result>
3 <result> more stuff </result>
4</results>

I took the suggestion result and added it to my XML:

view plain print about
1<results suggestion="search for this instead">
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:

view plain print about
1//Look for the suggestion
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.

Comments

[Add Comment] [Subscribe to Comments]

Another nice example. But I wonder why "poop" is returned as the suggestion when 0 results are found?
Because I'm childish? I have no idea. :) I know I did test with poo and poop. It's silly, but I tend to use them as much as foo and goo.
Nice work! I'm going to have to implement that!

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.
Yeah, I saw that and was too lazy to fix it. ;)
I fixed the "hit enter no worky" thing.
I was confused to find that my implementation of this stopped working when I updated SpryAssets files from labs. Specifically SpryData.js - version 0.30 to version 0.35.

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
I did see it in the release notes, but it is definitely worth mentioning. I'll post a blog entry today and credit you.
I wanted to know if anyone has made this work. Can i get the search.cfm.

[Add Comment] [Subscribe to Comments]