Using MySQL Full text search in PHP
Wednesday, March 21st, 2007Full text search is the ability to perform text based search in MySQL database without matching exact text.
Exact text search such as “Starts with”, “Ends with” or “contains” can be performed using the LIKE operator in mysql. However this kind of a search requires the searcher to know the exact sequence of characters or words while performing the search.
MySQL full text search is an automatic quick and easy way to perform a search when the searcher may or may not type the words/characters in the same order as in the database.
Quick information
- Full text search can be performed on fields that are of “varchar” or “text” type.
- Full text search requires a “full text index” to be present in the database.
- A Full text index
- A full text index can be created in a single command as
- CREATE FULLTEXT INDEX <index_name> ON <table_name>(<column_name1>,<column_name2>)
- A full text index can be created in a single command as
- Full text index creation example
- CREATE FULLTEXT INDEX hotels ON hotel(name,description,review)
- Full text search query
- Example: Perform a full text search on hotel name
- Query :SELECT * FROM `hotel` WHERE MATCH(`name`) AGAINST (’leela’);
- Example:Perform a full text search on the hotel name as well as the description
- QUERY:SELECT * FROM `hotel` WHERE MATCH(`name`,`description`) AGAINST (’leela’);
MySQL support in determining the how relevant a search result is based on the “searched text”. Here is a quick way to harness the MySQL’s built-in relevance support
SELECT MATCH(’description’) AGAINST (’leela’) as `Relevance` FROM` hotel` WHERE MATCH(’description’) AGAINST (’leela’) as `Relevance` ORDER
BY Relevance DESC