To illustrate clearly how to retrieve different relevance rankings from MySQL when performing a full-text search against a specified database table, I'm going to use the same search engine that was built in the first article of the series. As you probably recall, it was composed of two simple source files. The first file was responsible for displaying the pertinent web form on the browser for entering obviously diverse search strings. The second one was tasked with executing real full-text queries against a sample "USERS" database table. Naturally, in this case I'm going to modify slightly the SELECT statement that returns the corresponding database results to handle the aforementioned relevance rankings. However, as you'll see for yourself in the next few lines, the rest of the search application will remain nearly the same. Having explained how this practical example will be developed, I'm going to create the mentioned "USERS" database table by specifying the corresponding full-text indexes for it. This simple process is demonstrated by the SQL statement below: CREATE TABLE users As you can see, the definition for the prior database table specifies that three fields of it, that is "firstname,""lastname" and "comments" respectively, will be created as full-text indexes via the FULLTEXT command that you learned in the previous tutorial of the series. So far, so good, right? Now, the next step consists of populating the above table with some trivial records, like the ones shown below: ("users" database table) Id firstname lastname email comments 1 Alejandro Gervasio alejandro@domain.com MySQL is great for building a search engine Having already inserted some primitive data into the previous database table, it's time to show the signatures of the two files that integrate this MySQL-driven search engine. These files look like this: (definition of form.htm file) <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" h1{ p{ #formcontainer{ (definition of search.php file) <?php // define 'Result' class try{ catch(Exception $e){ Despite the rather lengthy signature that corresponds to the last PHP file, you should pay attention particularly to the way that the pertinent search query has been constructed: $result=$db->query("SELECT firstname, MATCH In this case, I used the already familiar MATCH and AGAINST commands (covered in the preceding article of the series) to return from the sample "USERS" table a relevance ranking, depending on the search terms entered in the respective search form. However, the functionality of this ranking will be better understood if I show you some results outputted by the previous PHP file, according to the search term entered in the mentioned web form. That being said, here are the corresponding database results: // displays the following entering 'Alejandro' search term Name: Alejandro Relevance: 1.0167628961849 Name: John Relevance: 0 // displays the following entering 'Susan' search term Name: John Relevance: 0 // displays the following entering 'John' search term Name: Alejandro Relevance: 0 Name: John Relevance: 1.0277009445163 Name: Susan Relevance: 0 Name: Julie Relevance: 0 // displays the following entering 'Julie' search term Name: Alejandro Relevance: 0 Name: John Relevance: 0 Name: Susan Relevance: 0 Name: Julie Relevance: 1.0167628961849 As you can see, the above list of examples shows in a friendly fashion how to retrieve some relevance rankings in accordance with diverse search terms entered in the search form. Here, it's clear to see that this ranking value is a positive decimal value, and obviously varies in consonance with the inputted search string. Quite simple, right? Okay, at this point I believe that the previous results should give you a better idea of how to return relevance values using full-text searches. So what is the next step that must be taken on this educational journey? Well, since I assume that you're interested in learning a bit more about how MySQL handles relevance rankings, in the following section I'm going to show you a concrete example to illustrate how to work the so-called "50%" threshold. Does this sound complex to you? Fear not, since it's much simpler than you think! Just keep reading to learn more on this topic. |
|
|
|
|
|
|
|