[MySQL Must Know and Know (15)] [Full Text Search]

Previous: [MySQL Must Know and Know (14)] [Combined Query]

+++++++++++++Start line++++++++++++++++

One, understand full text search

Several important limitations:
1. Performance: Wildcard and regular expression matching usually requires MySQL to try to match all rows in the table. Therefore, due to the increasing number of searched lines, these searches may be very time-consuming
. 2. Clear control: using wildcards and regular expression matching, it is difficult to clearly control what matches and what does not match
3. Intelligent results: although based on wildcards And regular expression search provides a very flexible search, but they can not provide an intelligent method of selecting results

Second, use full text search

In order to perform a full-text search, the columns to be searched must be indexed and re-indexed continuously as the data changes. After proper design of the table columns, MySQL will automatically perform all indexing and re-indexing.
After indexing, SELECT can be used with Match() and Against() to actually perform the search.

2.1 Enable full text search support

Generally, full-text search is enabled when the table is created. CREATE TABLE accepts the FULLTEXT clause, which gives a comma-separated list of indexed columns

mysql> CREATE TABLE productnotes
    -> (
    -> note_id int NOT NULL AUTO_INCREMENT,
    -> prod_id char(10) NOT NULL,
    -> note_date datetime NOT NULL,
    -> note_text text NULL,
    -> PRIMARY KEY(note_id),
    -> FULLTEXT(note_text)
    -> )ENGINE=MyISAM;

Don't use FULLTEXT when importing data

Updating the index takes time, although not a lot, but it takes time after all. You should import all the data first, then modify the table, and then define FULLTEXT.

2.2 Perform a full text search

Match() specifies the column to be searched, Against() specifies the search expression to be used

mysql> SELECT note_text
    -> FROM productnotes
    -> WHERE Match(note_text) Against('rabbit');

Insert picture description here

Use the complete Match() instructions

The value passed to Match() must be the same as in the definition of FULLTEXT(). If multiple columns are specified, they must be listed in the correct order

The previous example can also be simply completed with the LIKE clause

mysql> SELECT note_text
    -> FROM productnotes
    -> WHERE note_text LIKE '%rabbit%';

Insert picture description here

2.4 Boolean text search

Insert picture description here

MySQL supports another form of full text search, called Boolean

mysql> SELECT note_text
    -> FROM productnotes
    -> WHERE Match(note_text) Against('heavy' IN BOOLEAN MODE);

Insert picture description here

Matches lines that contain heavy but do not contain any words starting with rope

mysql> SELECT note_text
    -> FROM productnotes
    -> WHERE Match(note_text) Against('heavy -rope*' IN BOOLEAN MODE);

Insert picture description here

Search for rows containing rabbit and bait

mysql> SELECT note_text
    -> FROM productnotes
    -> WHERE Match(note_text) Against('+rabbit +bait' IN BOOLEAN MODE);

Insert picture description here

No operator is specified, this search matches lines that contain at least one of rabbit and bait

mysql> SELECT note_text
    -> FROM productnotes
    -> WHERE Match(note_text) Against('rabbit bait' IN BOOLEAN MODE);

Insert picture description here

Match the phrase rabbit bait

mysql> SELECT note_text
    -> FROM productnotes
    -> WHERE Match(note_text) Against('"rabbit bait"'IN BOOLEAN MODE);

Insert picture description here

Match rabbit and carrot, increase the level of the former and decrease the level of the latter

mysql> SELECT note_text
    -> FROM productnotes
    -> WHERE Match(note_text) Against('>rabbit <carrot' IN BOOLEAN MODE);

Insert picture description here

2.5 Instructions for full text search

1. When indexing full-text data, short words are ignored and excluded from the index 2.
MySQL has a built-in list of non-use words, these words are always ignored when indexing full-text data
3. If the table If the number of lines is less than 3 lines, the full text search will not return results
. 4. Ignore single quotes in words.
5. Only support full text search in the MyISAM database engine

+++++++++++++End line++++++++++++++++

Next: [MySQL Must Know and Know (16)] [Insert Data]

Guess you like

Origin blog.csdn.net/qq_42893334/article/details/108848050