Free Newsletters:
DatabaseJournal  
DBANews
Database Journal
Search Database Journal:
 
HOME News MS SQL Oracle DB2 Access MySQL PostgreSQL PHP SQL Etc Scripts Links Discussion
internet.com

» HOME
» NEWS
» FEATURES
» SERIES
MS SQL
Oracle
MS Access
MySQL
DB2
» RESOURCES
Products
Scripts
Links
» DISCUSSION
» TECH JOBS

Marketplace Partners
Be a Marketplace Partner


Compare Project Management Software Now!



Click for Technology & Business providers


Read “The Oracle DBA of Tomorrow”

Webcast: Governing IT in a Green World


Webcast:Implementing a Green Data Center


Meeting the Challenge of SMB Storage




internet.commerce
Be a Commerce Partner
Online Universities
Desktop Computers
Hurricane Shutters
PDA Phones & Cases
Imprinted Gifts
KVM over IP
Memory
Shop Online
Home Improvement
Car Donations
Get Business Software
Server Racks
Boat Donations
Phone Cards




Blue Coat: We Grow One Packet at a Time

Rhapsody Bets DRM-Free Downloads Can Foil iTunes

Heavies Joining Against Patent Suits?

internet.com
IT
Developer
Internet News
Small Business
Personal Technology
International

Search internet.com
Advertise
Corporate Info
Newsletters
Tech Jobs
E-mail Offers


Intel Whitepaper: Improve Security and Control of Your PCs


Solaris 8 Migration Assistant
Rapidly move your Solaris 8 application environments to new systems running Solaris 10 with the Solaris 8 Migration Assistant. Reduce migration risk while taking advantage of increased performance, reliability and security of the latest SPARC hardware platforms and Solaris 10 OS. »

 
Sun Partner Survey
Please take a quick 3 minute survey from Sun. Click here to take the survey. Your feedback will help Sun improve on their solution center and be more useful for you and your peers. Thank you for your time and consideration. »

 
Sun Eco Innovation: Power Calculators
Power consumption has increasingly become a priority in customer's minds when purchasing new systems or storage. Sun's Power Calculators provide data on power consumption of Sun products allowing IT managers to better plan the power requirements in the datacenter to achieve better energy and cost savings. »

 
Optimize the Web Tier: Consolidate to Get More Performance in Less Space and Lower Power Consumption
Expansion in the Web tier is generally accomplished by adding more servers whenever extra capacity is needed. As the pool of servers grows larger, however, the complexity of the environment can grow exponentially. »
Related Articles
Using Fulltext Indexes in MySQL - Part 1

Senior Software Engineer
Thomson
US-MA-Boston

Justtechjobs.com Post A Job | Post A Resume
MySQL
February 19, 2003
Using Fulltext Indexes in MySQL - Part 2, Boolean searches
By Ian Gilfillan

By default only whole words are matched, unless the '*' operator is used.

The '<' and '>' symbols are less commonly used, but they allow a great degree of control for relevance. In the following examples, we return the relevance indicator to demonstrate the difference between the queries.

mysql> SELECT copy, MATCH(copy) 
AGAINST ('appears good alert' IN BOOLEAN MODE) 
AS m FROM fulltext_sample WHERE MATCH(copy) 
AGAINST ('appears good alert' IN BOOLEAN MODE);
+---------------------------+------+
| copy                      | m    |
+---------------------------+------+
| It appears good from here |    2 |
| An all-out alert          |    1 |
| A good alert              |    2 |
+---------------------------+------+

mysql> SELECT copy, MATCH(copy) 
AGAINST ('appears <good alert' IN BOOLEAN MODE) 
AS m FROM fulltext_sample WHERE MATCH(copy) 
AGAINST ('appears <good alert' IN BOOLEAN MODE);
+---------------------------+------------------+
| copy                      | m                |
+---------------------------+------------------+
| It appears good from here |  1.6666667461395 |
| An all-out alert          |                1 |
| A good alert              |  1.6666667461395 |
+---------------------------+------------------+

The '<' operator decreases the relevance of the word 'good', in this case by approximately 0.33.

