learning_brain
New Member
- Messages
- 206
- Reaction score
- 1
- Points
- 0
I have an image search engine (not on this server) which is now crawling nicely, but the search function leaves much to be desired!
Currently, I'm using the fulltext MATCH AGAINST system; which works nicely but has two major limitations:
1) My host limits me to a 4 character search (all 3 character strings are ignored)
2) It doesn't support the %searchstring% feature.
Firstly, I want a 3 character search and 2nd, I have urls which I want to use the %searchstring% functionality.
In addition, it has be be sorted by relevance!!!
OK..... so I have done a lot of work on this and have come up with something that works but I'm VERY concerned about efficiency. With only a thousand or so records, this would be OK, but when the index grows to millions, this is going to be a headache.
Wondering if there is a better way of doing it?
Currently, I'm using the fulltext MATCH AGAINST system; which works nicely but has two major limitations:
1) My host limits me to a 4 character search (all 3 character strings are ignored)
2) It doesn't support the %searchstring% feature.
Firstly, I want a 3 character search and 2nd, I have urls which I want to use the %searchstring% functionality.
In addition, it has be be sorted by relevance!!!
OK..... so I have done a lot of work on this and have come up with something that works but I'm VERY concerned about efficiency. With only a thousand or so records, this would be OK, but when the index grows to millions, this is going to be a headache.
Wondering if there is a better way of doing it?
PHP:
<?php
mysql_connect("blah de blah") or die(mysql_error());
mysql_select_db("DB") or die(mysql_error());
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Frameset//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-frameset.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Search</title>
</head>
<body>
<form id="form1" method="post" action="">
<label>
<input type="text" name="search" id="search" />
</label>
<label>
<input type="submit" name="button" id="button" value="Submit" />
</label>
</form>
<?php
$q=strtolower(strip_tags($_POST['search']));
echo $q."<br/>";
$q_array = explode(" ", $q);
$q_num = count($q_array);
print_r($q_array);
$query = "SELECT ID, IMG_URL, KEYWORDS FROM IMAGES"; //full table content query
$result = mysql_query($query); //action result
$num_results = mysql_num_rows($result); //count rows
for ($i = 0;$i < $num_results; $i++) { //loop through rows
$row = mysql_fetch_object($result); //get row content as object
//assign object values to variables
$id = $row->ID;
$img_url = strtolower(strip_tags($row->IMG_URL));
$img_url_FC = $row->IMG_URL_FC;
$keywords = strtolower(strip_tags($row->KEYWORDS));
$keywords_FC = $row->KEYWORDS_FC;
//------------------------------------------SCORING----------------------------------------
$relevancy = 0; //set relevance to zero
//--------------------SEARCH THROUGH 1ST COLUMN---------------------------
//score for exact string
$relevancy = $relevancy + (substr_count($img_url, $q))*10;
for ($a = 0; $a < $q_num; $a++)
{
//count characters. if less than 3, ignore
if(strlen($q_array[$a])>2){
//gives score of 1 for each occasion words appear
$relevancy = $relevancy + substr_count($img_url, $q_array[$a]);
}
}
//--------------------SEARCH THROUGH 2ND COLUMN---------------------------
//score for exact string
$relevancy = $relevancy + (substr_count($keywords, $q))*10;
for ($a = 0; $a < $q_num; $a++)
{
//count characters. if less than 3, ignore
if(strlen($q_array[$a])>2){
//gives score of 1 for each occasion words appear
$relevancy = $relevancy + substr_count($keywords, $q_array[$a]);
}
}
//------------------------------------------END SCORING----------------------------------------
//add id to relevancy
$relevancy = $relevancy.".".$id;
//assign results to array
if ($relevancy>0.999999999999999999999999999999){
$processed_array[$relevancy]["IMG_URL_FC"] = $img_url_FC;
$processed_array[$relevancy]["KEYWORDS_FC"] = $keywords_FC;
}
} //end for ($i = 0;$i < $num_results; $i++) { //loop through rows
echo "<br/>";
krsort($processed_array);
$count_processed_array = count($processed_array);
echo "<br/>".$count_processed_array." results<br/>";
foreach($processed_array as $relevance=>$val){
echo "<br/>";
echo "Relevance: ".floor($relevance)."<br/>";
echo $processed_array [$relevance] [IMG_URL_FC].": ";
echo $processed_array [$relevance] [KEYWORDS_FC]."<br/>";
}
?>
</body>
</html>