Solved

Using wildcards in MySQL fulltext searches

Posted on 2008-10-02
6
1,664 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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
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

Get up to 2TB FREE CLOUD per backup license!

An exclusive Black Friday offer just for Expert Exchange audience! Buy any of our top-rated backup solutions & get up to 2TB free cloud per system! Perform local & cloud backup in the same step, and restore instantly—anytime, anywhere. Grab this deal now before it disappears!

Join & Write a Comment

Creating and Managing Databases with phpMyAdmin in cPanel.
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

747 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