14 nov 07
Recently I’ve been exploring search options for my site. Now, I’m really not so vain as to think there’s a pressing need to search my content, although from time to time I’ve done so. And as always it’s fun to play around with, especially when there’s integration involved.
First, I thought about “rolling my own” search. Django provides plenty of tools for this, like icontains, or, more importantly, search, which uses MySQL fulltext indexing. This is still a viable option, and the admin, if I’m not mistaken, uses icontains for the fields you specify and works alright. Wilson Miner and Jeff Croft both appear to be rolling their own search, with good results.
Nonetheless, I thought I’d check out Google’s search API, seeing as there is no way I could know more about searching than they do. They offer a custom search engine, which of course works great because it’s Google, but ultimately turned me off. Why? Well, they offer two flavors for integration, one being an IFrame (yuck) and the other being an AJAX thing (also yuck). With the IFrame, you get virtually no way to customize the results to fit with the look of your site (they let you change the link color). With the AJAX option, it just seemed like it was going to be too much work to really customize it, and it didn’t clearly provide a linkable URL for a particular search.
Google used to have a SOAP API to their search, so you could get the results server side and display them to your liking. Yay! They discontinued it.
Luckily, Yahoo! does open their search API in this way, although thankfully they are REST services. And, thanks to pYsearch, it is incredibly easy to use this in your Django app. The view can be as simple as this:
from django.shortcuts import render_to_response
from django.conf import settings
from yahoo.search import web
def search(request):
query = request.GET.get(‘q’, None)
site = [‘www.notmyself.com’]
results = None
if query:
search = web.WebSearch(settings.YAHOO_ID, query=query, site=site)
results = search.parse_results()
return render_to_response(‘search.html’, {‘query’: query, ‘results’: results})
It is that easy! Note I’m pulling in my application ID from my Django settings file. And of course, pYsearch offers you more options than the ones I’m using here.
(Thanks to Chris for alerting me of the Yahoo! search API’s existence.)