Chapter 19. Integrating Search

Table of Contents

Google Ajax Search
Manipulating Search Results
Yahoo! Search
Yahoo! Images
Microsoft Search
Google Desktop HTTP/XML Gateway

No one needs to be reminded that search engines are at the heart of the current web infrastructure. Not surprisingly, it’s useful to be able to integrate search functionality and search results into mashups. If a mashup is integrated with search engines via their APIs, users of the mashups can more easily find and reuse that digital content.

This chapter shows how to use the Google, Yahoo!, and search APIs, as well as configuring searchable web sites for access as a search ­plug-­in in Firefox 2.0 or Internet Explorer 7 using OpenSearch. This chapter will also examine briefly how to use the Google Desktop Search API.

Google Ajax Search

Google was one of the first major search companies to provide an API: the Google SOAP API. Since December 2006, no new developer keys have been issued because Google is directing users to its newer Ajax Search API, which we will now study.

The Google Ajax Search API ( gives you a search widget that you can embed in your web site. You can access functionality for searching the Web, doing local searches (tied to maps), and doing video searches. The widget displays a search box and takes care of displaying search results in an HTML element that you designate.

Like Google Maps, you have to sign up for a key that is tied to a specific directory; you can do that here:

Paste the “Hello, World” code into your page, and load it.[323]

Manipulating Search Results

Let’s adapt the basic code to let a user search a particular search source (the web search) and save a result. This is done by creating a callback (KeepHandler) with the setOnKeepCallback method. You’ll also see some code to access the attributes of the result.[324]

            <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
            <html xmlns="">
                <meta ?http-?equiv="content-type" content="text/html; charset=utf-8"/>
                <link href="" type="text/css"
                <script src=";v=1.0&amp;key=[KEY]"
                <script type="text/javascript">
                function KeepHandler(result) {
                  // clone the result html node
                  var node = result.html.cloneNode(true);
                  // attach it
                  var savedResults = document.getElementById("saved_results");
                  // extract some info from the result to show to get at the individual
                  // attributes.
                  // see
                  var title = result.title;
                  var unformattedtitle = result.titleNoFormatting;
                  var content = result.content;
                  var unescapedUrl = result.unescapedUrl;
                  alert("Saving " + unformattedtitle + " " + unescapedUrl + " " + content);
                function OnLoad() {
                  // Create a search control
                  var searchControl = new GSearchControl();
                  // attach a handler for saving search results
                  searchControl.setOnKeepCallback(this, KeepHandler);
                  // expose the control to manipulation by the JavaScript shell and Firebug.
                  window.searchControl = searchControl
                  // Add in the web searcher
                  searchControl.addSearcher(new GwebSearch());
                  // Tell the searcher to draw itself and tell it where to attach
                  // Execute an initial search
                <div id="search_control"></div>
                <div id="saved_div"><span>Saved Search Results:</span>
                <div id="saved_results"></div></div>

There’s obviously more you can do with the Google Ajax Search API, such as styling the search widget. Consult the documentation to learn how. Here are some noteworthy extras:

Indeed, you can learn plenty of things for your specific applications from the sample code:

For those of you who are looking for a way of using Google search without creating an HTML interface, take a look specifically at the following:

This sample gets the closest to giving you back the raw search functionality that the SOAP interface has, although you still need to use JavaScript and embed that search in a web page on the public Web.