Using Boolean Operators for Full Text and Boolean Searches with MySQL

Implementing full text searches with MySQL can improve the execution of queries against specific database tables. If you want to put this useful feature to work for you, start reading this article now! Welcome to the final tutorial of the series that began with "Performing Full Text and Boolean Searches with MySQL." Made up of three tutorials, this series walks you through the basics of creating full text indexes in MySQL tables, and shows you how to take advantage of Boolean searches to improve the performance of your SQL queries.

Introduction

As you’ll probably remember, in the previous article of the series I discussed in detail some crucial topics surrounding the implementation of full text searches using MySQL. Specifically, I covered the use of the MATCH and AGAINST statements, which when used in conjunction allow us to retrieve easily not only database results, but also different relevance rankings, depending on the terms included into a given search string.

Of course, I accompanied the discussion with numerous code samples, which hopefully helped you to grasp more easily how MySQL-based full text searches can be implemented in real-world situations. Certainly, analyzing the subject from a completely practical point of view implies that many web developers can have at their disposal a decent variety of hands-on examples to help them start rapidly incorporating these useful features into their MySQL-driven applications.

Nonetheless, I have to admit that full text searches and MySQL is a huge topic, which possibly deserves many more articles than the ones I wrote for the excellent Developer Shed network. But this doesn’t prevent me from making my humble contribution to the subject. So in this last installment of the series I’m going to dive a bit deeper into the area of using full text searches with MySQL by showing you how to use the popular Boolean operators to add (and discard) specific terms from a given search string.

Are you ready to learn how to incorporate Boolean search capabilities to your web applications? Let’s begin now!

{mospagebreak title=Reintroducing some earlier concepts}

An excellent starting point consists of using the same search engine that I built in the first article of the series, incorporating the sample "USERS" database table created specifically in that occasion.

So let me remind you quickly of the set of simple SQL statements that create the database table in question:

CREATE TABLE users
(
   id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY NOT NULL,
   firstname VARCHAR(64),
   lastname VARCHAR(64),
   email VARCHAR(64)
   comments TEXT
   FULLTEXT(firstname,lastname,comments)
);

As you’ll certainly recall, the previous database table was defined in such a way that it incorporates three fields as full-text indexes, called "firstname," "lastname" and "comment" respectively.

So far, so good, right? Having created the prior database table, it’s time to fill it in with some basic records, as indicated below:

("users" database table)

Id firstname lastname      email            comments

1 Alejandro Gervasio alejandro@domain.com MySQL is great for building a search engine
2 John ‘Williams     john@domain.com      PHP is a server side scripting language
3 Susan Norton       susan@domain.com     JavaScript is good to manipulate documents
4 Julie Wilson       julie@domain.com     MySQL is the best open source database server

And finally, here are the signatures that correspond to the two source files that comprise the MySQL-based search engine utilized in the preceding articles of the series:

(definition of form.htm file)

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-
8859-1" />
<title>Working with relevance results</title>
<style type="text/css">
body{
  
padding: 0;
  
margin: 0;
  
background: #fff;
}

h1{
  
font: bold 16px Arial, Helvetica, sans-serif;
  
color: #000;
  
text-align: center;
}

p{
  
font: bold 11px Tahoma, Arial, Helvetica, sans-serif;
  
color: #000;
}

#formcontainer{
  
width: 40%;
  
padding: 10px;
  
margin-left: auto;
  
margin-right: auto;
  
background: #6cf;
}
</style>
</head>
<body>
 
<h1>Working with relevance results</h1>
 
<div id="formcontainer">
   
<form action="search.php" method="get">
     
<p>Enter search term here : <input type="text"
name="searchterm" title="Enter search term here" /><input
type="submit" name="search" value="Search Now!" /></p>
   
</form>
 
</div>
</body>
</html>

(definition of search.php file)