mysql> SELECT copy, MATCH(copy) 
AGAINST ('appears good >alert' IN BOOLEAN MODE) 
AS m FROM fulltext_sample WHERE MATCH(copy) 
AGAINST ('appears good >alert' IN BOOLEAN MODE);
+---------------------------+------+
| copy                      | m    |
+---------------------------+------+
| It appears good from here |    2 |
| An all-out alert          |  1.5 |
| A good alert              |  2.5 |
+---------------------------+------+

The '>' operator increases the relevance of the word 'alert', in this case by 0.5.

The parentheses group words into a subexpression. In the following example, the '+' symbol applies to the group of words, so that at least one of 'appears' or 'past' must appear.

mysql> SELECT copy, MATCH(copy) 
AGAINST ('+(appears past)' IN BOOLEAN MODE) 
AS m FROM fulltext_sample WHERE MATCH(copy) 
AGAINST ('(appears past)' IN BOOLEAN MODE);
+---------------------------+------+
| copy                      | m    |
+---------------------------+------+
| It appears good from here |    1 |
| The here and the past     |    1 |
+---------------------------+------+

You can also apply the operators to words in the subexpression, as follows:

mysql> SELECT copy, MATCH(copy) 
AGAINST ('+(>appears <past)' IN BOOLEAN MODE) 
AS m FROM fulltext_sample WHERE MATCH(copy) 
AGAINST ('+(>appears <past)' IN BOOLEAN MODE);
+---------------------------+------------------+
| copy                      | m                |
+---------------------------+------------------+
| It appears good from here |              1.5 |
| The here and the past     | 0.66666668653488 |
+---------------------------+------------------+
The '~' operator contributes negatively to the relevance, but does not bar the word from appearing, as the '-' operator does. It also does not do the same as the '<' operator, which is to make a reduced, yet still positive, difference to the relevance. These subtle difference allows for powerfully tuned searches, but can cause confusion. Look at the difference between the following. First, the word 'here' is compulsory, and the word 'past' optional:
mysql> SELECT copy, MATCH(copy) 
AGAINST ('+here past' IN BOOLEAN MODE) 
AS m FROM fulltext_sample WHERE MATCH(copy) 
AGAINST ('+here past' IN BOOLEAN MODE);
+---------------------------+-----------------+
| copy                      | m               |
+---------------------------+-----------------+
| It appears good from here |               1 |
| The here and the past     | 1.3333333730698 |
| Why are we here           |               1 |
+---------------------------+-----------------+
Next, a reduced relevance for the word 'past'. The same results are returned, but the record with both words ('The here and the past') has less weighting, though still more than the other records.
mysql> SELECT copy, MATCH(copy) 
AGAINST ('+here <past' IN BOOLEAN MODE) 
AS m FROM fulltext_sample WHERE MATCH(copy) 
AGAINST ('+here <past' IN BOOLEAN MODE);
+---------------------------+-----------------+
| copy                      | m               |
+---------------------------+-----------------+
| It appears good from here |               1 |
| The here and the past     | 1.2222222089767 |
| Why are we here           |               1 |
+---------------------------+-----------------+
Take note of the relevance in the next example:
mysql> SELECT copy, MATCH(copy) 
AGAINST ('+here ~past' IN BOOLEAN MODE) 
AS m FROM fulltext_sample WHERE MATCH(copy) 
AGAINST ('+here ~past' IN BOOLEAN MODE);
+---------------------------+------------------+
| copy                      | m                |
+---------------------------+------------------+
| It appears good from here |                1 |
| The here and the past     | 0.83333331346512 |
| Why are we here           |                1 |
+---------------------------+------------------+
The important difference is that the relevance is negatively affected, and is now lower than the other two records. This means if you asked for only '~past', you'd get nothing back, as the relevance would be lower than 0 (and therefore not returned)
mysql> SELECT copy, MATCH(copy) 
AGAINST ('~past' IN BOOLEAN MODE) 
AS m FROM fulltext_sample WHERE MATCH(copy) 
AGAINST ('~past' IN BOOLEAN MODE);
Empty set (0.00 sec)
Finally, '-' simply removes the second record from the result set, as we've seen before.
mysql> SELECT copy, MATCH(copy) 
AGAINST ('+here -past' IN BOOLEAN MODE) 
AS m FROM fulltext_sample WHERE MATCH(copy) 
AGAINST ('+here -past' IN BOOLEAN MODE);
+---------------------------+------+
| copy                      | m    |
+---------------------------+------+
| It appears good from here |    1 |
| Why are we here           |    1 |
+---------------------------+------+
The final operator, the double quotes ("), is useful to group phrases together. So, if you were interested in returning 'good from here' but none of those words if they did not appear in the phrase, you'd use:
mysql> SELECT copy, MATCH(copy) 
AGAINST ('"good from here"' IN BOOLEAN MODE) 
AS m FROM fulltext_sample WHERE MATCH(copy) 
AGAINST ('"good from here"' IN BOOLEAN MODE);
+---------------------------+------+
| copy                      | m    |
+---------------------------+------+
| It appears good from here |    1 |
+---------------------------+------+

For many applications, boolean searches are one of MySQL 4's most useful features. With careful use, you can save yourself substantial of development time, and add many useful enhancements. Good luck!

» See All Articles by Columnist Ian Gilfillan

Go to page: Prev  1  2  

Tools:
Add databasejournal.com to your favorites
Add databasejournal.com to your browser search box
IE 7 | Firefox 2.0 | Firefox 1.5.x
Receive news via our XML/RSS feed

MySQL Archives

Whitepaper: Rightsizing Blades for the Midmarket. Sponsored by HP.
Try the SQL Toolbelt and get a copy of Grant Fritcheys’s book on the art of high-performance SQL code.
Whitepaper: The Cost Advantages of Blade Servers. Sponsored by HP.
Intel Whitepaper: Comparing Multi-Core Processors for Server Virtualization
Intel eBook: Managing the Evolving Data Center


Latest Forum Threads
MySQL Forum
Topic By Replies Updated
MySQL WHERE clause using LIKE devel95 0 July 2nd, 02:08 PM
JOIN with multiple WHERE cjjubb 0 June 20th, 11:12 AM
Insert Array 2 Dimensional Into Database warhead2020 0 June 17th, 09:18 PM
Transfer information from an access database to MySQl database sculptor44 3 May 23rd, 08:31 AM







JupiterOnlineMedia

internet.comearthweb.comDevx.commediabistro.comGraphics.com

Search:

Jupitermedia Corporation has two divisions: Jupiterimages and JupiterOnlineMedia

Jupitermedia Corporate Info


Legal Notices, Licensing, Reprints, & Permissions, Privacy Policy.

Advertise | Newsletters | Tech Jobs | Shopping | E-mail Offers

Solutions
Whitepapers and eBooks
IBM eBook: Planning a Service Oriented Architecture
IBM eBook: Choosing the Right Architecture--What It Means for You and Your Business
Microsoft Article: Will Hyper-V Make VMware This Decade's Netscape?
Avaya Article: Using Intelligent Presence to Create Smarter Business Applications
Intel Go Parallel Article: Getting Started with TBB on Windows
Microsoft Article: 7.0, Microsoft's Lucky Version?
Avaya Article: How to Feed Data into the Avaya Event Processor
IBM Article: Developing a Software Policy for Your Organization
Microsoft Article: Managing Virtual Machines with Microsoft System Center
Intel Go Parallel Article: Intel Threading Tools and OpenMP
HP eBook: Storage Networking , Part 1
Microsoft Article: Solving Data Center Complexity with Microsoft System Center Configuration Manager 2007
MORE WHITEPAPERS, EBOOKS, AND ARTICLES
Webcasts
HP Video: StorageWorks EVA4400 and Oracle
HP Webcast: Storage Is Changing Fast - Be Ready or Be Left Behind
Microsoft Silverlight Video: Creating Fading Controls with Expression Design and Expression Blend 2
MORE WEBCASTS, PODCASTS, AND VIDEOS
Downloads and eKits
Red Gate Download: SQL Toolbelt and free High-Performance SQL Code eBook
Iron Speed Designer Application Generator
MORE DOWNLOADS, EKITS, AND FREE TRIALS
Tutorials and Demos
Silverlight 2 App and Walkthrough: Leverage Silverlight 2 with SQL Server and XML
IBM Article: Enterprise Search--Do You Know What's Out There?
HP Demo: StorageWorks EVA4400
Microsoft Article: The Progress and Promise of Deep Zoom
Microsoft How-to Article: Get Going with Silverlight and Windows Live
MORE TUTORIALS, DEMOS AND STEP-BY-STEP GUIDES