Apa itu full text search mysql

MySQL's full-text search capability has few user-tunable parameters. You can exert more control over full-text searching behavior if you have a MySQL source distribution because some changes require source code modifications. See Section 2.9, “Installing MySQL from Source”.

Full-text search is carefully tuned for effectiveness. Modifying the default behavior in most cases can actually decrease effectiveness. Do not alter the MySQL sources unless you know what you are doing.

Most full-text variables described in this section must be set at server startup time. A server restart is required to change them; they cannot be modified while the server is running.

Some variable changes require that you rebuild the FULLTEXT indexes in your tables. Instructions for doing so are given later in this section.

Configuring Minimum and Maximum Word Length

The minimum and maximum lengths of words to be indexed are defined by the and for

#define GWS_IN_USE GWS_PROB
0 search indexes, and and for
#define GWS_IN_USE GWS_PROB
3 ones.

Minimum and maximum word length full-text parameters do not apply to FULLTEXT indexes created using the ngram parser. ngram token size is defined by the option.

After changing any of these options, rebuild your FULLTEXT indexes for the change to take effect. For example, to make two-character words searchable, you could put the following lines in an option file:

[mysqld]
innodb_ft_min_token_size=2
ft_min_word_len=2

Then restart the server and rebuild your FULLTEXT indexes. For

#define GWS_IN_USE GWS_PROB
3 tables, note the remarks regarding myisamchk in the instructions that follow for rebuilding
#define GWS_IN_USE GWS_PROB
3 full-text indexes.

Configuring the Natural Language Search Threshold

For

#define GWS_IN_USE GWS_PROB
3 search indexes, the 50% threshold for natural language searches is determined by the particular weighting scheme chosen. To disable it, look for the following line in
#define GWS_IN_USE GWS_FREQ
1:

#define GWS_IN_USE GWS_PROB

Change that line to this:

#define GWS_IN_USE GWS_FREQ

Then recompile MySQL. There is no need to rebuild the indexes in this case.

By making this change, you severely decrease MySQL's ability to provide adequate relevance values for the function. If you really need to search for such common words, it would be better to search using

#define GWS_IN_USE GWS_FREQ
3 instead, which does not observe the 50% threshold.

Modifying Boolean Full-Text Search Operators

To change the operators used for boolean full-text searches on

#define GWS_IN_USE GWS_PROB
3 tables, set the system variable. (
#define GWS_IN_USE GWS_PROB
0 does not have an equivalent setting.) This variable can be changed while the server is running, but you must have privileges sufficient to set global system variables (see Section 5.1.8.1, “System Variable Privileges”). No rebuilding of indexes is necessary in this case.

Character Set Modifications

For the built-in full-text parser, you can change the set of characters that are considered word characters in several ways, as described in the following list. After making the modification, rebuild the indexes for each table that contains any FULLTEXT indexes. Suppose that you want to treat the hyphen character ('-') as a word character. Use one of these methods:

  • Modify the MySQL source: In

    #define GWS_IN_USE GWS_FREQ
    
    8 (for
    #define GWS_IN_USE GWS_PROB
    
    0), or in
    #define GWS_IN_USE GWS_FREQ
    
    1 (for
    #define GWS_IN_USE GWS_PROB
    
    3), see the
    mysql> set GLOBAL innodb_optimize_fulltext_only=ON;
    Query OK, 0 rows affected (0.01 sec)
    
    mysql> OPTIMIZE TABLE opening_lines;
    +--------------------+----------+----------+----------+
    | Table              | Op       | Msg_type | Msg_text |
    +--------------------+----------+----------+----------+
    | test.opening_lines | optimize | status   | OK       |
    +--------------------+----------+----------+----------+
    1 row in set (0.01 sec)    
    2 and
    mysql> set GLOBAL innodb_optimize_fulltext_only=ON;
    Query OK, 0 rows affected (0.01 sec)
    
    mysql> OPTIMIZE TABLE opening_lines;
    +--------------------+----------+----------+----------+
    | Table              | Op       | Msg_type | Msg_text |
    +--------------------+----------+----------+----------+
    | test.opening_lines | optimize | status   | OK       |
    +--------------------+----------+----------+----------+
    1 row in set (0.01 sec)    
    3 macros. Add
    mysql> set GLOBAL innodb_optimize_fulltext_only=ON;
    Query OK, 0 rows affected (0.01 sec)
    
    mysql> OPTIMIZE TABLE opening_lines;
    +--------------------+----------+----------+----------+
    | Table              | Op       | Msg_type | Msg_text |
    +--------------------+----------+----------+----------+
    | test.opening_lines | optimize | status   | OK       |
    +--------------------+----------+----------+----------+
    1 row in set (0.01 sec)    
    4 to one of those macros and recompile MySQL.

  • Modify a character set file: This requires no recompilation. The

    mysql> set GLOBAL innodb_optimize_fulltext_only=ON;
    Query OK, 0 rows affected (0.01 sec)
    
    mysql> OPTIMIZE TABLE opening_lines;
    +--------------------+----------+----------+----------+
    | Table              | Op       | Msg_type | Msg_text |
    +--------------------+----------+----------+----------+
    | test.opening_lines | optimize | status   | OK       |
    +--------------------+----------+----------+----------+
    1 row in set (0.01 sec)    
    2 macro uses a “character type” table to distinguish letters and numbers from other characters. . You can edit the contents of the
    mysql> set GLOBAL innodb_optimize_fulltext_only=ON;
    Query OK, 0 rows affected (0.01 sec)
    
    mysql> OPTIMIZE TABLE opening_lines;
    +--------------------+----------+----------+----------+
    | Table              | Op       | Msg_type | Msg_text |
    +--------------------+----------+----------+----------+
    | test.opening_lines | optimize | status   | OK       |
    +--------------------+----------+----------+----------+
    1 row in set (0.01 sec)    
    6 array in one of the character set XML files to specify that
    mysql> set GLOBAL innodb_optimize_fulltext_only=ON;
    Query OK, 0 rows affected (0.01 sec)
    
    mysql> OPTIMIZE TABLE opening_lines;
    +--------------------+----------+----------+----------+
    | Table              | Op       | Msg_type | Msg_text |
    +--------------------+----------+----------+----------+
    | test.opening_lines | optimize | status   | OK       |
    +--------------------+----------+----------+----------+
    1 row in set (0.01 sec)    
    4 is a “letter.” Then use the given character set for your FULLTEXT indexes. For information about the
    mysql> set GLOBAL innodb_optimize_fulltext_only=ON;
    Query OK, 0 rows affected (0.01 sec)
    
    mysql> OPTIMIZE TABLE opening_lines;
    +--------------------+----------+----------+----------+
    | Table              | Op       | Msg_type | Msg_text |
    +--------------------+----------+----------+----------+
    | test.opening_lines | optimize | status   | OK       |
    +--------------------+----------+----------+----------+
    1 row in set (0.01 sec)    
    6 array format, see Section 10.13.1, “Character Definition Arrays”.

  • Add a new collation for the character set used by the indexed columns, and alter the columns to use that collation. For general information about adding collations, see Section 10.14, “Adding a Collation to a Character Set”. For an example specific to full-text indexing, see Section 12.10.7, “Adding a User-Defined Collation for Full-Text Indexing”.

