Solved

Using wildcards in MySQL fulltext searches

Posted on 2008-10-02
6
1,674 Views
Last Modified: 2012-05-05
I'm using a MySQL database, and I have a table containing a text field. I was using a query to return all rows containing a string within the text field -

... WHERE desc like '%MSDN%'

But this is was too slow on a large table. I speeded this up by using a fulltext index on the 'desc' column in my table.
However, the equivalent fulltext search clause

... MATCH desc AGAINST ('MSDN')

returns rows matching

...w/MSDN Prem...

but not

...w/MSDNPrem...

I've tried modifying my fulltext search with wildcards i.e.

... MATCH desc AGAINST ('*MSDN*')

but this didn't work, I still didn't get the complete set of matches I expected. Does anyone know what the equivalent fulltext search pattern is, to return exactly the same resultset as my original query?
0
Comment
Question by:smartstream-stp
  • 3
  • 2
6 Comments
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 22622620
what about:
... MATCH desc AGAINST ('MSDN*')

Open in new window

0
 

Author Comment

by:smartstream-stp
ID: 22622740
Unfortunately that doesn't work. 'MSDN*' does not return rows containing '...w/MSDNPrem...'
0
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 22622769
first you cannot use *something in FT...

so, I am not sure, but maybe / is not a word breaker....
http://dev.mysql.com/doc/refman/5.0/en/fulltext-fine-tuning.html

#If you want to change the set of characters that are considered word characters, you can do so in two ways. Suppose that you want to treat the hyphen character ('-') as a word character. Use either of these methods:
 

    *    Modify the MySQL source: In myisam/ftdefs.h, see the true_word_char() and misc_word_char() macros. Add '-' to one of those macros and recompile MySQL.
 

    *    Modify a character set file: This requires no recompilation. The true_word_char() macro uses a character type table to distinguish letters and numbers from other characters. . You can edit the <ctype><map> contents in one of the character set XML files to specify that '-' is a letter. Then use the given character set for your FULLTEXT indexes.
 

After making the modification, you must rebuild the indexes for each table that contains any FULLTEXT indexes.

Open in new window

0
Zoho SalesIQ

Hassle-free live chat software re-imagined for business growth. 2 users, always free.

 
LVL 5

Accepted Solution

by:
jericotolentino earned 500 total points
ID: 22622902
Try if MySQL boolean search works for you:

SELECT *,MATCH(desc) AGAINST ('*MSDN*' IN BOOLEAN MODE) AS score FROM search WHERE MATCH(desc) AGAINST ('*MSDN*' IN BOOLEAN MODE);

It requires that you're using MyISAM and the indexes you're searching are of type varchar or text.

More info is available here:
http://dev.mysql.com/doc/refman/5.0/en/fulltext-search.html
0
 

Author Comment

by:smartstream-stp
ID: 22622945
If '/' isn't a word breaker, then how is 'w/MSDN Prem' matched, but not 'w/MSDNPrem' ?

I don't think fulltext indexing is going to be 100% reliable for me, as it relies on the text field being split into words. In my case I can't guarantee this. I don't think there is any direct equivalent for 'WHERE desc LIKE '%MSDN%' i.e. matching an aribitrary string within a text field.

0
 

Author Comment

by:smartstream-stp
ID: 22622997
jericotolentino's suggestion worked much better, but didn't match a field value of

VStudio Team Ed Sft Test AllLngLic/SAOLVNL1YRAcqY1wMSDNPrmAdPrdPrtnrOnly
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
How can I iterate through mysql tables to alter character set? 3 60
SQL Update Query 23 93
MySQL Server 5.5.36 Tuning 9 50
Problem with Simple PHP/mySQL Query 3 51
This guide whil teach how to setup live replication (database mirroring) on 2 servers for backup or other purposes. In our example situation we have this network schema (see atachment). We need to replicate EVERY executed SQL query on server 1 to…
Introduction Since I wrote the original article about Handling Date and Time in PHP and MySQL (http://www.experts-exchange.com/articles/201/Handling-Date-and-Time-in-PHP-and-MySQL.html) several years ago, it seemed like now was a good time to updat…
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…
Internet Business Fax to Email Made Easy - With  eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, f…

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now