<?php
// define ‘MySQL’ class
class MySQL{
  
private $conId;
  
private $host;
  
private $user;
  
private $password;
  
private $database;
  
private $result;
  
const OPTIONS=4;
  
public function __construct($options=array()){
    
if(count($options)!=self::OPTIONS){
      
throw new Exception(‘Invalid number of connection
parameters’);
    
}
    
foreach($options as $parameter=>$value){
      
if(!$value){
        
throw new Exception(‘Invalid parameter ‘.$parameter);
      
}
      
$this->{$parameter}=$value;
    
}
    
$this->connectDB();
   
}
  
// connect to MySQL
  
private function connectDB(){
    
if(!$this->conId=mysql_connect($this->host,$this-
>user,$this->password)){
      
throw new Exception(‘Error connecting to the server’);
    
}
    
if(!mysql_select_db($this->database,$this->conId)){
      
throw new Exception(‘Error selecting database’);
    
}
  
}
  
// run query
  
public function query($query){
    
if(!$this->result=mysql_query($query,$this->conId)){
      
throw new Exception(‘Error performing query ‘.$query);
    
}
    
return new Result($this,$this->result);
  
}
  
public function escapeString($value){
    
return mysql_escape_string($value);
  
}
}
// define ‘Result’ class
class Result {
  
private $mysql;
  
private $result;
  
public function __construct($mysql,$result){
    
$this->mysql=$mysql;
    
$this->result=$result;
  
}
  
// fetch row
  
public function fetchRow(){
    
return mysql_fetch_assoc($this->result);
  
}
  
// count rows
  
public function countRows(){
    
if(!$rows=mysql_num_rows($this->result)){
      
return false;
    
}
    
return $rows;
  
}
  
// count affected rows
  
public function countAffectedRows(){
    
if(!$rows=mysql_affected_rows($this->mysql->conId)){
      
throw new Exception(‘Error counting affected rows’);
    
}
    
return $rows;
  
}
  
// get ID form last-inserted row
  
public function getInsertID(){
    
if(!$id=mysql_insert_id($this->mysql->conId)){
      
throw new Exception(‘Error getting ID’);
    
}
    
return $id;
  
}
  
// seek row
  
public function seekRow($row=0){
    
if(!is_int($row)||$row<0){
      
throw new Exception(‘Invalid result set offset’);
    
}
    
if(!mysql_data_seek($this->result,$row)){
      
throw new Exception(‘Error seeking data’);
    
}
  
}
}
try{
  
// connect to MySQL
  
$db=new MySQL(array(‘host’=>’host’,’user’=>’user’,’password’=>’password’,
‘database’=>’database’));
  
$searchterm=$db->escapeString($_GET['searchterm']);
  
$result=$db->query("SELECT firstname, MATCH
(firstname,lastname,comments) AGAINST(‘$searchterm’) AS relevance
FROM users");
  
if(!$result->countRows()){
    
echo ‘No results were found.';
  
}
  
else{
    
echo ‘<h2>Users returned are the following:</h2>';
    
while($row=$result->fetchRow()){
      
echo ‘<p>Name: ‘.$row['firstname'].’ Relevance: ‘.$row
['relevance'].'</p>';
    
}
  
}
}
catch(Exception $e){
  
echo $e->getMessage();
  
exit();
}
?>

As illustrated above, the search engine is composed of only two source files. The first file is responsible for displaying a common web form for entering different search strings, while the second one is tasked with returning the corresponding result sets according to the search words typed into the referenced online form.

In this case, you can see that I set up a SELECT query by using the already familiar MATCH and AGAINST commands for returning a specific relevance ranking. Thus, based on this context, and providing that the pertinent web form has been filled in with the search term "Alejandro," the results returned by MySQL would be as follows:

/*

Users returned are the following:

Name: Alejandro Relevance: 1.0167628961849

Name: John Relevance: 0

Name: Susan Relevance: 0

Name: Julie Relevance 0

*/

Quite simple, right? Logically, at this point I’m assuming that you’re already familiar with working with relevance values, since the topic was treated deeply in the previous article, so in theory you shouldn’t have major problems understanding how the above example works.

Okay, now that you hopefully recalled all of the steps required to return relevance rankings from a specific MySQL database table, let’s move forward and see how to build queries that accept Boolean operators for performing more advanced searches.

To learn more on this interesting topic, please click on the link that appears below and keep reading.

{mospagebreak title=Using the plus operator}

As you know, it’s possible to refine (or filter) a specific search string by using Boolean operators, including the plus (+) and minus (-) signs. Of course, there are other characters that can be included as part of a given search value, but I’m only going to cover those two here.

Now let me show you the signatures of the two source files listed in the section that you just read, including a SQL query that implements Boolean searches.

These sample files look like this:

(definition of form.htm file)

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-
8859-1" />
<title>Testing boolean searches using the plus (+)
operator</title>
<style type="text/css">
body{
  
padding: 0;
  
margin: 0;
  
background: #fff;
}

h1{
  
font: bold 16px Arial, Helvetica, sans-serif;
  
color: #000;
  
text-align: center;
}

p{
  
font: bold 11px Tahoma, Arial, Helvetica, sans-serif;
  
color: #000;
}

#formcontainer{
  
width: 40%;
  
padding: 10px;
  
margin-left: auto;
  
margin-right: auto;
  
background: #6cf;
}
</style>
</head>
<body>
 
