Have you ever thought how does the search functionality is implemented in all the websites! Most of the internet blogs and websites are powered by MySQL database. MySQL provides a wonderful way (Full-text Search) of implementing a little search engine in your website. All you have to do is to have MySQL 4.x and above. MySQL provides full text search capabilities that we can use to implement search functionality.
First let us setup a sample table for our example. We will create a table called Article.
CREATE TABLE articles (
id INT UNSIGNED AUTO_INCREMENT NOT NULL PRIMARY KEY,
Also add some sample data in this table. Execute following insert query.
INSERT INTO articles (title,body) VALUES
('MySQL Tutorial','DBMS stands for DataBase ...'),
('How To Use MySQL Well','After you went through a ...'),
('Optimizing MySQL','In this tutorial we will show ...'),
('1001 MySQL Tricks','1. Never run mysqld as root. 2. ...'),
('MySQL vs. YourSQL','In the following database comparison ...'),
('MySQL Security','When configured properly, MySQL ...');
Once the sample data is ready in our table, we can start with our full-text search functionality.
Natural Language Full-Text Searches</font>
Try to execute following Select query on our sample table.
SELECT * FROM articles
WHERE MATCH (title,body) AGAINST ('database');
You will be able to see following result:
5 MySQL vs. YourSQL In the following database comparison ...
1 MySQL Tutorial DBMS stands for DataBase ...
In above sql query we used MATCH (title,body) AGAINST (âÇśdatabaseâÇÖ) to select all the records by performing a full text search on columns title and body.
You can modify this query and create your own version to perform full-text search in your own database.
<font color=#0033E6>Boolean Full-Text Searches</font>
It may happen that you want to specify certain keywords in your search criteria. Also you may want to ignore certain keywords. Boolean Full-Text Search can used to perform a full text search for such requirements.
Check the following Select query.
SELECT * FROM articles WHERE MATCH (title,body)
AGAINST ('+MySQL -YourSQL' IN BOOLEAN MODE);
If you notice the above Select query, we have added IN BOOLEAN MODE in against(). This query will fetch all the records which has MySQL keyword but not YourSQL keyword. Notice the + and âÇô that we have specified before the keywords!
In implementing this feature, MySQL uses what is sometimes referred to as implied Boolean logic, in which:
+ stands for AND
- stands for NOT
[no operator] implies OR
Following are few examples for the boolean search criteria.
Find rows that contain at least one of the two words.
Find rows that contain both words.
Find rows that contain the word âÇťappleâÇŁ, but rank rows higher if they also contain âÇťmacintoshâÇŁ.
Find rows that contain the word âÇťappleâÇŁ but not âÇťmacintoshâÇŁ.
Find rows that contain the word âÇťappleâÇŁ, but if the row also contains the word âÇťmacintoshâÇŁ, rate it lower than if row does not. This is âÇťsofterâÇŁ than a search for âÇś+apple -macintoshâÇÖ, for which the presence of âÇťmacintoshâÇŁ causes the row not to be returned at all.
âÇś+apple +(>turnover <strudel)âÇÖ
Find rows that contain the words âÇťappleâÇŁ and âÇťturnoverâÇŁ, or âÇťappleâÇŁ and âÇťstrudelâÇŁ (in any order), but rank âÇťapple turnoverâÇŁ higher than âÇťapple strudelâÇŁ.
<font color=#0033CC> Restrictions</font>
The Full-text searches are supported for MyISAM tables only. As of MySQL 4.1, the use of multiple character sets within a single table is supported. However, all columns in a FULLTEXT index must use the same character set and collation. The MATCH() column list must match exactly the column list in some FULLTEXT index definition for the table, unless this MATCH() is IN BOOLEAN MODE. Boolean-mode searches can be done on non-indexed columns, although they are likely to be slow.