Jump to content

How to search my website more effeciently


brooksh

Recommended Posts

Here is my code, but it misses a lot of searches just by missing a word, or it pulls too many invalid search results. Any ideas how to make it better? Here is what I have:

 


$sql = "select * from questions where (Question LIKE '%$quest_search%' OR Answer LIKE '%$quest_search%') AND (active='yes')";
$result = mysql_query ($sql);
$count = mysql_num_rows( $result );

if($count == "0"){
$sql = "SELECT * FROM questions WHERE MATCH(Question,Answer) AGAINST ('$quest_search' IN BOOLEAN MODE) AND (active='yes')";

}

 

Link to comment
Share on other sites

What if you explode the search terms into separate words, search on all the words, then rank results by how many of the searched words appear in each row. Something like this:

$search_terms=explode(' ', $quest_search);
foreach ($search_terms as $value){
   if ($like!='') $like.='OR ';
   $like.='\'%'.$value.'%\' ';
}
$sql = "select * from questions where (Question LIKE $like OR Answer LIKE $like) AND (active='yes')";
$result = mysql_query ($sql);
$count = mysql_num_rows( $result );

if($count == "0"){
$sql = "SELECT * FROM questions WHERE MATCH(Question,Answer) AGAINST ('$quest_search' IN BOOLEAN MODE) AND (active='yes')";

}

If the number of results is managable, read the rows into an array and count matches.

I also found this to sort approach by total matches in a row. http://stackoverflow.com/questions/5651605/mysql-order-by-number-of-matches

Edited by davidannis
Link to comment
Share on other sites

Before

 

What if you explode the search terms into separate words, search on all the words, then rank results by how many of the searched words appear in each row. Something like this:

$search_terms=explode(' ', $quest_search);

foreach ($search_terms as $value){

   if ($like!='') $like.='OR ';

   $like.='\'%'.$value.'%\' ';

}

$sql = "select * from questions where (Question LIKE $like OR Answer LIKE $like) AND (active='yes')";
$result = mysql_query ($sql);
$count = mysql_num_rows( $result );

if($count == "0"){
$sql = "SELECT * FROM questions WHERE MATCH(Question,Answer) AGAINST ('$quest_search' IN BOOLEAN MODE) AND (active='yes')";

}

I agree on exploding but then if the user inputs something like this, "What do you do if you step on a mine?" then you should create a list of words to take away from the search parameters.  Put that list in a seperate php.  Get rid of words in the string such as the words "the, if, you, do, a, etc".  So now the search becomes "step on mine".  I think the search results would be more effecitve.  Then explode step on mine and put the new string in the variable %$quest_search%

Edited by RonnieCosta50
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.