<h1>Testing Boolean searches using the plus (+) operator</h1>
 
<div id="formcontainer">
   
<form action="search.php" method="get">
      <p>Enter search term here : <input type="text" name="searchterm" title="Enter search term here" /><input
type="submit" name="search" value="Search Now!" /></p>
   
</form>
 
</div>
</body>
</html>

(definition of search.php file)

<?php
// define ‘MySQL’ class
  
class MySQL{
  
private $conId;
  
private $host;
  
private $user;
  
private $password;
  
private $database;
  
private $result;
  
const OPTIONS=4;
  
public function __construct($options=array()){
    
if(count($options)!=self::OPTIONS){
      
throw new Exception(‘Invalid number of connection
parameters’);
    
}
    
foreach($options as $parameter=>$value){
      
if(!$value){
        
throw new Exception(‘Invalid parameter ‘.$parameter);
      
}
      
$this->{$parameter}=$value;
    
}
    
$this->connectDB();
  
}
  
// connect to MySQL
  
private function connectDB(){
    
if(!$this->conId=mysql_connect($this->host,$this-
>user,$this->password)){
      
throw new Exception(‘Error connecting to the server’);
    
}
    
if(!mysql_select_db($this->database,$this->conId)){
      
throw new Exception(‘Error selecting database’);
    
}
  
}
  
// run query
  
public function query($query){
    
if(!$this->result=mysql_query($query,$this->conId)){
      
throw new Exception(‘Error performing query ‘.$query);
    
}
    
return new Result($this,$this->result);
  
}
  
public function escapeString($value){
    
return mysql_escape_string($value);
  
}
}
// define ‘Result’ class
class Result {
  
private $mysql;
   private $result;
  
public function __construct($mysql,$result){
    
$this->mysql=$mysql;|
    
$this->result=$result;
  
}
  
// fetch row
  
public function fetchRow(){
    
return mysql_fetch_assoc($this->result);
  
}
  
// count rows
  
public function countRows(){
    
if(!$rows=mysql_num_rows($this->result)){
      
return false;
    
}
    
return $rows;
  
}
  
// count affected rows
  
public function countAffectedRows(){
    
if(!$rows=mysql_affected_rows($this->mysql->conId)){
      
throw new Exception(‘Error counting affected rows’);
    
}
    
return $rows;
  
}
  
// get ID form last-inserted row
  
public function getInsertID(){
    
if(!$id=mysql_insert_id($this->mysql->conId)){
      
throw new Exception(‘Error getting ID’);
    
}
    
return $id;
  
}
  
// seek row
  
public function seekRow($row=0){
    
if(!is_int($row)||$row<0){
      
throw new Exception(‘Invalid result set offset’);
     
}
    
if(!mysql_data_seek($this->result,$row)){
      
throw new Exception(‘Error seeking data’);
    
}
  
}
}
try{
  
// connect to MySQL
  
$db=new MySQL(array(‘host’=>’host’,’user’=>’user’,’password’=>’password’,
‘database’=>’database’));
  
$searchterm=$db->escapeString($_GET['searchterm']);
  
$result=$db->query("SELECT firstname FROM users WHERE MATCH
(firstname,lastname,comments) AGAINST(‘$searchterm’ IN BOOLEAN
MODE)");
  
if(!$result->countRows()){
    
echo ‘No results were found.';
  
}
  
else{
    
echo ‘<h2>Users returned are the following:</h2>';
    
while($row=$result->fetchRow()){
      
echo ‘<p>Name: ‘.$row['firstname'].’ Relevance: ‘.$row
['relevance'].'</p>';
    
}
  
}
}
catch(Exception $e){
  
echo $e->getMessage();
  
exit();
}
?>

As you can see, the last PHP file performs a real Boolean search against the familiar "USERS" database table, by using the brand new "IN BOOLEAN MODE" statement. This process is clearly demonstrated by the following line of PHP code:

$result=$db->query("SELECT firstname FROM users WHERE MATCH
(firstname,lastname,comments) AGAINST(‘$searchterm’ IN BOOLEAN
MODE)");

Basically I utilized the same search query that you learned in the prior two articles of the series, except that in this case I added the Boolean command listed above. Do you see how easy is to implement Boolean searches with MySQL? I bet you do!

Now, let me show you how to use the plus (+) sign to concatenate two simple search terms to return a database result set that contains both of them.

Here’s the corresponding example assuming that the respective search string has been constructed by concatenating the terms "Alejandro+JavaScript" via the (+) operator:

// displays the following entering ‘Alejandro+JavaScript’ search
term
/*
Users returned are the following:

Name: Alejandro

Name: Susan

*/

Indeed, you’ll have to agree with me that using the plus (+) sign to perform Boolean searches with MySQL is actually a simple process, which can be achieved with minor hassles.

All right, at this moment you hopefully learned how to use the previous (+) operator to return a result set that contains all the concatenated search terms. However, I’d also like to teach you how to perform the reverse procedure, that is discarding certain words from a given search string via the minus (-) sign. Sounds pretty interesting, right?

To learn how to use this brand new Boolean operator, jump ahead and read the final section of this article. I’ll be there, waiting for you.

{mospagebreak title=Using the minus operator}

As I explained in the previous section, the last example that I’m going to show you in this article surrounding the implementation of Boolean searches with MySQL is based upon using the minus (-) operator. This operator is useful for discarding specific terms from a particular search string.

To demonstrate a simple usage of this handy operator, first I’m going to list the two source files that you learned in the previous section, which look like this:

(definition of form.htm file)

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-
8859-1" />
<title>Testing Boolean searches using the minus (-) operator</title>
<style type="text/css">
body{
  
padding: 0;
  
margin: 0;
  
background: #fff;
}

h1{
  
font: bold 16px Arial, Helvetica, sans-serif;
   color: #000;
  
text-align: center;
}

p{
  
font: bold 11px Tahoma, Arial, Helvetica, sans-serif;
  
color: #000;
}

#formcontainer{
  
width: 40%;
  
padding: 10px;
  
margin-left: auto;
  
margin-right: auto;
  
background: #6cf;
}
</style>
</head>
<body>
 
<h1>Testing boolean searches using the minus (-) operator</h1>
 
<div id="formcontainer">
   
<form action="search.php" method="get">
     
<p>Enter search term here : <input type="text"
name="searchterm" title="Enter search term here" /><input
type="submit" name="search" value="Search Now!" /></p>
   
</form>
  
</div>
</body>
</html>

(definition of search.php file)

<?php
// define ‘MySQL’ class
class MySQL{
  
private $conId;
  
private $host;
  
private $user;
  
private $password;
  
private $database;
  
private $result;
  
const OPTIONS=4;
  
public function __construct($options=array()){
    
if(count($options)!=self::OPTIONS){
      
throw new Exception(‘Invalid number of connection
parameters’);
    
}
    
foreach($options as $parameter=>$value){
      
if(!$value){
        
throw new Exception(‘Invalid parameter ‘.$parameter);
      
}
      
$this->{$parameter}=$value;
    
}
    
$this->connectDB();
  
}
  
// connect to MySQL
  
private function connectDB(){
    
if(!$this->conId=mysql_connect($this->host,$this-
>user,$this->password)){
      
throw new Exception(‘Error connecting to the server’);
    
}
    
if(!mysql_select_db($this->database,$this->conId)){
      
throw new Exception(‘Error selecting database’);
    
}
  
}
  
// run query
  
public function query($query){
    
if(!$this->result=mysql_query($query,$this->conId)){
      
throw new Exception(‘Error performing query ‘.$query);
    
}
    
return new Result($this,$this->result);
  
}
  
public function escapeString($value){
    
return mysql_escape_string($value);
  
}
}
// define ‘Result’ class
class Result {
  
private $mysql;
  
private $result;
  
public function __construct($mysql,$result){
    
$this->mysql=$mysql;
    
$this->result=$result;
  
}
  
// fetch row
  
public function fetchRow(){
    
return mysql_fetch_assoc($this->result);
  
}
  
// count rows
  
public function countRows(){
    
if(!$rows=mysql_num_rows($this->result)){
      
return false;
    
}
    
return $rows;
  
}
  
// count affected rows
   
public function countAffectedRows(){
    
if(!$rows=mysql_affected_rows($this->mysql->conId)){
      
throw new Exception(‘Error counting affected rows’);
    
}
    
return $rows;
  
}
  
// get ID form last-inserted row
  
public function getInsertID(){
    
if(!$id=mysql_insert_id($this->mysql->conId)){
      
throw new Exception(‘Error getting ID’);
    
}
    
return $id;
  
}
  
// seek row
  
public function seekRow($row=0){
    
if(!is_int($row)||$row<0){
      
throw new Exception(‘Invalid result set offset’);
    
}
    
if(!mysql_data_seek($this->result,$row)){
      
throw new Exception(‘Error seeking data’);
    
}
  
}
}
try{
   // connect to MySQL
  
$db=new MySQL(array(‘host’=>’host’,’user’=>’user’,’password’=>’password’,
‘database’=>’database’));
  
$searchterm=$db->escapeString($_GET['searchterm']);
  
$result=$db->query("SELECT firstname FROM users WHERE MATCH
(firstname,lastname,comments) AGAINST(‘$searchterm’ IN BOOLEAN
MODE)");
  
if(!$result->countRows()){
    
echo ‘No results were found.';
  
}
  
else{
    
echo ‘<h2>Users returned are the following:</h2>';
    
while($row=$result->fetchRow()){
      
echo ‘<p>Name: ‘.$row['firstname'].’ Relevance: ‘.$row
['relevance'].'</p>';
    
}
  
}
}
catch(Exception $e){
  
echo $e->getMessage();
  
exit();
}
?>

All right, having listed the respective signatures of the above source files, you should notice that the corresponding SELECT query remains completely the same, meaning that the query in question also uses the "IN BOOLEAN MODE" statement.

That being said, here are the results returned by MySQL when the search string "MySQL -database" is entered in the respective web form:

/*
Users returned are the following:

// displays the following entering ‘MySQL -database’ search term
/*
Users returned are the following:

Name: Alejandro

*/

In this case, the (-) operator has been used to return all the database rows containing the word "MySQL" but not including the term "database." As you can see by the group of basic examples shown here, by using the "IN BOOLEAN MODE" statement it’s possible to build quickly a MySQL-driven search engine that supports full text and Boolean searches.

Final thoughts

Sadly, we’ve come to the end of this series. Hopefully, after examining all of the code samples included in these tutorials, you’ll have a much more solid background in how to implement full text and Boolean searches with MySQL.

See you in the next PHP tutorial!

[gp-comments width="770" linklove="off" ]

chat