Previous Section  < Day Day Up >  Next Section

Hack 29. Weight a Query Keyword

Add more weight to a particular keyword in your Google search query for more targeted results.

As we've mentioned before, Google will provide different results based on how many times a search word is used in a query [Hack #15] .

Part of this is assumed to be because Google tries to search your query in order, giving higher relevance to words that appear in the same order in a page as they do in your query. (In other words, when you search for baseball baseball, Google tries to find, and assigns more weight to, pages that have the phrase "baseball baseball" in them, even if you don't use quotes in your search.)

Google also seems to look for multiple iterations of the same word in your result pages. So when you search for baseball baseball baseball, Google looks for the word baseball three times in your result pages.

This is a nifty and little-known search trick when you want to emphasize a particular word in your search, but typing in the same word several times is a drag. This hack automates the process for you, adding weight to the keyword of your choice and sending you on to Google for results.

2.11.1. The Code

Save the following code as a CGI script ["How to Run the Hacks" in the Preface] named sinker.cgi in your web site's cgi-bin directory.

This script doesn't require the Google API! Instead, it directly opens a Google search result URL.


# sinker.cgi

# Weight a specific keyword in a Google query.

use strict;

use CGI qw{:standard};

# Display the query form if both a query and sinker are not provided.

unless (param('query') and param('sinker')) {


    header( ),

    start_html("Search Sinker"),

    h1("Search Sinker"),


    'Query: ', textfield(-name=>'query'), p( ),

    'Sinker (word to weight): ', textfield(-name=>'sinker'), p( ),

    submit(-name=>'submit', -value=>'Search'), p( ),

    "You will be taken straight to a Google results page. Be sure to look in the Google 

query box at the top of the page to see how your query turned out.",

    end_form( ), p( );


else {

  # Normalize the sinker and prefix with a space.

  my($sinker) = param('sinker') =~ /^\s*(.+)\s*$/;

  $sinker = " $sinker";

  # Build the Google query URL.

  my $query = param('query') .  $sinker x ( 10 - (scalar split / /, param('query')) );

  # Webify the query.

  $query =~ s/([^a-zA-Z0-9 ])/"%".sprintf("%2.2x", unpack("C", ($1)))/eg;

  $query =~ tr/ /+/;

  # Redirect the browser to Google, sinker search in tow

  print redirect("$query");


2.11.2. Running the Hack

Point your browser at the sinker.cgi script on your web server. In the form, enter any query in the Query field and the word you want to emphasize in the Sinker field and click the Search button. You'll be redirected to a Google results page chock-full of up to 100 results.

Be sure to take a gander at how your query turned out in the Google search box at the top of the results page. If, for instance, you queried for "Moises Alou" with a sinker of baseball, the resulting query sent on to Google would look like this:

"Moises Alou" baseball baseball baseball baseball baseball baseball baseball baseball

    Previous Section  < Day Day Up >  Next Section