googlesearch
Class Query

java.lang.Object
  extended by googlesearch.Query
Direct Known Subclasses:
CompositeQuery, SimpleQuery

public abstract class Query
extends java.lang.Object

Encapsulates a Google search query. It will let you search the Google indexes from a program just as you can from Google.com

This is version 2. It has the following differences from V1. (a) it uses Java 5 for its collections (templates). (b) it constructs the query string as late as possible, using a recursive look at the decorator chain that defines it. As such, it removes some responsibility from the user to construct a correct query. For exampmle it will guarantee that site: searches put the site: attribute last in any query. To use this you will need to get your own Google Key by registering at http://www.google.com/apis/ You will also need to download the googleapi.jar file from there and put it into your project.

Created on Apr 12, 2006

Author:
jbergin

Field Summary
static Query NULL_QUERY
          An empty Query.
 
Constructor Summary
Query()
           
 
Method Summary
abstract  java.lang.String buildSearchString(java.util.List<Query> start, java.util.List<Query> finish)
          Used by getSearchString to build up the query string recursively from the parts.
static java.lang.String getQueryKey()
          Get the Google search key that you set with setQueryKey.
abstract  java.lang.String getSearchString()
          Obtain the search string specified when the object was created.
 SearchResultPage lucky()
          Return the top ranked page for this result
 java.lang.String modifier()
          Used only by queries that must place their query text first or last in the full query string.
 SearchResult search()
          Search for the highest ranked results.
 SearchResult search(int start)
          Return up to ten search result pages starting with the index (ranking) you specify.
static void setQueryKey(java.lang.String googleKey)
          Set the Google search key.
static java.lang.String suggestSpelling(java.lang.String trial)
          Ask the Google servers to suggest spellings for a possible search "trial".
 java.lang.String toString()
           
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

NULL_QUERY

public static final Query NULL_QUERY
An empty Query. Searching with the query will return a SearchResult.NULL_RESULT instead of a valid SearchResult.

Constructor Detail

Query

public Query()
Method Detail

search

public SearchResult search()
Search for the highest ranked results. Up to ten result pages are returned.

Returns:
The search results object with up to ten result pages

search

public SearchResult search(int start)
Return up to ten search result pages starting with the index (ranking) you specify.

Parameters:
start - the (zero based) index at which to start
Returns:
the search results object with up to ten pages

lucky

public SearchResultPage lucky()
Return the top ranked page for this result

Returns:
the top ranked page.

toString

public java.lang.String toString()
Overrides:
toString in class java.lang.Object

getSearchString

public abstract java.lang.String getSearchString()
Obtain the search string specified when the object was created.

Returns:
the search string

buildSearchString

public abstract java.lang.String buildSearchString(java.util.List<Query> start,
                                                   java.util.List<Query> finish)
Used by getSearchString to build up the query string recursively from the parts. Note that the work is done on the unwinding phase of the recursion. Those query fragments that must appear first or last in a query string defer being added to the string until the end, by putting a reference to themselves intoone of the two lists. These lists are then processed at the end after some verification.

Parameters:
start - the list of query objects that must come first in a Google query
finish - the list of query objects that must come last in a Google query. e.g. site:
Returns:
the query string

modifier

public java.lang.String modifier()
Used only by queries that must place their query text first or last in the full query string. See SiteQuery, for example.

Returns:
the modifier text that should be pre/post pended to the rest of the query.

suggestSpelling

public static java.lang.String suggestSpelling(java.lang.String trial)
Ask the Google servers to suggest spellings for a possible search "trial".

Parameters:
trial - the trial string that you think may be misspelled
Returns:
the google suggested spellings. Often blank.

getQueryKey

public static java.lang.String getQueryKey()
Get the Google search key that you set with setQueryKey.

Returns:
the Google-supplied search key.

setQueryKey

public static void setQueryKey(java.lang.String googleKey)
Set the Google search key. This is supplied by Google when you register for this service.

Parameters:
googleKey - the Google-supplied search key.