MYSQL fulltext search order by relevance

拈花ヽ惹草 提交于 2019-12-21 11:01:36

问题


I am trying to get my Full text search to order by relevance. here is my code it works if remove the ORDER BY but doesn't sort by relevance. I have tried this and it actually makes it so it doesnt find any results at all... Any ideas?

$query_for_result=mysql_query("
     SELECT * FROM Assets WHERE MATCH 
    (`Badge`,`First Name`,`Last Name`,`Service Tag`,`Asset Tag`)
     AGAINST ('".$query."*' IN BOOLEAN MODE) and
   `deleted` = '0'  ORDER BY relevance DESC");

edit*

<input type="text" name="query" />
<input type="hidden" value="Search" name="submit" />
<input type="submit" name="submit" value="Search" />

</form>
<h4>Search by: Badge, First or Last Name, Service Tag, Asset Tag, Printer Queue or Printer IP.</h4>
<br>
</center>

<?php


if(isset($_GET['submit'])){


$db_tb_name=Assets;
$db_tb_atr_name=`First Name`;

$query=mysql_real_escape_string($_GET['query']);

$query_for_result=mysql_query("
 SELECT * FROM Assets WHERE MATCH 
(`Badge`,`First Name`,`Last Name`,`Service Tag`,`Asset Tag`)
 AGAINST ('".$query."*' IN BOOLEAN MODE) and
`deleted` = '0'  ORDER BY relevance DESC");

then later on in the code

while($data_fetch=mysql_fetch_array($query_for_result))
{

print '<h3>';   
print "<a href=\"modify.php?id=" . $data_fetch['id'] . "\">" . $data_fetch['First Name']  . ' ' . $data_fetch['Last Name'] . "</a>";
print '<br/><b>Badge:</b> '. $data_fetch['Badge'];
print '<br/> <b>Service Tag:</b> '. $data_fetch['Service Tag'];
print ' <b>Asset Tag:</b> '. $data_fetch['Asset Tag'];
print '<br/> <b>Status:</b> '. $data_fetch['Status'];
print '<br/><b>Employee Status: </b>';
    if( $data_fetch['Employee Status'] == 'Active' ){
print '<font color="#32CD32">' . $data_fetch['Employee Status'] . '</font>';
    }
elseif( $data_fetch['Employee Status'] == 'Terminated' ){
print '<font color="red">' . $data_fetch['Employee Status'] . '</font>';}
print '<br/> <b>Last Modified:</b> '. $data_fetch['Last Modified'];
print "<span> </span>";
print '</h3>';
print '<br/><p>' ; 

}

UPDATE This worked for me I finally got it working by using

$query_for_result=mysql_query("SELECT  *, MATCH 
(`Badge`,`First Name`,`Last     Name`,`Service Tag`,`Asset Tag`) 
AGAINST ('".$query."'IN BOOLEAN MODE) AS Relevance FROM Assets WHERE 1 AND MATCH 
(`Badge`,`First Name`,`Last Name`,`Service Tag`,`Asset Tag`)
AGAINST ('".$query."' IN BOOLEAN MODE) and
 `deleted` = '0' ORDER BY Relevance DESC "); 

回答1:


From MySQL Boolean Full-Text Searches documentation:

They do not automatically sort rows in order of decreasing relevance. You can see this from the preceding query result: The row with the highest relevance is the one that contains “MySQL” twice, but it is listed last, not first.

That explains why it is not sorted by relevance without the ORDER BY. Now to be able to order by relevance, you need to define it:

SELECT *, MATCH (`Badge`,`First Name`,`Last Name`,`Service Tag`,`Asset Tag`) as relevance
WHERE MATCH AGAINST ('".$query."*' IN BOOLEAN MODE) and `deleted` = '0'
ORDER BY relevance DESC


来源:https://stackoverflow.com/questions/13494460/mysql-fulltext-search-order-by-relevance

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!