Monday, 9 July 2007

Adding Google as a search scope

Well the title is prehaps miss leading but you can trick MOSS in to using Google as a search scope.

Firstly how do we get google working with in a Moss site
As Google is droping thier Web service api (which was limated to 1000 quires a day) and are promoting thier Ajax API i will describe how to use the Ajax API.
We will use a CWEP (Content Editor Web Part) and drop some custom code in to the Source section.


<link href="http://www.google.com/uds/css/gsearch.css" type="text/css" rel="stylesheet">
    <script src="http://www.google.com/uds/api?file=uds.js&amp;v=1.0&key=REPLACE_ME" type="text/javascript"></script>
    <script language="Javascript" type="text/javascript">
    //<![CDATA[     function OnLoad() {       // Create a search control       var searchControl = new GSearchControl();       // Add in a full set of searchers       searchControl.addSearcher(new GwebSearch());       searchControl.addSearcher(new GvideoSearch());       searchControl.addSearcher(new GblogSearch());       searchControl.addSearcher(new GnewsSearch());       searchControl.addSearcher(new GbookSearch());              // draw in tabbed layout mode       var drawOptions = new GdrawOptions();       drawOptions.setDrawMode(GSearchControl.DRAW_MODE_TABBED);       searchControl.draw(document.getElementById("search_control_tabbed"), drawOptions);       // Execute an inital search       searchControl.execute(cleanup(getQval("k")));     }     GSearch.setOnLoadCallback(OnLoad);     //]]>
    function cleanup(str)
    {
      str = str.replace(/%22/ig, "\"");
      str = str.replace(/\+/ig, " ");
      str = str.replace(/%20/ig, " ");
      str = str.replace(/%2B/ig, "+");
      return str;
    }
function getQval(name)
{
var m, Q = window.location.search.substring(1);
if ('' != Q)
{
var re = new RegExp(escape(name) + '=([^&$]+)');
if (m = Q.match(re))
return m[1];
else return '';
}
return '';
}


    </script>
<div class="search-control" id="search_control_tabbed">Loading</div>
<div id="error"></div>




Replace the REPLACE_ME with your google API key which you can get here
http://code.google.com/apis/ajaxsearch/signup.html
NOTE espaly for corprate clients they must read and approve of the tearms and conditions in relation to Google's API key

The Google part of the code is in the OnLoad()
and you should be able to easly modfy it to your own needs just explor the API

the rest of the fuctions are there to tokenise the URL string so make the search work from a search scope
the cleanup(str) fuction is used to reformat the URL token in to plain text.

the getQval(name) Gets the url tokens for the search string. Since i am just lazy and not much of a javascript programer this is just a cut and past from this Forum all credit goes to RobB and Michael Winter.
http://www.thescripts.com/forum/thread149745.html

now you have a google search webpart
go to the Search colection in your portal
Add two pages one for Google Search and one for Google Search Results.
Add these pages to the relative tabs.
Drop a Cwep whith the above code.
you now have a search page and a results page that contain a Google search web part

now how to get Moss search scopes to point the results here

go to search scopes ehter in the SSP or at the toplvl of the site colection(recomended for this method)
Create a display groop and call it Google Search Group (or your own name)
Creat a search scope called Google Search that is atached to Search Dropdown and Google Search Group that uses a custon search results page. Point to your newly created Google search results Page were you added the Cwep.

add a rule that will not search anywere (point it to the google search page you created for a minimal search)

now you will have a google search in your defauld search dropdown and any search placed in there will open your Google Search Results page which will display the search results in the Cwep.

one last thing to fix

go edit page on your Google search page and go Modfy sheard webpart on the Search box
Hide the search dropdown and under Mics change the results page to your new Google Search Results and change the Scope (txt box just below) to Google Search Group.
repeat these steps on your Google Search Resluts page

sorry i was in a hurry with this post :P

1 comment:

Fintan said...

Thanks for posting this. I tried your steps but it didn't work for me I am afraid. The CEWP just displays 'Loading' (in both the search and results pages) and no Google search results are returned.