perform Boolean full-text searches using the “IN BOOLEAN MODE” modifier

The only thing you change to use Boolean mode is the AGAINST() part of your query. You add IN BOOLEAN MODE to the very end of it, and place the arguments right before it.
SELECT * FROM articles WHERE MATCH(title, body) AGAINST('+PHP -MySQL' IN BOOLEAN MODE);

Operator Meaning
By default (when neither plus nor minus is specified) the word
is optional, but the rows that contain it will be rated higher. This mimics the
behavior of MATCH() ...
AGAINST()DR
+ A leading plus sign indicates that this word must be present
in every row returned.
A leading minus sign indicates that this word must not be
present in any row returned.
< > These two operators are used to change a word’s contribution
to the relevance value that is assigned to a row. The < operator decreases
the contribution and the > operator increases it. See the example below.
( ) . Parentheses are put round sub-expressions to give them
higher precedence in the search.
~ A leading tilde acts as a negation operator, causing the
word’s contribution to the row relevance to be negative. It’s useful for marking
noise words. A row that contains such a word will be rated lower than others,
but will not be excluded altogether, as it would be with the minus
operator.
* An asterisk is the truncation operator. Unlike the other
operators, it is appended to the word, or fragment, not prepended.
Double quotes at the beginning and end of a phrase, matches
only rows that contain the complete phrase, as it was typed.

A Basic Boolean Searching Application
<?php

/* call this script “advs.php” */

if(!$c) {

?>

<form action=”advs.php?c=1″ method=POST>

<b>Find Results with: </b><br>
Any of these words: <input type=”text” length=40 name=”any”> <br>

All of these words: <input type=”text” length=40 name=”all”> <br>

None of these words: <input type=”text” length=40 name=”none”> <br>

<input type=”submit” value=”Search”>
</form>

<?

} else if($c) {

MySQL_connect(“hostname”, “username”, “password”);


MySQL_select_db(“database”);

if((!$all) || ($all == “”)) { $all = “”; } else { $all = “+(“.$all.“)”; }


if((!
$any) || ($any == “”)) { $any = “”; }

if((!$none) || ($none == “”)) { $none = “”; } else { $none = “-(“.$none.“)”; }


$query =

SELECT *,

MATCH(title, story) AGAINST (‘$all $none $any’ IN BOOLEAN MODE) AS score


FROM compsite

WHERE MATCH(title, story) AGAINST (‘$all $none $any’ IN BOOLEAN MODE)”;

$artm1 = MySQL_query($query);


if(!
$artm1) {

echo MySQL_error().“<br>$query<br>”;

}

echo “<b>Article Matches</b><br>”;


if(
MySQL_num_rows($artm1) > 0) {

echo “<table>”;

echo “<tr><td>Score </td><td>Title </td><td>Body</td></tr>”;


while(
$artm2 = MySQL_fetch_array($artm1)) {

$val = round($artm2[‘score’], 3);


$val = $val*100;

echo “<tr><td>$val</td>”;

echo “<td>{$artm2[‘title’]}</td>”;


echo
“<td>{$artm2[‘body’]}</td></tr>”;

}

echo “</table>”;


}

else {

echo “No Results were found in this category.<br>”;

}

echo “<br>”;


}

Links:

  • Using MySQL Full-text Searching
  • Advertisements

    Leave a Reply

    Fill in your details below or click an icon to log in:

    WordPress.com Logo

    You are commenting using your WordPress.com account. Log Out /  Change )

    Google+ photo

    You are commenting using your Google+ account. Log Out /  Change )

    Twitter picture

    You are commenting using your Twitter account. Log Out /  Change )

    Facebook photo

    You are commenting using your Facebook account. Log Out /  Change )

    Connecting to %s

    %d bloggers like this: