|
What is OpenSearch?
According to the OpenSearch.org official website:
OpenSearch is a collection of simple formats for the sharing of search results.
You're more likely to know it as the format that websites use in order to let you
access their search engines directly from the search box in Internet Explorer (IE) 7 and Firefox 2.0.
While OpenSearch actually specifies a number of formats which are useful in
many different scenarios, this article assumes that you run a website and
want to use OpenSearch to point visitors to your search engine.
To that end, I'll be touching upon the the following topics:
- Creating an OpenSearch description document for your search engine.
- Adding autodiscovery links to your web site so people can easily find your OpenSearch description document.
- Specifying an icon or logo that search clients can display along with your search.
All the information and examples in this article use the most recent version
of the OpenSearch specification which as of this writing is OpenSearch 1.1 (Draft 3).

The search box in IE 7

The search box in Firefox 2.0
OpenSearch Description Document
The first step in making your site's search engine accessible via OpenSearch is
to create what's called an OpenSearch description document.
An OpenSearch description document is simply an XML file that describes your
search's interface. There are many optional elements which you can add if you
want to, but in its basic form an OpenSearch description document looks something like this:
<?xml version="1.0" encoding="UTF-8"?>
<OpenSearchDescription xmlns="http://a9.com/-/spec/opensearch/1.1/">
<ShortName>15Seconds</ShortName>
<Description>Search for .NET solutions and information at 15Seconds.com</Description>
<Tags>15Seconds 15 Seconds ASP ASP.NET .NET</Tags>
<Contact>webmaster@15seconds.com</Contact>
<Url type="text/html"
template="http://www.15seconds.com/search/search.asp?query={searchTerms}&start={startIndex?}"
/>
<Query role="example" searchTerms="asp.net" />
</OpenSearchDescription>
Most of the elements are pretty self-explanatory and when creating your own
OpenSearch description document you should obviously replace the sample
values I've included above with values which more accurately reflect the
characteristics of the search to which the document points. In particular,
please change the email address... we already get quite enough email. ;)
The two elements which do warrant a brief explanation are Url and Query.
The Url element is used to describe an interface which a client can use to request search results.
If you're simply trying to allow users to search your site via OpenSearch, then you'll only need
a Url element with a type attribute of "text/html" as shown in the example above.
Note: Although most of you won't need to include more then one, it is perfectly acceptable for a OpenSearch
description document to contain multiple Url elements.
For example, if search results are also available as XML, RSS, or Atom, you would create a Url
element for each and in the type attribute you would include the MIME type which
indicates the format in which the search results are returned.
The Url element's template attribute is where the real magic happens. This is where you
provide the URL and format that search clients should use to query your search engine. OpenSearch 1.1 defines
a number of parameters which you can use as placeholders in the template. The most important of these is
the "searchTerms" parameter which is replaced by the keyword(s) for which the client is searching.
In the example above, I've also included the "startIndex" parameter as an optional template parameter.
You indicate that the parameter is optional by adding the "?" character to the end of the parameter name.
The search client will use an empty string as a value if no value is specified for an optional parameter.
The Query element is used to define a search that a client can perform. In our scenario it's
not all that useful, but in more complex applications you can use the Query element to pass
parameters (ie. spelling corrections, number of search results, or related search terms)
back to the client. The Query element in our sample description document has the role
of "example" which indicates that it's simply included to provide search clients with
a sample query with which to test the search engine. Whatever example terms you provide are expected
to return search results.
That should be all the information you need in order to create an OpenSearch description document
for your site's search engine, but if you find you need any more information the best place to turn is to the
OpenSearch description document section of the OpenSearch 1.1 specification.
Autodiscovery Links
So now that you've created your OpenSearch description document and uploaded it to your web site, how
do you let people know where it is? That's where autodiscovery links come into the picture.
For HTML it really couldn't be much simpler. Simply add a profile attribute and a link element
to the head section of your web page that references the related OpenSearch description document which
you just created.
<head profile="http://a9.com/-/spec/opensearch/1.1/">
...
<link
rel="search"
type="application/opensearchdescription+xml"
href="/search/opensearch1.1.xml"
title="15 Seconds"
/>
...
</head>
The sections in red are the parts you'll need to add to your existing HTML document.
The values for the profile, rel, and type attributes are fine as they are above.
The href attribute should point to your OpenSearch description document and the
title attribute should be changed to the name of your search.
Once you've added the code above to your HTML file, when you load the file in a
browser you'll notice a couple of things. Near the search box there's usually a
subtle indication that the page you're on contains a link to a description document.
In IE 7, the arrow next to the search button gets an orange-ish tint, while in
Firefox 2.0 it's more of a blue-green haze around the currently selected search
provider's icon.

IE 7 search box without (top) and with (bottom) autodiscovery link

Firefox 2.0 search box without (top) and with (bottom) autodiscovery link
If you then click on the highlighted button, you'll have the option to add your
search provider to the search bar.

Add Search Provider -> 15 Seconds (IE 7)

Add "15 Seconds" (Firefox 2.0)
Once they've added your search provider to the browser the user can search your site
directly from the search box anytime they want.
Just as with the OpenSearch description document, any additional infomation you may need
is probably best obtained from the
autodiscovery section of the OpenSearch 1.1 specification.
How Do I Get My Logo To Show Up?
You'll notice that while Internet Explorer simply lists the short name of the different search providers, Firefox
takes things one step further and actually displays a small icon next to the different providers.
If your site already has a "favicon.ico" file in the site's root directory, Firefox will most likely find and
use the file automatically. If the file is named something else, isn't located in the root folder,
or you simply wish to use a different image, you can specify an "Image" element in your
OpenSearch description document.
...
<Image width="16" height="16" type="image/vnd.microsoft.icon">http://www.15seconds.com/favicon.ico</Image>
...
You can also specify multiple images of different sizes and in different formats.
For more information see
The "Image" element section of the OpenSearch 1.1 specification.
Where Can I Learn More?
As you've probably guessed the best place to learn more about OpenSearch is from the official
site, but Wikipedia also offers a good overview. I'm also including links to the search add-in
pages for both IE7 and Firefox.
Conclusion
I hope this article has helped open your eyes to just how easy it is to get started using OpenSearch.
Along with the basics I've already covered, OpenSearch 1.1 offers a lot more if you've got a use for it.
If you're looking to share search results either as a provider or as a consumer, you really should take
a closer look at the technology. In addition to defining response elements to provide search metadata
when results are returned in an XML-based format, the specification includes a number of extensions to provide
everything from relevance weighting and suggestions to a new Geo extension which aims to to provide a
standard way to query based on geographical location.
|