当前位置:网站首页>MySQL - full text index

MySQL - full text index

2022-07-24 10:31:00 Camellia Populus

Full-text index ( English Search )

  • Full text index is mainly based on word segmentation index of string type , Mainly based on CHAR、VARCHAR and TEXT Field on , In order to be able to query string type fields with large amount of data more quickly .
  • Full text index is based on words ,MySQL The default participle is that all special symbols other than letters and numbers are participles .
  • MySQL from 3.23.23 Full text indexing is now supported ,MySQL5.6 In the past, it was only in the storage engine for MyISAM Create a full-text index on the data table of ,5.6 after InnoDB Start supporting full-text indexing (5.7 After that, Chinese full-text indexing is supported ) . By default , The search execution of full-text index is case insensitive , If the field associated with the full-text index is a binary data type , Just do a case sensitive search .

1. Define indexes when creating tables

CREATE TABLE tablename(
    propname1 type1,
    propname2 type2,
    ……
    propnamen type..n,
    FULLTEXT INDEX | KEY
    [indexname] (propnamen [(length)] ) );

Usage method

mysql> create table class(id int, name varchar(64), comment varchar(64), FULLTEXT INDEX comment_index(comment));	// establish class surface ,  Appoint comment For full-text indexing 
mysql> show create table class;										// Query table structure 
mysql> insert into class values(1, ' Zhang San ', 'I''m Zhang san');		// insert data 1
mysql> insert into class values(2, ' Li Si ', 'I''m Li si');			// insert data 2
mysql> select * from class where match(comment) AGAINST('Li');		// Inquire about 

 
 
2. Create an index on an existing table
Method 1 :
stay MySQL In addition to creating a full-text index through SQL sentence FULLTEXT INDEX To realize the external , You can also use SQL sentence CREATE FULLTEXT INDEX To achieve , Its grammatical form is as follows :

CREATE FULLTEXT INDEX indexname
    ON tablename( propname1 [ ( length ) ] ); 

In the above statement , keyword CREATE FULLTEXT INDEX For creating a full-text index .
The following example table already exists , It can be done by CREATE Statement to create a full-text index :

mysql> create table class(id int, name varchar(64), comment varchar(64));	// establish class surface 
mysql> create FULLTEXT INDEX comment_index ON class(comment); 		// Add full text index 
mysql> show create table class;										// Query table structure 
mysql> insert into class values(1, ' Zhang San ', 'I''m Zhang san');		// insert data 1
mysql> insert into class values(2, ' Li Si ', 'I''m Li si');			// insert data 2
mysql> select * from class where match(comment) AGAINST('Zhang');	// Inquire about 

Method 2 :
In addition to the above two ways to create a full-text index , stay MySQL You can also create a full-text index through SQL sentence ALTER To achieve , Its grammatical form is as follows :

ALTER TABLE tablename
	ADD FULLTEXT INDEX|KEY indexname(propname [(length)]);

Usage method

mysql> create table class(id int, name varchar(64), comment varchar(64));	// establish class surface 
mysql> alter table class add FULLTEXT INDEX comment_index(comment); // Add full text index 
mysql> show create table class;										// Query table structure 
mysql> insert into class values(1, ' Zhang San ', 'I''m Zhang san');		// insert data 1
mysql> insert into class values(2, ' Li Si ', 'I''m Li si');			// insert data 2
mysql> select * from class where match(comment) AGAINST('Zhang');	// Inquire about  // Inquire about 

 
 
 
 

MySQL8 Chinese word segmentation support

The configuration file my.ini(Windows 10 The default path : C:\ProgramData\MySQL\MySQL Server 8.0) Add the following configuration items in , meanwhile restart MySQL80 service :

[mysqld]			// Find this line 
ngram_token_size=2	// Copy this line ,  Set up 2 Words are a group of search words 

 Insert picture description here

mysql> create table class(id int, name varchar(64), FULLTEXT INDEX name_index(name) with parser ngram);	// establish class surface  name Set column to full-text index ,  Support Chinese word segmentation index 
mysql> insert into class values(1, ' Frenchman Zhang San ');		// insert data 1
mysql> insert into class values(2, ' Nicholas Zhao si ');		// insert data 2
mysql> select * from class where match(name) against(' Maniac ');	// Only query two words 

 Insert picture description here

原网站

版权声明
本文为[Camellia Populus]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/205/202207241025250897.html