Update: If you enjoy reading articles on programming or web development, please see our sister blog here: devblog.techhead.biz
Google is making it too easy for the modern web developer. I remember writing a brute-force search engine in Perl for a small web site about 11 years ago. It was slow, resource intensive, and would not scale well with a site of any size.
Skip forward a bit. Google now provides the ability to add a custom search to your site at no cost. Unless yours is for a non-profit organization, displaying ads is mandatory, but you do have the option to share in the profit (or pay to have the ads removed).
Google Custom Search is really quite flexible. You can tell it what sites to include or exclude. You can have it boost the ranking of a given number or sites or simply filter out all the sites that don't meet your criteria. Custom Search even gives you the ability to generate these rules on the fly so that results can be tailored to a particular user or a particular page of the site.
This article is not intended to be an in-depth tutorial on Custom Search. For documentation, see here: http://www.google.com/coop/docs/cse/
The definition of your search engine can either be Google hosted or you can host it yourself (This is termed a Linked CSE). For context-sensitive search, yours must be a Linked CSE.
Custom Search Engines (CSEs) are defined by a simple XML specification. This specification may be generated dynamically for interesting results. Below is a very simple example of a dynamically generated CSE which parses all of the links out of the referring page in order to include only those sites within the search results. The code snippet is written in PHP.
<?php
header('Content-type: text/xml');
$url = $_SERVER['QUERY_STRING'];
if (!$url) $url = $_SERVER['HTTP_REFERER'];
echo <<<CSE
<?xml version="1.0" encoding="UTF-8"?>
<GoogleCustomizations>
<CustomSearchEngine keywords="" language="en">
<Title>TECHHEAD</Title>
<Description>The search for All Things Tech</Description>
<Context>
<BackgroundLabels>
<Label name="techhead_site" mode="FILTER"/>
</BackgroundLabels>
</Context>
</CustomSearchEngine>
CSE;
if ($url) {
$url = urlencode(urldecode($url));
echo <<<CSE
<Include type="Annotations"
href="http://www.google.com/cse/tools/makeannotations?url=$url&label=techhead_site"/>
CSE;
}
echo <<<CSE
<Annotations>
<Annotation about="*.techhead.biz/*">
<Label name="techhead_site"/>
</Annotation>
</Annotations>
</GoogleCustomizations>
CSE;
?>
Notice how certain annotations are included dynamically based on $url (which is simply the HTTP referer of the script). Annotations are for matching URL patterns with labels. The CSE is set to FILTER out any site that isn't part of "techhead_site". So the annotations define what is to be considered for inclusion in the search results.
Now we have a dynamic CSE definition. To use it, paste the appropriate HTML code snippets (http://www.google.com/coop/docs/cse/cref.html) into your web page or blog template. You will need to substitute the value of the hidden form parameter "cref" for whatever the URL to your PHP program is. For a more concrete example, simply look at the page source of this blog at http://blog.techhead.biz
For a much better Google Custom Search tutorial, see here: http://www.free-your-mind.info/ Then come back to fill in the blanks and make your search engine context-sensitive.
