jAndy
jAndy

Reputation: 236092

Google Ajax search API

I'm wondering, is it possible to receive google results over their own ajax API in a way like, 100 results per page?

Without a visible search field, I'd like to get the results in the background to create a progression for some search phrases.

My basic question is, what are the restrictions of the google search api ?

--update--

is it possible to change language for a search with google api ? From the start on, it just delivers from .com in english

Kind Regards

--Andy

Upvotes: 1

Views: 7466

Answers (3)

Tsvetan Filev
Tsvetan Filev

Reputation: 49

Here is my code:

<script src="https://www.google.com/jsapi?key=GOOGLE_SEARCH_KEY" type="text/javascript"></script>
<script language="Javascript" type="text/javascript">
//<![CDATA[
google.load("search", "1");

function OnLoad() {
  // Create a search control
  var searchControl = new google.search.SearchControl();
  var options = new google.search.SearcherOptions();
  options.setExpandMode(google.search.SearchControl.EXPAND_MODE_OPEN);
  searchControl.addSearcher(new google.search.WebSearch(),options);
  searchControl.setResultSetSize(google.search.Search.LARGE_RESULTSET);
  // Tell the searcher to draw itself and tell it where to attach
  searchControl.draw(document.getElementById("searchcontrol"));
}
google.setOnLoadCallback(OnLoad);
//]]>
</script>
<style>.gsc-control { width: 80%; } input.gsc-search-button { border: 1px solid black; }</style>

Upvotes: 0

Finbarr
Finbarr

Reputation: 32166

The largest number of results you can get is 64, 8 per page of the searcher.

It is possible to combine all of these into one page, but it involves the searcher making 8 calls to the Google Ajax Search API.

Further, you will need to create your own function to render the results:

var s;
var page = 1;

google.load('search', '1', {'nocss' : true});
google.load('jquery', '1.4.2'); // optional

google.setOnLoadCallback(function() {
    // T&C's state you should display branding, create a <div id="branding"></div>
    google.search.Search.getBranding(document.getElementById('branding'));
    s = new google.search.WebSearch();
    s.setResultSetSize(google.search.Search.LARGE_RESULTSET);
    s.setSearchCompleteCallback(this, searchComplete, null);
    s.setNoHtmlGeneration();
});

function searchComplete() {
    if(s.results && s.results.length > 0) {
        var results = s.results;
        for(var i = 0; i < results.length; i++) {
            var result = results[i];
            // render the results
        }
        if(page < 8) {
            s.gotoPage(page);
            page++;
        }
    }
}

For information about how to render your results see: http://code.google.com/apis/ajaxsearch/documentation/reference.html#_class_GwebResult

To change the language, add the hl argument when including the script in web pages:

<script src="http://www.google.com/jsapi?hl=en" type="text/javascript"></script>

Upvotes: 7

Gabriel
Gabriel

Reputation: 18780

http://code.google.com/apis/ajaxsearch/documentation/reference.html#_class_GSearchControl This has information about the main controller class used. It appears that the following answers your question about result size:

.setResultSetSize(switchTo)

This method is called to select the number of results returned by each of the searchers. Note, this is not a scalar. It is an enumeration that indicates either a small number of results, or a large number of results. In the future, this method may be enhanced to support medium and extra large result sets. From the sample applications, you have probably seen the more/less twiddle control at the top of the search control. This method is used by that twiddle control.

switchTo - supplies en enumeration which indicates the desired number of search results to return for each configured searcher. Valid values include: google.search.Search.LARGE_RESULTSET - request a large number of results (typically 8 results) google.search.Search.SMALL_RESULTSET - request a small number of results (typically 4 results) google.search.Search.FILTERED_CSE_RESULTSET - request up to 10 results. This will only work for Web Search queries scoped to a Filter Custom Search engine, otherwise an error will be returned. returns - n/a

Upvotes: 0

Related Questions