How to Build your own Search Engine


In Internet world site searches have become quite poor. As Google, Yahoo and other search providers have provided their effective and target search results to web users so site searches have been disappearing by web world. Now-a-days website owners are attracted towards Google, Yahoo and Bing as there are providing free site search powered by search providers.
But a Great Thanks to Yahoo Build your Own Search Service (BOSS). Web Developers can make use of Yahoo!’s open search web services platform. Using Boss user can use his unique ideas to create his own Search Engine.
How to make your own search engine using BOSS?

Just following few steps you can create your own search engine.
  • Get a Yahoo Boss API key

  • Create class file named yahoo.boss.class.php

<?php

/************************************

FILE : yahooboss.class.php

Description : To perform Simple yahoo web search, image search and news search by using yahoo boss apis and return the resultset.

VERSION : 1.0

AUTHOR : Hastimal C. Shah

E-Mail : hasti.shah@gmail.com

**************************************/



class YahooBoss

{

private $mYahooBossAppId = '';

private $mYahooHost = 'http://boss.yahooapis.com';

private $mFormat = array('json'=>'json','xml'=>'xml');

private $mAttributes = array('count'=>'count','start'=>'start','totalhits'=>'totalhits','deephits'=>'deephits');

private $mError = '';

// Yahoo Directory Path

private $mSearchWebDirectory = '/ysearch/web/v1/';

private $mSearchImagesDirectory = '/ysearch/images/v1/';

private $mSearchNewsDirectory = '/ysearch/news/v1/';



public function __construct()

{

$this->mYahooBossAppId = 'YOUR API KEY';

$this->mYahooHost = 'http://boss.yahooapis.com';

}



public function YahooWebSearch($query,$format,$start='',$optionalParameters= '',$attributes = '',$count='',$type='')

{

/* Parameters description are as under

1-> $query contains the query or keywords to be searched.[REQUIRED]

2-> $format contains the resultset format[XML,JSON] [REQUIRED].

3-> $optionalParameters for future enhancement.

4-> $attributes for future enhancement.

*/



if('' == $query)

{

$this->mError = 'Query is blank';

return false;

}



$checkformat = $this->_CheckFormat($format);

if($type !='')

{

$this->mType = $type;

}

if(false == $checkformat)

{

return false;

}

if($count == '')

{

$count = '10';

}

if('' == $start)

{

$url = $this->mYahooHost.$this->mSearchWebDirectory.$query.'?appid='.$this->mYahooBossAppId.'&format='.$format.'&count='.$count.'&type='.$type;

}

else

{

$url = $this->mYahooHost.$this->mSearchWebDirectory.$query.'?appid='.$this->mYahooBossAppId.'&format='.$format.'&start='.$start.'&count='.$count.'&type='.$type;

}



//Get the results



$results = $this->_processCurl($url);



$websearch = array();

$output = array();



$results=$this->_processCurl($url);



if(false == $results)

{

$this->mError = 'Sys:CURL not initialized';

return false;

}



$results = simplexml_load_string($results,'SimpleXMLElement',LIBXML_NOCDATA);



$websearch['maximumresults'] = $results->resultset_web['deephits'].'';



foreach ($results->resultset_web->result as $theresult) {



$output['title'] = $theresult->title.'';

$output['url'] = $theresult->url.'';

$output['abstract'] = $theresult->abstract.'';

array_push($websearch,$output);

}



return $websearch;



}



public function YahooImagesSearch($query,$format,$start='',$optionalParameters= '',$attributes = '',$count='')

{

/* Parameters description are as under

1-> $query contains the query or keywords to be searched.[REQUIRED]

2-> $format contains the resultset format[XML,JSON] [REQUIRED].

3-> $optionalParameters for future enhancement.

4-> $attributes for future enhancement.

*/



if('' == $query)

{

$this->mError = 'Query is blank';

return false;

}



$checkformat = $this->_CheckFormat($format);

if(false == $checkformat)

{

return false;

}



if($count == '')

{

$count = '10';

}

if('' == $start)

{

$url = $this->mYahooHost.$this->mSearchImagesDirectory.$query.'?appid='.$this->mYahooBossAppId.'&format='.$format.'&count='.$count.'&filter=yes';

}

else

{

$url = $this->mYahooHost.$this->mSearchImagesDirectory.$query.'?appid='.$this->mYahooBossAppId.'&format='.$format.'&start='.$start.'&count='.$count.'&filter=yes';;

}



//Get the results



$results = $this->_processCurl($url);



$results = simplexml_load_string($results,'SimpleXMLElement',LIBXML_NOCDATA);



$imageSearch = array();

foreach ($results->resultset_images->result as $theresult)

{

if($theresult->result->abstract.'' == '')

{

$imageAbstract = urldecode($query);

}

else

{

$imageAbstract = $theresult->result->abstract.'';

}

$output['abstract'] = $imageAbstract;

$output['clickurl'] = $theresult->clickurl.'';

$output['format'] = $theresult->format.'';

$output['height'] = $theresult->height.'';

$output['mimetype'] = $theresult->mimetype.'';

$output['size'] = $theresult->size.'';

$output['thumb_height'] = $theresult->thumbnail_height.'';

$output['thumb_url'] = $theresult->thumbnail_url.'';

$output['thumb_width'] = $theresult->thumbnail_width.'';

$output['title'] = $theresult->title.'';

$output['width'] = $theresult->width.'';



array_push($imageSearch,$output);

}



return $imageSearch;

}



public function YahooNewsSearch($query,$format,$start='',$optionalParameters= '',$attributes = '')

{

/* Parameters description are as under

1-> $query contains the query or keywords to be searched.[REQUIRED]

2-> $format contains the resultset format[XML,JSON] [REQUIRED].

3-> $optionalParameters for future enhancement.

4-> $attributes for future enhancement.

*/



//$searchDirectory = '/ysearch/news/v1/';



if('' == $query)

{

$this->mError = 'Query is blank';

return false;

}



$checkformat = $this->_CheckFormat($format);

if(false == $checkformat)

{

return false;

}



if('' == $start)

{

$url = $this->mYahooHost.$this->mSearchNewsDirectory.$query.'?appid='.$this->mYahooBossAppId.'&format='.$format;

}

else

{

$url = $this->mYahooHost.$this->mSearchNewsDirectory.$query.'?appid='.$this->mYahooBossAppId.'&format='.$format.'&start='.$start;

}



//Get the results

$results=$this->_processCurl($url);

$results = simplexml_load_string($results,'SimpleXMLElement',LIBXML_NOCDATA);

$newsSearch = array();

foreach ($results->resultset_news->result as $theresult)

{

$output['abstract'] = $theresult->abstract.'';

$output['clickurl'] = $theresult->clickurl.'';

$output['date'] = $theresult->date.'';

$output['language'] = $theresult->language.'';

$output['source'] = $theresult->source.'';

$output['sourceurl'] = $theresult->sourceurl.'';

$output['time'] = $theresult->time.'';

$output['title'] = $theresult->title.'';

$output['url'] = $theresult->url.'';



//$output['filetype'] = $this->FindExtension($output['url']);

array_push($newsSearch,$output);

}



return $newsSearch;

}



public function _CheckFormat($format)

{

if('' == $format)

{

$this->mError = 'Please give the return resultset format, it is blank!';

return false;

}

else

{

$checkFormat = array_key_exists($format,$this->mFormat);

if(!$checkFormat)

{

$this->mError = 'Please give the correct format, xml or json!';

return false;

}

else

{

return true;

}

}



}



public function GetError()

{

return $this->mError;

}



public function _processCurl($url)

{

$ch = curl_init();

if(false == $ch)

{

return false;

}



curl_setopt($ch, CURLOPT_URL, $url);

curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

curl_setopt($ch, CURLOPT_HEADER, 0);

$data = curl_exec($ch);

curl_close($ch);

return $data;

}

}

