Build your own Search Engine with BOSS
Back in July, Yahoo! started offering the BOSS service, an open search platform. Using the BOSS API, developers have access to Yahoo’s entire search index to create custom search engines.
The goal with BOSS (Build your Own Search Service) is simple — foster innovation in the search landscape. As anyone who follows the search industry knows, the barriers to successfully building a high quality, web-scale search engine are incredibly high.
So what is BOSS?
BOSS is an open platform that offers programmatic access to the entire Yahoo! Search index via an API. BOSS allows developers to take advantage of Yahoo!’s production search infrastructure and technology, combine that with their own unique assets, and create their own search experiences. While search APIs have been available for some time, BOSS removes many of the usage restrictions that have prevented other companies from using them to build innovative new search engines.
This approach lets anyone build a search engine to their specific skills, mash the data with other sources, re-arrange results, or any other novel idea for the next big search engine. You could also easily use BOSS to add search capabilites to a pre-existing site, limiting the results to just its data.
For example, I recently had to create a wordpress plugin that used BOSS to search a fairly large site instead of using the built-in search engine. I’ve also set BOSS up on a couple of my other sites for searches.
Today, we’re going to build a simple BOSS powered search engine in PHP. To start, make sure you get an APP key from the BOSS website, then folow the code.
To start, let’s enter the beginning info:
<?php
$apikey = 'YOUR-API-KEY';
$mysite = "yourdomain.com";
$plimit = 20;
?>
Replace the text that says “YOUR-API-KEY” with the API Key you get from BOSS. The next variable is called “mysite”, this lets you specify a domain that you want BOSS to search, if you don’t want to search any specific website, then leave mysite blank.
Another variable is called “plimit”, this lets you specify how many results to display on each page. We’ll use 20 per page here, but you can change it at anytime.
Now, all that’s left to do is to search:
Finished Code:
<?php
$apikey = 'YOUR-API-KEY';
$mysite = "yourdomain.com";
$plimit = 20;
$offon1 = "checked=TRUE";
$offon2 = "checked=TRUE";
if( !isset($_REQUEST['sitesearch']) ){
$offon2 = "";
}else{
if( empty($_REQUEST['sitesearch']) ){
$offon1 = "";
}else{
$offon2 = "";
}
}
?>
<br style="clear: both" />
<form id="searchform" method="POST" >
<label for="Keywords" class="hidden">Search for:</label>
<input type="text" id="Keywords" name="Keywords" value="<?=( isset($_REQUEST['Keywords']) ? $_REQUEST['Keywords'] : null )?>"/> <input type="submit" class="submitButton" value="GO" id="searchsubmit" name="submit"/>
<?php if($mysite != ""){ ?>
<br style="clear: both" />
<input type="radio" <?=$offon1?> value="1" name="sitesearch"/>Search <?=$blogname?>
<input type="radio" <?=$offon2?> value="" name="sitesearch"/>Search WWW
<?php } ?>
</form>
<br style="clear: both" />
<?php
$start = $_REQUEST['start'];
if ($_REQUEST['Keywords'] != "") {
$thequery = urlencode($_REQUEST['Keywords']);
if( !empty($_REQUEST['sitesearch']) && $mysite != "" ){
$site = 'site:'.$mysite;
$url = "http://boss.yahooapis.com/ysearch/web/v1/$thequery+$site?appid=$apikey&format=xml&count=".$plimit;
}else{
$url = 'http://boss.yahooapis.com/ysearch/web/v1/'.$thequery.'?appid='.$apikey.'&format=xml&count='.$plimit;
}
if (!empty($start)) $url .= "&start=$start";
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, 0);
$data = curl_exec($ch);
curl_close($ch);
$results = new SimpleXmlElement($data, LIBXML_NOCDATA);
$total = $results->resultset_web->attributes();
$total = $total['totalhits'];
$results = $results->resultset_web->result;
$i = 0;
$adyet = 1;
?>
<div><?=$total?> results for <b><?=$_REQUEST['Keywords']?></b></div>
<?php
foreach ($results as $theresult) {
if( !empty($theresult->title) ){
$string = '<div>';
$string .= '<h2><a href='.$theresult->clickurl.'>'.$theresult->title.'</a></h2>';
$string .= '<p>'.$theresult->abstract.'</p>';
$string .= '<a class="url" href='.$theresult->clickurl.'>'.$theresult->dispurl.'</a>';
$string .= '<br style="clear: both" />';
$string .= '</div>';
echo $string;
if($i == 3){
echo '<br style="clear: both" />';
$i = 0;
}
}
}
?>
<br style="clear: both" />
<div id="pager">
<?php
if ($start > 0){
if ($start == $plimit)
$startParam = '';
else
$startParam = "&start=" . ($start - $plimit);
echo dalink('Prev', $self . "?Keywords=$thequery" . $startParam) . " ";
}
$page = 0;
for ($i=0; $i<$total; $i=$i+$plimit) {
$page++;
if ($i > ($start-50) && $i < ($start+50)) {
if ($i == $start) {
echo " $page ";
} else if ($i === 0) {
echo dalink($page, $self . "?Keywords=$thequery");
} else {
echo dalink($page, $self . "?Keywords=$thequery&start=$i");
}
} else if ($i == ($start-60)) {
echo '... ';
} else if ($i == ($start+60)) {
echo '... '; break;
}
}
echo " ";
if (count( $results ) === $plimit){
$next = $start + $plimit;
echo dalink('Next', $self . "?Keywords={$thequery}&start={$next}");
}
$countPages = ceil($total / $plimit);
echo " ($total results on $countPages pages)";
?>
</div>
<?php
}
function dalink($text,$url){return "<a href='{$url}'>{$text}</a> ";}
?>
And that’s it, you now have your very own Yahoo BOSS powered search engine.









Add Yours
YOU