Reputation: 83
I've got a search query witch sorts the results based on how many times the term is used in an article. For example 1 time in the title and 1 time in the content. This one will end higher up in the list than a article with te term only in it's content area. But I want a little extra, is it possible to add multipliers, for example when the term is used in the title then it counts for 2 instead of one. Look at the code below:
<?PHP
//SET THE SEARCH TERM
$term = "Search Term";
$sql = "SELECT *, MATCH(subject, message,reference,textbody) AGAINST('". $term ."') as score FROM pages WHERE MATCH (subject, message,reference,textbody) AGAINST('". $term ."') ORDER BY score DESC";
$query = mysql_query($sql);
//BUILD A LIST OF THE RESULTS
while($result = mysql_fetch_assoc($query)) {
// your stuff for showing result
}
?>
Could you please help me out?
Upvotes: 2
Views: 1323
Reputation: 562921
MySQL's MATCH function does not have a feature for weighting the fields. You would have to create a separate fulltext index on the field you want to have higher weight, and then calculate the relevance against both fulltext indexes with separate MATCH() expressions.
SELECT *,
MATCH(subject) AGAINST('". $term ."') * 2 AS score_s /* 2x weight */
MATCH(subject, message, reference, textbody) AGAINST('". $term ."') AS score
FROM pages
WHERE
MATCH(subject, message, reference, textbody) AGAINST('". $term ."')
ORDER BY score_s + score DESC
MySQL's builtin fulltext search is really not very flexible. Other specialized tools are often a better choice when you have specific functional requirements:
Sphinx Search supports relevance ranking, and the default ranking when you use extended matching mode is by word frequency, which seems to be what you're asking for.
http://sphinxsearch.com/blog/2010/08/17/how-sphinx-relevance-ranking-works/
Apache Solr supports "boosting" the weight of words matching a given field. http://wiki.apache.org/solr/SolrRelevancyFAQ#How_can_I_make_.22superman.22_in_the_title_field_score_higher_than_in_the_subject_field
Those other tools are frequently much faster for searching large datasets, too. See my presentation Fulltext Search Throwdown for comparisons.
Upvotes: 1
Reputation: 37243
just add +1
to it like that
$sql = "SELECT *, MATCH(subject, message,reference,textbody) AGAINST('". $term ."')+1 as score
FROM pages ORDER BY score DESC";
Upvotes: 1