PHP
  Home arrow PHP arrow Page 4 - Using Relevance Rankings for Full Text and Boolean Searches with MySQL
Dev Shed Forums  
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Smartphone Development  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Mobile Linux  
App Generation ROI  
IBM® developerWorks  
Forums Sitemap  
E-Commerce Hosting  
Linux Web Hosting  
Managed Hosting  
Small Business Hosting  
VPS Hosting  
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid  
Request Media Kit
Contact Us  
Site Map  
Privacy Policy  
Support  
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
Google.com  
PHP

Using Relevance Rankings for Full Text and Boolean Searches with MySQL
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 7
    2007-06-13


    Table of Contents:
  • Using Relevance Rankings for Full Text and Boolean Searches with MySQL
  • Developing a basic MySQL-driven search engine
  • Determining the 50 percent threshold
  • Building an additional example

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      error-file:tidyout.log Del.ici.ous error-file:tidyout.log Digg
      error-file:tidyout.log Blink error-file:tidyout.log Simpy
      error-file:tidyout.log Google error-file:tidyout.log Spurl
      error-file:tidyout.log Y! MyWeb error-file:tidyout.log Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article

     
     
    ADVERTISEMENT


    Using Relevance Rankings for Full Text and Boolean Searches with MySQL - Building an additional example
    ( Page 4 of 4 )

    In consonance with the concepts deployed in the previous section, the last example that I'm going to show you here will consist of demonstrating how MySQL is capable of returning different relevance rankings according to the search terms entered in a web form.

    In this case, I'm going to use two concatenated search words to return a couple of relevance values from the previous "USERS" database table, instead of only one. I'm going to use the pair of source files that you learned in the beginning of the article, so here are their respective signatures:

    (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();

    }

    ?>

    Now, after listing the two previous source files, please study the following results returned by MySQL after entering the search string "alejandro+susan" in the corresponding web form:

    // PHP file displays the following:
    /*
    Users returned are the following:
    Name: Alejandro Relevance: 1.0167628961849
    Name: John Relevance: 0
    Name: Susan Relevance: 1.0277009445163
    Name: Julie Relevance: 0
    */

    As you can see, in the previous case the search query has returned two different relevance rankings in accordance with the inputted search terms. Hopefully, this last example should give you a better idea of the way that MySQL handles relevance values.

    As usual with many of my articles on PHP development, feel free to modify all the code samples shown here, so you can start quickly implementing full-text searches in your own web applications.

    Final thoughts

    In this second article of the series, I provided you with a basic introduction to using relevance rankings when performing full-text searches with MySQL. In the final part I'm going to complete this interesting subject by teaching you how to use Boolean operators with your search queries.

    You've been warned, so don't miss it!



     
     
    >>> More PHP Articles          >>> More By Alejandro Gervasio
     

       

    PHP ARTICLES

    - Implementing Factory Methods in PHP 5
    - Merging a File Split for FTP Upload using PHP
    - Getting Data from Yahoo Site Explorer Inboun...
    - Method Chaining: Adding More Selecting Metho...
    - How to Split a File During an FTP Upload Usi...
    - Expanding a Custom CodeIgniter Library with ...
    - Using the Yahoo Site Explorer Inbound Links ...
    - Building a CodeIgniter Custom Library with M...
    - Building an E-mini Trading System Using PHP ...
    - Completing the MySQL Class with Method Chain...
    - Building Dynamic Queries with Chainable Meth...
    - PHP Encryption and Decryption Methods
    - Building a MySQL Abstraction Class with Meth...
    - Completing a Sample String Processor with Me...
    - Mastering WHILE Loops for PHP and MySQL





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 5 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek