Search suggestions in Firefox


I notice that the milestone release of Firefox 2 has implemented a mechanism for "Google Suggest"-style auto-completions right in the browser's search box. Very neat feature.

I haven't pinged Gavin yet about whether or not this feature will be exposed in the new search plugin model. This document hints that it might be; perhaps via an additional element called <SuggestionsURL/>. Of course, those are early design docs, not formal specifications, so I don't want to read too much into anything.

And Google/Firefox developer Tony Chang wrote earlier today about a Wikipedia Suggest implementation he cranked out. Tony's impressively concise "80 lines of Python" lead me to believe that the element might be called <SuggestionURL/> (no "s") and live in the OpenSearch Description document.

But now that I'm thinking about it, one appropriate way to expose the "suggest" URLs would be by reusing the existing <Url> element in the OpenSearch Description document. The type attribute would indicate that the URL is for a "suggestion" search. The docs recommend returning JSON; this too can be accounted for in the type value.

The type I have in mind is:

application/x-suggestions+json


Thus a full "suggest" URL element would look something like this (using Google's API as an example):

<Url type="application/x-suggestions+json" 
     template="http://www.google.com/complete/search?hl=en&amp;js=true&amp;qu={searchTerms}"/>


A full OpenSearch Description document, with a "x-suggestions" type:

<?xml version="1.0" encoding="UTF-8"?>
<OpenSearchDescription xmlns="http://a9.com/-/spec/opensearch/1.1/">
  <ShortName>Web Search</ShortName>
  <Description>Use Example.com to search the Web.</Description>
  <Tags>example web</Tags>
  <Contact>admin@example.com</Contact>
  <Url type="text/html" 
     template="http://example.com/html/{searchTerms}&amp;pw={startPage?}"/>
  <Url type="application/atom+xml" 
     template="http://example.com/atom/{searchTerms}&amp;pw={startPage?}"/>
  <Url type="application/x-suggestions+json" 
     template="http://example.com/suggest/{searchTerms}"/>
</OpenSearchDescription>


Clients that support "application/x-suggestions" could thus explicitly look for this type and call the URL accordingly. Also, the "content type+format" notation will help if search engines and clients decide to support "+xml" or similar some time in the future.

Another nice thing about doing it this way is that it reuses the convention of {searchTerms} as a template parameter. The other way simply tacks on the search at the end of the string. This would be a bit more flexible and potentially support more suggestion APIs.

Yet another plus is that this approach doesn't even require an extension to OpenSearch. Clients that want suggestion URLs will look for it by type; clients that don't will silently ignore it.

This approach feels right to me, but I'd like to sleep on it.

[Update: Fixed the encoding of the '&' characters in my sample URLs.]

[Update 2: Fixed typos.]