Thursday, September 28, 2006

Today's discovery: Google AJAX Search API

Look around a little... Do you see that Google Search text box at the top right corner of my blog? :-) Yes..there you go. That is the latest addition to my blog template and will remain there for some time at least. And today's blog is going to be about this little discovery I had today.

Google AJAX Search API (http://code.google.com/apis/ajaxsearch/)is a Javascript library that can be used to embed our very own Google Search in our web pages and web applications. The library provides simple-to-use Javascipt objects that perform inline searches over a number of Google services, which interestingly include:

1. Google Web Search (this is our very own google search engine)
2. Google Video Search (this is something interesting, especially from the point of view of one of the projects we might be doing)
3. Google Blog Search
4. Local Search
5. Google Map Search

Steps to follow to include Google Search within a web page:

1. Include the URL of the Google AJAX Search API Javascript library via a <script> tag in the web page as follows:
<script src="http://www.google.com/uds/api?file=uds.js&v=0.1&key=ABCDEFG" type="text/javascript"></script>

Note the "key" parameter. The value needs to be replaced by your key that you can get by signing up for one. (This seems a little unfriendly, discussed below as a down-side)

2. Include the CSS stylesheet that is used by the Google search component:
<link href="http://www.google.com/uds/css/gsearch.css" type="text/css" rel="stylesheet"/>

3. GSearchControl is the main class of the API that co-ordinates the search across different services. These services are to be added as children of this class.

4. GlocalSearch, GwebSearch, GblogSearch and GvideoSearch are the objects representing the various services. One or more of these can be added to the GSearchControl component using the "addSearcher" method.

5. Layout option: All these different searchers can be added on one page. They can be laid out linearly or in a tabbed page. Take a look at the google search I have added to my blog site. When you search for something, three tabs come up - Web Search, Google Blog Search and Blogger Search.

6. The control is drawn using the "draw" method of GSearchControl

7. When the user enters a search expression in the search textbox and clicks Search button, the "execute" method of the GSearchControl object is to be called to perform the actual search

8. The results are displayed inline. The number of results to be shown by default can also be controlled.

Some good points about this API:

1. With this API, I can very easily include Search capabilities in my web pages
2. The Video Search feature also provides a Playback feature using which I can play videos in the web page itself
3. The search results could be distributed or shared with other people. This is done by providing a "Copy" option below the links. So, when I click on the "Copy" option a callback function can be called. In this function, I can write my own logic for storing / copying these links. Makes a lot of sense in a "Bookmarks" kind of an application I am trying to develop right now. So the user can search for new URLs from within my application and store them as bookmarks then and there.

Some of the down-sides as of today:

1. This is still an experimental API, current version is 0.1. Google will be coming up with a version 1.0 soon which is expected to be a little different from this version. So at that time people might also have compatibility issues. And since this is just a basic first version, it will have bugs and early adopters will have to face some issues.

2. To use Google AJAX Search API in your web pages, you need to sign up for a key for a particular URL. The idea is that one can use that key in all the web pages under that directory. I am finding this a little tricky (especially when it comes to testing out applications that I am testing on my local Tomcat server) and also a little fishy, in terms of business use.

3. Browser compatibility: Currently supports Firefox, IE 6, and Safari, with support for additional browsers coming soon..

All in all - pretty exciting to try this out in some of the web applications.

P.S.: If you want to add Google Search capability to your blog, let me know :-)

No comments: