Database Articles
  Home arrow Database Articles arrow Full Text Searches and Strings
Codewalker Forums 
  Tutorials  
Database Articles  
Miscellaneous  
Navigation Usability  
PEAR Articles  
Programming Basics  
Server Administration  
XML Tutorials  
  Reviews  
Database Book Reviews  
Linux Book Reviews  
Miscellaneous Reviews  
PHP Book Reviews  
PHP Software Reviews  
Server Admin Reviews  
SQL Tool Reviews  
  Code Gallery  
Content Management Code  
Contest Code  
Counters Code  
Database Code  
Date Time Code  
Discussion Board Code  
Email Code  
File Manipulation Code  
GUI Code  
Link Farm Code  
Miscellaneous Code  
Search Code  
Site Navigation Code  
User Management Code  
Forums Sitemap 
Dedicated Servers  
Download TestComplete 
IBM® developerWorks
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? 
DATABASE ARTICLES

Full Text Searches and Strings
By: O'Reilly Media
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 1
    2007-09-27

    Table of Contents:
  • Full Text Searches and Strings
  • 5.17 Requiring or Excluding FULLTEXT Search Words
  • Another Solution
  • 5.18 Performing Phrase Searches with a FULLTEXT Index

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb 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

    Stay one step ahead of the competition. Evaluate and give feedback on some of the hottest web development tools on the market today. Make your opinion heard! Click Here

    Full Text Searches and Strings
    (Page 1 of 4 )

    This conclusion to a series of articles that explain how to work with strings and string data rounds out your education on full text searches and strings. It is excerpted from chapter five of the MySQL Cookbook, Second Edition, written by Paul DuBois (O'Reilly; ISBN: 059652708X). Copyright © 2006 O'Reilly Media, Inc. All rights reserved. Used with permission from the publisher. Available from booksellers or direct from O'Reilly Media.

    5.16 Using a FULLTEXT Search with Short Words

    Problem

    FULLTEXT searches for short words return no rows.

    Solution

    Change the indexing engine’s minimum word length parameter.

    Discussion

    In a text like the KJV, certain words have special significance, such as “God” and “sin.” However, if you perform FULLTEXT searches on the kjv table for those words, you’ll observe a curious phenomenon—both words appear to be missing from the text entirely:

      mysql> SELECT COUNT(*) FROM kjv WHERE MATCH(vtext) AGAINST('God');
      +----------+
      | COUNT(*) |
      +----------+
      |        0 |
      +----------+
      mysql> SELECT COUNT(*) FROM kjv WHERE MATCH(vtext) AGAINST('sin');
      +----------+
      | COUNT(*) |
      +----------+
      |        0 |
      +----------+

    One property of the indexing engine is that it ignores words that are “too common” (that is, words that occur in more than half the rows). This eliminates words such as “the” or “and” from the index, but that’s not what is going on here. You can verify that by counting the total number of rows, and by using SQL pattern matches to count the number of rows containing each word:*

      mysql> SELECT COUNT(*) AS 'total verses',
         -> COUNT(IF(vtext LIKE '%God%',1,NULL)) AS 'verses containing "God"',
         -> COUNT(IF(vtext LIKE '%sin%',1,NULL)) AS 'verses containing "sin"'

         
    -> FROM kjv;

     

     

    total verses  verses containing "God" verses containing "sin"

     

     31102  4118  1292

     


    Neither word is present in more than half the verses, so sheer frequency of occurrence doesn’t account for the failure of a FULLTEXT search to find them. What’s really happening is that by default, the indexing engine doesn’t include words less than four characters long. The minimum word length is a configurable parameter, which you can change by setting the ft_min_word_len server variable. For example, to tell the indexing engine to include words as short as three characters, add a line to the [mysqld] group of the /etc/my.cnf file (or whatever option file you put server settings in):

      [mysqld]
      ft_min_word_len=3

    After making this change, restart the server. Next, rebuild the FULLTEXT index to take advantage of the new setting:

      mysql> REPAIR TABLE kjv QUICK;

    Finally, try the new index to verify that it includes shorter words:

      mysql> SELECT COUNT(*) FROM kjv WHERE MATCH(vtext) AGAINST('God');
      +----------+
      | COUNT(*) |
      +----------+
      |     3878 |
      +----------+
      mysql> SELECT COUNT(*) FROM kjv WHERE MATCH(vtext) AGAINST('sin');
      +----------+
      | COUNT(*) |
      +----------+
      |      389 |
      +----------+

    That’s better!

    But why do the MATCH() queries find 3,878 and 389 rows, whereas the earlier LIKE queries find 4,118 and 1,292 rows? That’s because the LIKE patterns match substrings and the FULLTEXT search performed by MATCH() matches whole words.

    More Database Articles Articles
    More By O'Reilly Media


       · This article is an excerpt from the book "MySQL Cookbook, Second Edition," published...
     
     

    Buy this book now. This article is excerpted from chapter five of the MySQL Cookbook, Second Edition, written by Paul DuBois (O'Reilly; ISBN: 059652708X). Check it out today at your favorite bookstore. Buy this book now.

    DATABASE ARTICLES ARTICLES

    - More on Query Optimization for Oracle Databa...
    - Query Optimization in Oracle
    - Clusters and Other Data Structures for Oracle
    - Using Indexes with an Oracle Database
    - The Basics of Data Structures in Oracle
    - Oracle Data Structures
    - Best Practices for PL/SQL Variables
    - What`s Code Without Variables?
    - Clauses, Sorting, and SQL Queries
    - The From Clause and SQL Queries
    - Query Primer
    - Full Text Searches and Strings
    - Searching with Strings
    - Pattern Matching with Strings
    - Working with Cases of Strings





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 5 hosted by Hostway