The Google Enterprise Search XML API and Ruby on Rails
October 25th, 2007by Jeremy Thomas
I’m a Java and C# guy. I’ve grown to be very comfortable with the two languages and associated frameworks (J2EE, ASP.NET), and can write programs that at least compile in each without having to search through the internet for examples. Hence my hesitation to learn Ruby - the language required for me to indulge in Ruby on Rails. But everybody’s doing it, and I don’t want to be left behind.
I decided to build a Ruby on Rails-based enterprise search application leveraging the Google Search Appliance (GSA) REST API. My aim was to show that A) Ruby on Rails is an agile framework leading to quick application development and B) the extensibility of the Search API (being that it can be leveraged from virtually any programming framework).
Here’s what I did:
I created controller class called Search Controller that manages the API invocation. This is done in an action handler method called on_search. This method invokes the GSA API, then converts the XML response into a REXML::Document object (part of the Ruby on Rails library.

url_escape is a nifty little method I found here that converts a search term like, say “technology integration” into “technology%20integration”. If you don’t do this things will break. params[:query] contains the search term the user enters into the search box on my search.rhtml page. Last the @search_results object is stored in the session and is a container for a collection of search results plus some global information about the search (like result_count and search_time). This object is bound back to my search.rhtml page to display the search results.
Next, I had to pull relevant information out of the XML response and add it to user-friendly objects that are then used to display the results on the view:

This method leverages ‘rexml/document’ to parse XML, i.e. search_response.elements[”GSP/RES/M”].text. “GSP” is the root node in the GSA XML API response document, and in this example I’m pulling out the result count, appropriately called “M”, inside the “RES” element under the document root. Parsing continues where I iterate through each search result, pull out the relevant information (snippet, url, title), add it to a SearchResult object, then add said object to the results collection in the SearchResults object (which, again, is stored in the session).
That’s it. In two quick and easy methods I can invoke the GSA XML API and parse its response into usable objects that I bind to my view. There is a lot that I’m not doing (i.e. good error handling), but I’ve at least proven what I wanted to. I’ve done this before in Java, and it took me less time to do it in Ruby even though I was learning the framework as I went.
I love Ruby on Rails.




Follow Me
October 26th, 2007 at 3:12 am
Just wait until you start working with relational data, Ruby on Rails saves you weeks of development time.
October 26th, 2007 at 10:08 am
You’re right on that point Jake. Although, because I don’t know the framework very well at this point, I find myself re-inventing ways to use objects that are already done with the standard Rails library (like ‘Singleton’). I always find myself asking “Can it really be this simple”?
August 18th, 2008 at 11:09 am
Thanks so much for posting this information. I found it very helpful.
In addition to the “T”, “U” and “S” elements (shown in your example), I needed to pull some additional attributes out of the search results XML. Specifically, I needed to extract attributes from single-tagged elements (either freestanding, or nested within other elements). I wasn’t able to find any examples of how to do this online, so I wanted to share what I came up with. Here’s the code for parsing these attributes…
1)
Extracting the attribute of a single-tagged element nested within a fully-enclosed element is called using this REXML syntax (here, I am extracting the size attribute from the tag):
result.size = xml_result.elements[”HAS/C”].attributes[”SZ”]
2)
Extracting the attribute (VALUE) of a single-tagged element is called using this REXML syntax:
result.date = xml_result.elements[”FS”].attributes[”VALUE”]