Understanding the Google API Query

The core of a Google application is the query. Without the query, there’s no Google data, and without that, you don’t have much of an application. Because of its importance, it’s worth taking a little time to look into the anatomy of a typical query.

Query Essentials

The command in a typical Perl-based Google API application that sends a query to Google looks like this:

my $results = $google_search ->
  doGoogleSearch(
    key, query, start, maxResults, 
    filter, restrict, safeSearch, lr, 
    ie, oe
  );

Usually, the items within the parentheses are variables, numbers, or Boolean values (true or false). In the previous example, I’ve included the names of the arguments themselves rather than sample values so that you can see their definitions here:

key

This is where you put your Google API developer’s key. Without a key, the query won’t go very far.

query

This is your query, composed of keywords, phrases, and special syntaxes.

start

Also known as the offset, this integer value specifies at what result to start counting when determining which 10 results to return. If this number were 16, the Google API would return results 16-25; if 300, results 300-309 (assuming, of course, that your query found that many results). This is known as a zero-based index, since counting starts at 0, not 1. The first result is result 0, and the 999th, 998. It’s a little odd, admittedly, but you get used to it quickly—especially if you go on to do a lot of programming. Acceptable values ...

Get Google Hacks, 2nd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.