?>

  • Create demo.php file

<?php

require_once("library/yahooboss.class.php");

$start = '';

$count = '';

$final_search_result = array();



$format = 'xml';

$offset = '10';

$display_record_per_page = '20';

$query = $_GET['searchq'];

$obj_yahoo = new YahooBoss();



?>

<html>

<head>

<title>Hastimal Shah : Yahoo Boss Lib</title>

<link type="text/css" rel="stylesheet" href="general.css" />

</head>

<body>

<div id="search-bar" align="center" style="padding-top:50px;">

<form id="form1" name="form1" method="get" action="">

<p>

<label> Enter the keyword :</label>

<input name="searchq" type="text" class="search-style" id="searchq" size="40" autocomplete="off" value="<?php if(isset($query)){echo $query;};?>" />

</p>

<input name="submit" type="submit" value="Search" />

</form>

</div>

<div>

<?php

if($_GET['searchq'])

{

$query = $_GET['searchq'];

//$page = $_REQUEST['page'];

$page = '1';

if($page == 1)

$start = 1;

else

$start = (($display_record_per_page)*($page-1))+1;

//print $start;die;

if($query)

{

/****************** Web Search Start. ********************/

$prepare_query_for_search = urlencode($query);



$count = $display_record_per_page;

$search_result = $obj_yahoo->YahooWebSearch($prepare_query_for_search,$format,$start,'','',$count);

if($search_result)

{

$totalSearchPpt = count($search_result);



$searchHTML = '<table border="0" width="100%" cellpadding="0" cellspacing="0" class="stdTable">';

for($counter = 0; $counter < $totalSearchPpt-1; $counter++)

{

$c= $counter+1;

$searchHTML .= '<tr>';

$searchHTML .= '<td><a href="'.$search_result[$counter]['url'].'" target="_blank"><b>'.$search_result[$counter]['title'].'</b></a></td>';

$searchHTML .= '</tr>';

$abstract = $search_result[$counter]['abstract'];

$searchHTML .= '<tr>';

$searchHTML .= '<td>'.$abstract.'</td>';

$searchHTML .= '</tr>';

$searchHTML .= '<tr>';

$searchHTML .= '<td><a href="'.$search_result[$counter]['url'].'" target="_blank">'.$search_result[$counter]['url'].'</a></td>';

$searchHTML .= '</tr>';

$searchHTML .= '<tr><td>&nbsp;</td></tr>';



}

$searchHTML .= '</table>';

}

else

{

$searchHTML = '<table border="0" cellpadding="0" cellspacing="0" width="100%"><tr><td>No Result Found</td></tr></table>';

}

/****************** Web Search End. ********************/

}

}



echo $searchHTML;

?>

</div>

</body>

</html>

Just Enter your API key and enjoying searching. I’ve made three different functions for web search, image search and news search in yahoo boss class.
Using yahoo boss you can create your own search engine using your unique ideas. Please feel free to contact me for any help.
0 Comments
Disqus
Fb Comments
Comments :

0 comments:

Post a Comment