SELECT headline, story, MATCH(headline,story) AGAINST ('Hurricane damage recent') as score
FROM news
WHERE MATCH (headline,story) AGAINST ('Hurricane damage recent')
ORDER BY score DESC;
SELECT headline, story
FROM news
WHERE MATCH (headline,story) AGAINST ('+hurricane -"New Orleans"' IN BOOLEAN MODE);
oTable = $('#example').dataTable( {
"bProcessing": true,
"bServerSide": true,
"sAjaxSource": "back-end-server-side-code.php",
"aoColumns": [
{ "sName": "id", "sTitle" : 'ID' },
// ... more columns here
{ "sName": "0 as relevance", sTitle: "relevance" }
});
// set up filter for mediaquery (main search)
$("#search-textbox").keyup( function () {
var col = set_relevance_column( this.value ); // set relevance column is a convenience function to find the col number of "relevance" and update sName for that column
if (col > -1)
oTable.fnSort([ [col,'desc'], [1,'asc'] ]); // you could add a function here to delay/collate calls to avoid excessive AJAX calls. I did, but took it out for this example
} );
function set_relevance_column(inputstring) {
if (inputstring.length < 3) relevance = "0 as relevance";
// note: "|" will be converted to comma in back-end-server-side-code.php
// this is because I explode the db fields input parameter passed to my PHP code by commas but don't want these split
// and of course my column names are not "field2", "field3", etc. replace these as needed
else relevance = "MATCH(id| field2| field3| field4| field5| field6| field7) AGAINST('" + inputstring+"') as relevance";
// get the datatable column and modify the sTitle oSettings = oTable.fnSettings();
var col = -1;
for (i=0; i<oSettings.aoColumns.length; i++) {
if (oSettings.aoColumns[i].sTitle == "relevance") {
col = i;
break;
}
}
if (col >= 0) {
oSettings.aoColumns[col].sName = relevance;
}
if (inputstring.length < 3) return 0; // for lack of better idea, sort by index in col 0 if we have insufficient keywords. you might return -1 instead to avoid any AJAX call or sorting
return col;
}
//check if using MATCH query
for ($i = 0; $i < count($aColumns); $i++) {
if (strpos($aColumns[$i], "MATCH(id") !== false) {
$aColumns[$i] = str_replace('|', ',', $aColumns[$i]);
$fullText = true;
$fullText_order = " relevance desc "; // default to descending since higher scores are better
$fullText_where = str_replace(" as relevance", "", $aColumns[$i]); // for our WHERE clause, strip off the alias
$aColumns[$i] = "round(".$fullText_where.", 3) as relevance"; // since I'm displaying the score to user, round it to 3 decimal places
}
}
/*
* Ordering
*/
$sOrder = ""; // default value
if ( isset( $_GET['iSortCol_0'] ) )
{
$sOrder = "ORDER BY ";
for ( $i=0 ; $i<intval( $_GET['iSortingCols'] ) ; $i++ )
{
if ( $_GET[ 'bSortable_'.intval($_GET['iSortCol_'.$i]) ] == "true" )
{
if(strpos($aColumns[ intval( $_GET['iSortCol_'.$i] ) ], "as relevance") > -1) {
$sOrder .= " relevance desc, ";
} else {
$sOrder .= $aColumns[ intval( $_GET['iSortCol_'.$i] ) ]."
".mysql_real_escape_string( $_GET['sSortDir_'.$i] ) .", ";
}
}
}
$sOrder = substr_replace( $sOrder, "", -2 );
if ( $sOrder == "ORDER BY" )
{
$sOrder = "";
}
}
$sWhere = "";
if ( isset($_GET['sSearch']) && $_GET['sSearch'] != "" )
{
$sWhere = "WHERE (";
for ( $i=0 ; $i<count($aColumns) ; $i++ )
{
if (strpos($aColumns[$i], "as relevance") === false)
$sWhere .= $aColumns[$i]." LIKE '%".mysql_real_escape_string( $_GET['sSearch'] )."%' OR ";
}
$sWhere = substr_replace( $sWhere, "", -3 );
$sWhere .= ')';
}
if ($fullText) { // $fullText and $fullText_where were defined above
if($sWhere == "")
$sWhere = "WHERE " . $fullText_where;
else
$sWhere = str_replace("WHERE (", "WHERE (" . $fullText_where . ") AND (", $sWhere);
}
while ( $aRow = mysql_fetch_array( $rResult ) )
{
$row = array();
for ( $i=0 ; $i<count($aColumns) ; $i++ )
{
if ( $aColumns[$i] != ' ' )
{
/* General output */
$colname = $aColumns[$i];
if (strpos($colname, "as relevance") !== false) $colname = "relevance";
$row[] = $aRow[ $colname ];
}
}
$output['aaData'][] = $row;
}
It looks like you're new here. If you want to get involved, click one of these buttons!
Get useful and friendly help straight from the source.