
A MySQL full-text search index allows you to find matches for a given keyword against character-based columns. These types of indices are optimized for regular text columns and are much faster compared to wildcards searches. If you have a database application where end-users frequently search for words that don't perfectly match your records, consider implementing MySQL full-text search indices. For instance, assume you have a products table with thousands of records where a single item is saved as a WI-FI ROUTER WITH SIM SLOT. If a new staff member who is not conversant with your catalog tries to look up the item with a keyword like SIM CARD WIFI ROUTER, MySQL might return an empty set even if you use the wildcard ('%..%') search against the product_name column.
To overcome the above challenge, use MySQL full-text search index on your InnoDB and MyISAM tables for VARCHAR, CHAR, and TEXT fields. Under the hood, this feature splits the value of text-based columns into single words and creates an index based on them to make the searches faster. You can implement this technology to create a search engine for your online shopping carts, blogs, or HTML documents.
In this guide, you'll set up a test database and a table. You'll then populate the table with some records and run different types of full-text searches to see how the technology works.
Before you begin, make sure you've got the following:
sample_db DatabaseSSH to your server and run the command below to log in to MySQL as root.
Enter the root password of your MySQL server and press Enter to continue. Once the mysql> command prompt appears, set up a sample_db database.
Tell the MySQL server to use the sample_db database for subsequent statements.
Next, create a products table based on the InnoDB engine and define the product_name as a FULLTEXT index.
Insert the following records into the products table by running the commands below one by one.
Confirm that the data is in place by running a SELECT statement against the products table.
Ensure you get a list of all products as shown below.
Next, you're going to run some keyword searches against the table. Please note, there are three types of Full-Text searches.
Boolean operators such as +.Run a natural-language full-text mode search by implementing the MySQL MATCH(...) and AGAINST(...) functions.
Confirm the below output. As you can see from the results below, the full-text search is very powerful since even if you specified the word 'WIFI MySQL was able to retrieve a record having the word WI-FI.
In Boolean Full-Text mode, you can put certain characters in the beginning or at the end of a keyword when running a search. Use the + and the - operators to indicate the words you want to include or exclude in the results.
For instance, run the query below to retrieve all records that match the keyword LED and contains the word plastic.
Output
Use a leading minus - operator to return results that match the keyword LED but do not contain the word plastic.
Output
Still, in BOOLEAN MODE, you can enclose your keyword(s) in double-quotes to return only the rows that literally match the words as you've typed them in the same order. For instance, to look for the exact row that contains the phrase SMART PHONE, type the command below.
Output
You can use a full-text search with a QUERY EXPANSION statement only if the search phrase is too short. This adds relevant rows to the results.
By specifying the QUERY EXPANSION statement, you're instructing MySQL to widen the search by running it twice. In the first round, MySQL runs a search against the given keyword such as WIFI ROUTER, and once it retrieves high relevant rows, it conducts a second search based on the values of the top-ranked rows returned during the first search.
To put this into perspective, first, run the query with the keyword WIFI ROUTER in NATURAL LANGUAGE MODE
The above NATURAL LANGUAGE MODE query only returns two rows as you can see from the output below.
Next, run the WIFI ROUTER query again but this time around, include the QUERY EXPANSION statement modifier.
As you can confirm from the output below, four records have been returned.
If you examine the above search results carefully, you'll see that the third and fourth records (5G SMART PHONE WITH NANO SIM and NANO SIM CARD CUTTER) were returned based on the results of the first (SIMCARD ENABLED 4G WIFI ROUTER) and the second record (WI-FI ROUTER WITH SIM SLOT).
The MySQL QUERY EXPANSION statement is very powerful because indeed, the 5G SMART PHONE WITH NANO SIM and NANO SIM CARD CUTTER are somehow related to your search term WIFI ROUTER.
Optionally, you may retrieve a relevance ranking score when conducting full-text searches. Relevance is the measure of accuracy used by the MySQL server to sort returned rows based on their importance and this is highly determined by the total number of words that match a search term in a record as well as their order.
To see how MySQL allocates a relevance score, search the keyword LED SMART WATCH and include a relevance_score column using the SQL command below.
From the output below, you can see that the LED SMART PLASTIC WATCH has been allocated the highest score of ~1.60 since it contains all search keywords that you've defined almost in the same order.
By default, the MySQL server may not return results based on keyword terms that are shorter than 3 characters. For instance, try running the keyword DY against the products table in IN NATURAL LANGUAGE MODE.
The following output confirms that an empty set has been returned for the keyword DY although you have a matching product named DY MODEL 2T AIR CONTROL VALVE
In MySQL, the minimum size of the search term is controlled by the innodb_ft_min_token_size variable. Confirm if this is the current value by running the command below.
You can see that the default value is 3 unless you had previously changed it in the MySQL settings.
If you have two many two-lettered words, change the value of innodb_ft_min_token_size by editing the MySQL settings.
First, exit from the MySQL command-line interface.
Then, open the MySQL configuration file.
Add the following information into the bottom of the file.
Save and close the configuration file when you're through with editing. Then, restart the MySQL server to reload the changes.
Log in back to the MySQL server as root.
Key in the root password and press Enter to proceed. Then, switch back to the sample_db database.
First, retrieve the name of the full-text index from the information_schema.
Output
Rebuild the product_name full-text index.
Make sure you get an output confirming that the indices were rebuilt succesfully.
Try to run your two-lettered full-text search again.
You should now see the output below that confirms that MySQL full-text search is now working on two-lettered words.
In this tutorial, you've created a database and learned how to perform full-text searches against a table. You can use the technology in this guide to implement an intuitive search engine feature for your website, products' catalog, or blog by including the MySQL code in your favorite scripting languages such as PHP, Python, or Node. js.
0 Comments
Be the first to comment and share your perspective with the community.