Jump to content

Search Engine Relevancy Question


zachmorelikezack

Recommended Posts

Hey all, I just have a quick question about a search engine script.

[code]if(isset($_POST['search_term']))
{
$search = $_POST['search_term'];
} elseif (isset($_GET['search_term'])){
$search = $_GET['search_term'];
}
if(isset($_POST['search_term']))
{
$keywords = explode(" ", $search);

$query = "SELECT work_id,title,description FROM zdivozzo_news.all_works " .
"WHERE tags LIKE '%".$keywords['0']."%'";

for ($i=1; $i<count($keywords); $i++) {
$query .= " OR tags LIKE '%".$keywords[$i]."%'";
}
if($_POST['search_type'] == 'all_types'){

} else {
$search_type = $_POST['search_type'];
$query .= "AND work_type='$search_type'";

}
if($_POST['search_genre'] == 'all_genres'){

} else {
$search_genre = $_POST['search_genre'];
$query .= "AND type_2='$search_genre'";

}
$query .= "ORDER BY user_score ASC";
$result = mysql_query($query) or die(mysql_error());
$count_results2 = mysql_num_rows($result);

$total = $count_results + $count_results2;
$num_records = $total;[/code]

There is the code that I'm using to split up the keywords and find the results in the database.  This all works fine, but how can I make it so that the most relevant results (ie the ones that match with the most of the keywords that were typed in) pop up first?

Thanks,
Zack
Link to comment
https://forums.phpfreaks.com/topic/34075-search-engine-relevancy-question/
Share on other sites

I believe using the MySQL function "MATCH()" will automatically sort by relevance. You would need to FULLTEXT index the fields involved, though.

[code]SELECT * FROM `table` WHERE MATCH(`field1`,`field2`,`field3`) AGAINST ('search terms')[/code]

I'm still a little rough on mysql querys, though. So if you still have trouble, you might try posting on the mysql board. [url=http://www.phpfreaks.com/forums/index.php/board,3.0.html]MySQL Help[/url]

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.