Everyone and their dog seems to have written an example of how to use the Yahoo! Search BOSS API to build a simple search tool. I wanted to take that one step further and build something that would serve both as an example and as a usable service, and that could be extended and enhanced by other developers. Since my day job involves constant tradeoffs between making Yahoo! Search slower (by adding features) and making it faster (by optimizing those features), my primary goal here was to make something as fast as technically possible.
To do this, I wrote a very simple JavaScript module called Trogdor that uses dynamic script nodes to make cross-domain JSONP requests to the BOSS API as you type your query. Search results are returned and rendered almost instantly on each keystroke, and you can use the up and down arrow keys (or tab) and enter to quickly select the result you want—no mouse necessary.
Trogdor doesn’t require a JavaScript framework and works great in all modern browsers (and even some ancient, crappy browsers like IE6). The entire package (HTML, CSS and JS) weighs just a smidge under 2KB after minification and gzip, and it’s wonderfully fast.
Try it out for yourself at pieisgood.org/search and be sure to grab the heavily-commented source code on GitHub. If you’ve got ideas for features and improvements, fork the repo and go nuts (and be sure to let me know what you come up with). You’re also welcome to use Trogdor (modified or unmodified) in your own projects, although I do ask that you please use your own BOSS API key rather than the one included in the example.
Update, 11/26: Changed the name of the library from FastSearch to Trogdor, since dragons are awesome (and apparently there’s a Microsoft search product called FastSearch).
Comments
Great example.
I really dig how fast the results render, cool example as well. Thanks.
you shoulda snuck ramaze in there somehow...
:-)
Great work!
Re: you shoulda snuck ramaze in there somehow...
Actually, I’ve already written a Yahoo! Search helper for Ramaze as part of Thoth. It’s what powers the search feature on wonko.com. You can find the source here. It uses the old Search API though, since I wrote it pre-BOSS.
very cool
very cool ryan!
(and yes, i have an example of how to do use yahoo boss to build a search tool too! )
Nice - amazingly fast response
But how long until the cease and desist from Homestar Runner?
seems to be b0rken
http://pieisgood.org/search doesn’t work anymore
actually i think it's google's chrome that is broken
it works in firefox, but not in google chrome (1.0.154.42). i get these errors in the javascript console:
Uncaught SyntaxError: Unexpected number http://boss.yahooapis.com/ysearch/web/v1/p?appid=C82ywM3V34FSPr8EUqeE61WL6zQ1psBVArWY62MoVxyuljPRIfkcdnWSKDTKBuTkFsyMAkvp9Q--&format=jsonp&callback=Trogdor.results%5B'p'%5D (line 1)Resource interpreted as script but transferred with MIME type text/html. http://boss.yahooapis.com/ysearch/web/v1/p?appid=C82ywM3V34FSPr8EUqeE61WL6zQ1psBVArWY62MoVxyuljPRIfkcdnWSKDTKBuTkFsyMAkvp9Q—&format=jsonp&callback=Trogdor.results%5B’p’%5D
fix for google chrome
looks like encodeURIComponent doesn’t deal with quotes on chrome. So I added a replace() at the end:
callback: encodeURIComponent(“Trogdor.results[’” + query.replace(/’/g, \“\\’”) + “’]”).replace(/’/g,“%27”),
Re: fix for google chrome
Thanks Joe, I’ve implemented the fix.