Introduction to MySQL Full Text Search
The following article provides an outline for MySQL Full Text Search. We can define Full Text Search as a procedure that performs search operation for documents which effortlessly is not equal the search standards. Here, the documents mean database entities comprising of textual data such as blog post, articles, product details and so on. Let us see about its features, in search engines we use regularly such as Google and Bing implements the full text search or say FTS to allow searching which is based on keywords. These engines firstly assemble the content info from different websites into the databases.
In MySQL, the search is supported with partial text searching using the regular expression and LIKE operator. This means FTS produces the results that may include the searched words distinctly or consists of words in a separate order like using either and, or conjunctions or, sentence type. Since the text column can be large and using these two factors may slow down the performance, it can influence flexible search and relevance ranking.
Full Text Search Methodologies in MySQL
- In MySQL, the server maintenances full-text searching and indexing. Here, the full-text index denotes an index of MySQL Data type FULLTEXT. This kind of indexes related to full-text can only be applied in MyISAM or InnoDB tables and used only for TEXT, CHAR or VARCHAR database columns. This index can be defined using a CREATE TABLE query during table creation or added afterward using CREATE INDEX or, ALTER TABLE.
- We need to index columns in a table before applying a full text search to its data so that lookup on these indexes applying a classy algorithm can help in search query to match rows. In some cases, when the table column values get modified then, MySQL will restructure the full text index as for search facilitated table columns MySQL routinely maintenances indexing and re-indexing data process.
We will implement the basic syntax for MySQL Full Text Search written as follows:
MATCH (ColumnName1, ColumnName2, ...) AGAINST (Expression[SearchModifier])
Here, the terms are given below:
- ColumnName1,ColumnName2,… denotes the list of table columns separated by comma that are to be searched.
- AGAINST () accepts a string that is to be searched. It also includes a modifier which specifies the kind of search to execute and this is optional type.
- The searching is case insensitive by default. If we require a case-sensitive search based on full text index, we need to apply a binary collation for those indexed columns.
We should remember few words are avoided for the full text searches below:
The word to be searched should have minimum length of:
- 3 characters for search indexes in InnoDB
- 4 characters for search indexes in MyISAM
Some words like stop words that are exactly mutual as we can say – as, the or it, displays in almost each document are neglected during search operation.
The full text search can be categorized into three types described as follows:
1. Natural Language Full Text Search
In this Natural Language Full Text Search, the searching process understands the search string defined as a free text with no exceptional operators needed further. This free text is Natural Human Language. This type of full text search is a natural language search technique if the modifier named IN NATURAL LANGUAGE MODE is provided or not.
Based on the text collection, the function MATCH() finds a string and results a valid value for every row of the table. This shows similarity between the searching string provided in the function AGAINST() as an argument and the text, which denotes set of columns in the table displayed as MATCH() list.
We have an elementary format for this natural language type mode of search as follows:
SELECT * FROM TableName WHERE MATCH(ColumnName1, ColumnName2) AGAINST (‘Searching_Terms’ IN NATURAL LANGUAGE MODE);
To demonstrate let us create a table and insert some records into it using queries:
CREATE TABLE Training(TID INT UNSIGNED PRIMARY KEY AUTO_INCREMENT NOT NULL, Label VARCHAR(255), Information TEXT, FULLTEXT (Label, Information)) ENGINE=InnoDB;
INSERT INTO Training (Label, Information) VALUES ('MySQL JOINS', 'MySQL JOINS are the clauses that are applied on our database tables to combine two or more tables to provide the result set.'), ('Databases', 'Databases are the storage area where we save our data records in the form of rows and columns in a MySQL table created.'), ('Data types', 'Data types are defined as an important factor to determine the type of data values inserted into table columns in MySQL Database. It includes character, integers, float, Boolean, etc.');
View the table:
SELECT * FROM Training;
Now, suppose we will search string using ‘left right’ in information field:
SELECT TID,Label FROM training WHERE MATCH(Information) AGAINST('MySQL'IN NATURAL LANGUAGE MODE);
The MySQL word is searched and count is displayed as per the row contents in a case-insensitive approach.
2. Boolean Full Text Search
In this type of search method, MySQL Boolean operators are involved in the search queries to perform complex search process. In Boolean approach, MySQL searches for related words instead of concept used in the natural languages search.
Here, we will implement the modifier as IN BOOLEAN MODE in the AGAINST function as argument option.
SELECT TID, Label,Information FROM training WHERE MATCH(Information) AGAINST(' +MySQL -databases' IN BOOLEAN MODE);
The query searches for rows that includes MySQL but not database using Boolean operators + and – i.e. include or exclude and more operators can be used similarly.
3. Query Expansion Search
This full text search is based on programmed relevance response that helps to broaden the search results queries. Here, users search for data that is according to their knowledge and experience typing too short keywords. Thus, to help users perform a better search, MySQL full text search technique has introduced this query expansion methodology.
In this case, the search engine finds out all the relevant words from all the rows that might match the keyword and displays the related results that the users are looking for.
Suppose, we have the following Books table:
Select * from Books;
We will use the modifier as WITH QUERY EXPANSION as argument in AGAINST() in the search query as shown below:
SELECT BookID,BookName FROM books WHERE MATCH(BookName) AGAINST('Science' WITH QUERY EXPANSION);
The output contains rows having only the term Science which is searched with other identical rows too using query expansion.
In MySQL server, Full Text Search permits users to execute full-text commands against data in database tables which are character based. MySQL Full text search shows some essential features like Natural SQL like interface, Completely dynamic index, Speed and Reasonable index size. The MySQL Full text search also powers the search results on different websites like News, Blogs, E-commerce, Travels, etc.
This is a guide to MySQL Full Text Search. Here we discuss the introduction and the full text search methodologies in MySQL. You may also have a look at the following articles to learn more –