Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MYSQL fulltext search order by relevance

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 "); 
like image 842
user1548769 Avatar asked Nov 21 '12 13:11

user1548769


People also ask

What is advantage of FULLTEXT over like for performing text search in MySQL?

Using the LIKE operator gives you 100% precision with no concessions for recall. A full text search facility gives you a lot of flexibility to tune down the precision for better recall. Most full text search implementations use an "inverted index".

What is full text search in MySQL?

A full-text index in MySQL is an index of type FULLTEXT . Full-text indexes can be used only with InnoDB or MyISAM tables, and can be created only for CHAR , VARCHAR , or TEXT columns.

How do I create a full text search in MySQL?

To use full-text search in MySQL you need to use full-text indexes and the MATCH () function. The full-text index is FULLTEXT. Mysql supports full-text indexes on MyISAM tables. InnoDB support has been added since version 5.6.

What is FULLTEXT index in MySQL?

Full-text indexes are created on text-based columns ( CHAR , VARCHAR , or TEXT columns) to speed up queries and DML operations on data contained within those columns. A full-text index is defined as part of a CREATE TABLE statement or added to an existing table using ALTER TABLE or CREATE INDEX .


1 Answers

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
like image 121
Tchoupi Avatar answered Sep 19 '22 10:09

Tchoupi