Chapter 19. Integrating Search

Table of Contents

Google Ajax Search
Manipulating Search Results
Yahoo! Search
Yahoo! Images
Microsoft Live.com Search
OpenSearch
Google Desktop HTTP/XML Gateway
Summary

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 Live.com 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 (http://code.google.com/apis/ajaxsearch/) 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:

http://code.google.com/apis/ajaxsearch/signup.html

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"
            "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
            <html xmlns="http://www.w3.org/1999/xhtml">
              <head>
                <meta ?http-?equiv="content-type" content="text/html; charset=utf-8"/>
                <title>google.ajax.2.html</title>
                <link href="http://www.google.com/uds/css/gsearch.css" type="text/css"
                      rel="stylesheet"/>
                <script src="http://www.google.com/uds/api?file=uds.js&amp;v=1.0&amp;key=[KEY]"
                        type="text/javascript"></script>
                <script type="text/javascript">
                //<![CDATA[
            
                function KeepHandler(result) {
                  // clone the result html node
                  var node = result.html.cloneNode(true);
            
                  // attach it
                  var savedResults = document.getElementById("saved_results");
                  savedResults.appendChild(node);
            
                  // extract some info from the result to show to get at the individual
                  // attributes.
                  // see http://code.google.com/apis/ajaxsearch/documentation/reference.html
                  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
                  searchControl.draw(document.getElementById("search_control"));
            
                  // Execute an initial search
                  searchControl.execute("flower");
                }
                GSearch.setOnLoadCallback(OnLoad);
            
                //]]>
                </script>
              </head>
              <body>
                <div id="search_control"></div>
                <div id="saved_div"><span>Saved Search Results:</span>
                <div id="saved_results"></div></div>
              </body>
            </html>
         

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:

http://code.google.com/apis/ajaxsearch/samples.html

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:

http://www.google.com/uds/samples/apidocs/raw-searchers.html

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.