Optimizing InnoDB Full-Text Indexes

Running

mysql> REPAIR TABLE tbl_name QUICK;
0 on a table with a full-text index rebuilds the full-text index, removing deleted Document IDs and consolidating multiple entries for the same word, where possible.

To optimize a full-text index, enable and run

mysql> REPAIR TABLE tbl_name QUICK;
0.

mysql> set GLOBAL innodb_optimize_fulltext_only=ON;
Query OK, 0 rows affected (0.01 sec)

mysql> OPTIMIZE TABLE opening_lines;
+--------------------+----------+----------+----------+
| Table              | Op       | Msg_type | Msg_text |
+--------------------+----------+----------+----------+
| test.opening_lines | optimize | status   | OK       |
+--------------------+----------+----------+----------+
1 row in set (0.01 sec)    

To avoid lengthy rebuild times for full-text indexes on large tables, you can use the option to perform the optimization in stages. The

mysql> REPAIR TABLE tbl_name QUICK;
3 option defines the number of words that are optimized each time
mysql> REPAIR TABLE tbl_name QUICK;
0 is run. The default setting is 2000, which means that 2000 words are optimized each time
mysql> REPAIR TABLE tbl_name QUICK;
0 is run. Subsequent
mysql> REPAIR TABLE tbl_name QUICK;
0 operations continue from where the preceding
mysql> REPAIR TABLE tbl_name QUICK;
0 operation ended.

Rebuilding MyISAM Full-Text Indexes

If you modify full-text variables that affect indexing (, , or ), or if you change the stopword file itself, you must rebuild your FULLTEXT indexes after making the changes and restarting the server.

To rebuild the FULLTEXT indexes for a

#define GWS_IN_USE GWS_PROB
3 table, it is sufficient to do a
myisamchk --recover --ft_min_word_len=3 tbl_name.MYI
5 repair operation:

mysql> REPAIR TABLE tbl_name QUICK;

Alternatively, use

myisamchk --recover --ft_min_word_len=3 tbl_name.MYI
6 as just described. In some cases, this may be faster than a repair operation.

Each table that contains any FULLTEXT index must be repaired as just shown. Otherwise, queries for the table may yield incorrect results, and modifications to the table cause the server to see the table as corrupt and in need of repair.

If you use myisamchk to perform an operation that modifies

#define GWS_IN_USE GWS_PROB
3 table indexes (such as repair or analyze), the FULLTEXT indexes are rebuilt using the default full-text parameter values for minimum word length, maximum word length, and stopword file unless you specify otherwise. This can result in queries failing.

The problem occurs because these parameters are known only by the server. They are not stored in

#define GWS_IN_USE GWS_PROB
3 index files. To avoid the problem if you have modified the minimum or maximum word length or stopword file values used by the server, specify the same , , and values for myisamchk that you use for mysqld. For example, if you have set the minimum word length to 3, you can repair a table with myisamchk like this:

myisamchk --recover --ft_min_word_len=3 tbl_name.MYI

To ensure that myisamchk and the server use the same values for full-text parameters, place each one in both the

[mysqld]
ft_min_word_len=3

[myisamchk]
ft_min_word_len=3
4 and
[mysqld]
ft_min_word_len=3

[myisamchk]
ft_min_word_len=3
5 sections of an option file:

[mysqld]
ft_min_word_len=3

[myisamchk]
ft_min_word_len=3

An alternative to using myisamchk for

#define GWS_IN_USE GWS_PROB
3 table index modification is to use the
[mysqld]
ft_min_word_len=3

[myisamchk]
ft_min_word_len=3
7,
[mysqld]
ft_min_word_len=3

[myisamchk]
ft_min_word_len=3
8,
mysql> REPAIR TABLE tbl_name QUICK;
0, or
myisamchk --recover --ft_min_word_len=3 tbl_name.MYI
6 statements. These statements are performed by the server, which knows the proper full-text parameter values to use.