MySQL FULLTEXT Indexing and Searching
MySQL has supported FULLTEXT indexes since version 3.23.23. VARCHAR and TEXT Columns that have been indexed with FULLTEXT can be used with special SQL statements that perform the full text search in MySQL.
To get started you need to define the FULLTEXT index on some columns. Like other indexes, FULLTEXT indexes can contain multiple columns. Here's how you might add a FULLTEXT index to some table columns:
ALTER TABLE news ADD FULLTEXT(headline, story);
Once you have a FULLTEXT index, you can search it using MATCH and AGAINST statements. For example:
SELECT headline, story FROM news
WHERE MATCH (headline,story) AGAINST ('Hurricane');
The result of this query is automatically sorted by relevancy.
MATCH
The MATCH function is used to specify the column names that identify your FULLTEXT collection. The column list inside the MATCH function must exactly match that of the FULLTEXT index definition, unless your search in boolean mode (see below).
AGAINST
The AGAINST function is where your full text search query goes. Besides the default natural language search mode, you can perform boolean mode searches, and use query expansion.
Boolean Mode Searches
SELECT headline, story FROM news
WHERE MATCH (headline,story)
AGAINST ('+Hurricane -Katrina' IN BOOLEAN MODE);
The above statement would match news stories about hurricanes but not those that mention hurricane katrina.
See the MySQL documentation on Boolean Mode searches for more info.
Query Expansion
The Blind Query Expansion (or automatic relevance feedback) feature can be used to expand the results of the search. This often includes much more noise, and makes for a very fuzzy search.
In most cases you would use this operation if the users query returned just a few results, you try it again WITH QUERY EXPANSION and it will add words that are commonly found with the words in the query.
SELECT headline, story FROM news
WHERE MATCH (headline,story)
AGAINST ('Katrina' WITH QUERY EXPANSION);
The above query might return all news stories about hurricanes, not just ones containing Katrina.
A couple points about Full-Text searching in MySQL:
- Searches are not case sensitive
- Short words are ignored, the default minimum length is 4 characters. You can change the min and max word length with the variables
ft_min_word_lenandft_max_word_len - Words called stopwords are ignored, you can specify your own stopwords, but default words include the, have, some - see default stopwords list.
- You can disable stopwords by setting the variable
ft_stopword_fileto an empty string. - Full Text searching is only supported by the
MyISAMstorage engine. - If a word is present in more than 50% of the rows it will have a weight of zero. This has advantages on large datasets, but can make testing difficult on small ones.
Do you have any other good tips for fulltext searching and indexing in MySQL?
Tweet
add to del.icio.us
| Tags: mysql, fulltext, indexing, search, databases, sql
Related Entries
- Sphinx - Open Source SQL Full Text Search Engine - November 1, 2006
- Updated SQL Reserved Words Checker - March 28, 2006
- SQL to Select a random row from a database table - September 14, 2005
- Insert Delayed with MySQL - August 2, 2005
- Multiple Inserts with MySQL - June 10, 2005
Trackbacks
Comments
Thanks for this article. I was going crazy trying to figure out why I could do a search for WalMart but not CVS.
Thanks for the valuable information about Full text searching ability of MySQL. By the way, do you know how to implement search queries like "All words", "Any Words" & Exact Match like there are in PHPKB Knowledge Base Software at http://www.knowledgebase-script.com
the query does not bring back any results even if the value in the against exists and the status is 1 what could be the cause of it. thanx
the query does not bring back any results even if the value in the against exists and the status is 1 what could be the cause of it. thanx
i just want to know whether it is useful to alter the 50% threshold scheme that mysql uses by default ,is it beneficial in any way regarding serach(either by increasing or decreasing its value)or is it good to use default value of 50% only.
which is better (altering the file to change 50% value or using default 50 %)
thanx and regards
Are there any pointers to the performance of this Full Text search in mysql?
I have a table that stores short text messages (upto 4000 chars), and this table grows fast, about 15 mil/day.
I wonder if I should try the full text search in mysql or other ?
Thanks.
Bruce
http://deckerix.tuxfamily.org/leerArticulo.php?post=92
Likewise the arbitrary list of stopwords and ignoring any word in 50% => of the results has no basis in usability research. It's just what the IR guys thought would be good back when disk space was really expensive.
Frankly, canning this entirely and sending people to Solr (Lucene) or Sphinx Search would be a much better idea. Even programmers need decent search.
Index is limited to a size depending on which sql engine you use
Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in C:\xampp\htdocs\test\search2.php on line 35
my code is
<html> <head> <body>
<form name="form" action="search2.php" method="get"> <input type="text" name="q" /> <input type="submit" name="Submit" value="Search" /> </form>
<?php
$var = @$_GET['q'] ; $trimmed = trim($var); $quary = "'%".$trimmed."%'"; $select = "SELECT *, MATCH(news) AGAINST('$trimmed') AS score FROM articles WHERE MATCH(news) AGAINST('$trimmed') ORDER BY score DESC"; $con =mysql_connect("localhost","search","123456"); if(!con) { die ("Database not connected"); } else { mysql_select_db ("omassery", $con);
echo $result = mysql_query ($select, $con); } while ($row = mysql_fetch_array ($result)) { echo $row['news']."<br>"; } ?>
</body> </html>
Ex: if we are search for blue t-shirt, the query displays all the blue t-shirt’s and t-shirt’s keyword
Post a Comment
Recent Entries
- Writing Secure CFML cfObjective 2013 Slides
- Upgrading to Java 7 on Linux
- J2EE Sessions in CF10 Uses Secure Cookies
- Learn about ColdFusion Security at cfObjective 2013
- Session Loss and Session Fixation in ColdFusion
- FuseGuard 2.3 Released
- CKEditor Spell Checker Plugin
- Adobe Says Go Ahead and Upgrade your ColdFusion JVM





