MySQL full text search

Preparation before configuration

MySQL > 6.* versions

1. Prepare data table
    mysql Copy table structure and information to sql of another table
    1. Copy table structure and data to new table
        CREATE TABLE new table SELECT * FROM old table;
        
    2. Only copy table structure to new table
        CREATE TABLE new Table SELECT * FROM old table WHERE 1=2; (ie: let the WHERE condition not hold.)
        CREATE TABLE new table LIKE old table; (mysql4.0.25 does not support, mysql5 already supports)
        
    3. Copy the data of the old table to the new table (Assuming the two tables have the same structure)
        INSERT INTO new table SELECT * FROM old table;
        
    4. Copy the data of the old table to the new table (assuming the two tables have different structures)
        INSERT INTO new table (field 1, field 2,... .) SELECT field 1, field 2, ... FROM old table;

    5. Copy the additional condition information of the old table
        INSERT INTO target table (field 1, field 2, ...) 
        SELECT field 1, field 2, ... FROM source table  
            WHERE not exists (select * from target table  
            where target table. CompareField = SourceTable.CompareField);

Second, full-text retrieval
    1. MySQL full-text retrieval is to use the correlation between query keywords and query column content for retrieval, and full-text indexing can be used to improve
        the matching speed.
    
    2. Configuration conditions: TABLE needs to be of MyISAM type;
        col1 and col2 must be of char, varchar or text type;
        full-text indexes (FULLTEXT indexes) are established on col1 and col2 respectively;
        MySQL configuration my.ini [mysql] add ft_min_word_len = 1 
        -- (default 4, can be viewed through SHOW VARIABLES LIKE 'ft_min_word_len');
    
    3. Use the syntax  
        SELECT * FROM tab_name WHERE MATCH ('column name 1, column name 2...column name n') AGAINST('word 1 word 2 word 3 ... word m');
        that is: MATCH is equivalent to the column to be matched, and AGAINST is the content to be found.
        
    4. Retrieval methods
        a. Natural language retrieval: IN NATURAL LANGUAGE MODE
        b. Boolean retrieval: IN BOOLEAN MODE (commonly used)
            Features: 
              ·Do not exclude more than 50% of the rows that match. 
              • Does not automatically sort in reverse by relevance. 
              ·Can be searched on fields without FULLTEXT index, but will be very slow. 
              · Limit the longest and shortest strings. 
              · Apply Stopwords.
            Grammar rules:
                 + must have (data bars that do not contain this keyword are ignored). 
                 - Not allowed (excluding the specified keyword, all containing the keyword will be ignored). 
                 > Increase the weight value of the matching data. 
                 < Decrease the weight value of this matching data.
                 ~ Turns its correlation from positive to negative, indicating that owning the word will reduce the correlation (but unlike - which excludes it), it just
                       reduces the weight value when it comes later. 
                 * Wild words, unlike other grammars placed in the front, this should be followed by the string. 
                 " " Use double quotation marks to enclose a sentence to indicate that it must be completely consistent and cannot be split.
            For example:
                @ SELECT * FROM articles WHERE MATCH (title,content) AGAINST ('+apple -banana' IN BOOLEAN MODE);
                Note: + means AND, that is, must be included. - means NOT, i.e. must not contain. That is: the returned record must contain apple, and cannot contain banner.
                
                @SELECT * FROM articles WHERE MATCH (title,content) AGAINST ('apple banana' IN BOOLEAN MODE);
                Note: There is a space between apple and banana, and a space means OR. That is: the returned record contains at least one of apple and banana.
                
                @ SELECT * FROM articles WHERE MATCH (title,content) AGAINST ('+apple banana' IN BOOLEAN MODE);
                Note: The returned record must contain apple, and banana may or may not be included, if it is included, it will get a higher weight
                
                @ SELECT * FROM articles WHERE MATCH (title,content) AGAINST ('+apple ~banana' IN BOOLEAN MODE);
                 Note: ~ is the familiar XOR operator. The returned record must contain apple. If it also contains banana, the weight will be reduced.
                    But it's not as strict as +apple -banana, because the latter doesn't return at all if it contains banana.
                    
                @SELECT * FROM articles WHERE MATCH (title,content) AGAINST ('+apple +(>banana <orange)' IN BOOLEAN MODE);
                 Note: Returns records that must contain both "apple banana" or must also contain "apple orange" .
                    If records containing both "apple banana" and "apple orange" are included, the weight of "apple banana" is higher than the weight of "apple orange".
                    
        c. Query expansion retrieval: WITH QUERY EXPANSION
        
3. Precautions:
    1. The default search is case-insensitive. To be case-sensitive, the character set of the column should be changed from utf8 to utf8_bin.
    2. The preset MATCH...AGAINST is sorted by relevance, from high to low.
    3. The fields in MATCH(title, content) must be exactly the same as the fields in FULLTEXT(title, content).
        If you only need to check the title or content field, you have to build another FULLTEXT(title) or FULLTEXT(content), and because of this,
        the fields of MATCH() cannot span tables.
    4. MySQL does not support Chinese full-text indexing. The reason is very simple: unlike English, Chinese characters are written together, and there is no place where MySQL can find word segmentation;
        If your MySQL is installed on the Windows platform, you can use the full-text index to directly store Chinese without transcoding.
        However, if your MySQL is installed on Linux, you need to perform transcoding (urlencode / base64_encode / json_encode / location / pinyin), etc. to
        encode it so that it is stored in the database in the form of "alphanumerics".
    5. InnoDB newly introduced engine support for FULLTEXT indexes after MySQL 5.6. Before, only MyISAM engine supported FULLTEXT indexes.
        ***** That is, MySQL 5.6 and later versions do not require the type of data table, and can be used.

Article reference: http://www.cnblogs.com/martinzhang/p/3220345.html

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325906426&siteId=291194637