MYSQL全文搜索顺序相关

时间:2022-09-19 19:15:30

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?

我正在尝试通过相关性对我的全文搜索进行排序。这是我的代码,如果删除ORDER BY但不按相关性排序。我试过这个,它实际上是这样,它根本找不到任何结果......任何想法?

$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 "); 

thanks for the help

谢谢您的帮助

1 个解决方案

#1


6  

From MySQL Boolean Full-Text Searches documentation:

来自MySQL布尔全文搜索文档:

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.

它们不会按相关性降低的顺序自动对行进行排序。您可以从前面的查询结果中看到这一点:具有最高相关性的行是包含“MySQL”两次的行,但它列在最后,而不是第一个。

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:

这就解释了为什么没有ORDER BY就不按相关性排序。现在,为了能够按相关性排序,您需要定义它:

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

#1


6  

From MySQL Boolean Full-Text Searches documentation:

来自MySQL布尔全文搜索文档:

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.

它们不会按相关性降低的顺序自动对行进行排序。您可以从前面的查询结果中看到这一点:具有最高相关性的行是包含“MySQL”两次的行,但它列在最后,而不是第一个。

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:

这就解释了为什么没有ORDER BY就不按相关性排序。现在,为了能够按相关性排序,您需要定义它:

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