Solved

Using wildcards in MySQL fulltext searches

Posted on 2008-10-02
6
1,703 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
6 Comments
 
LVL 143

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 143

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
Database Solutions Engineer FAQs

In this series, we will discuss common questions received as a database Solutions Engineer at Percona. In this role, we speak with a wide array of MySQL and MongoDB users responsible for both extremely large and complex environments to smaller single-server environments.

 
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

Moving data to the cloud? Find out if you’re ready

Before moving to the cloud, it is important to carefully define your db needs, plan for the migration & understand prod. environment. This wp explains how to define what you need from a cloud provider, plan for the migration & what putting a cloud solution into practice entails.

Question has a verified solution.

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

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
This post looks at MongoDB and MySQL, and covers high-level MongoDB strengths, weaknesses, features, and uses from the perspective of an SQL user.
In this video, viewers will be given step by step instructions on adjusting mouse, pointer and cursor visibility in Microsoft Windows 10. The video seeks to educate those who are struggling with the new Windows 10 Graphical User Interface. Change Cu…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

617 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