14.9.7 Adding a User-Defined Collation for Full-Text Indexing
MySQL 8.4 Reference Manual Including MySQL NDB Cluster 8.4
14.9.7 Adding a User-Defined Collation for Full-Text Indexing
Warning
User-defined collations are deprecated; you should expect
support for them to be removed in a future version of MySQL.
The server issues a warning for any use of COLLATE
user_defined_collation in
an SQL statement; a warning is also issued when the server is
started with --collation-server
set equal to the name of a user-defined collation.
This section describes how to add a user-defined collation for
full-text searches using the built-in full-text parser. The
sample collation is like latin1_swedish_ci
but treats the '-' character as a letter
rather than as a punctuation character so that it can be indexed
as a word character. General information about adding collations
is given in Section 12.14, “Adding a Collation to a Character Set”; it is assumed
that you have read it and are familiar with the files involved.
To add a collation for full-text indexing, use the following procedure. The instructions here add a collation for a simple character set, which as discussed in Section 12.14, “Adding a Collation to a Character Set”, can be created using a configuration file that describes the character set properties. For a complex character set such as Unicode, create collations using C source files that describe the character set properties.
- Add a collation to the
Index.xmlfile. The permitted range of IDs for user-defined collations is given in Section 12.14.2, “Choosing a Collation ID”. The ID must be unused, so choose a value different from 1025 if that ID is already taken on your system.
<charset name="latin1">
...
<collation name="latin1_fulltext_ci" id="1025"/>
</charset>
2. Declare the sort order for the collation in the
latin1.xml file. In this case, the
order can be copied from
latin1_swedish_ci:
```
``
3. Modify thectypearray inlatin1.xml. Change the value
corresponding to 0x2D (which is the code for the'-'` character) from 10 (punctuation) to
01 (uppercase letter). In the following array, this is the
element in the fourth row down, third value from the end.
```
``` 4. Restart the server. 5. To employ the new collation, include it in the definition of columns that are to use it:
``` mysql> DROP TABLE IF EXISTS t1; Query OK, 0 rows affected (0.13 sec)
mysql> CREATE TABLE t1 ( a TEXT CHARACTER SET latin1 COLLATE latin1_fulltext_ci, FULLTEXT INDEX(a) ) ENGINE=InnoDB; Query OK, 0 rows affected (0.47 sec) ``` 6. Test the collation to verify that hyphen is considered as a word character:
``` mysql> INSERT INTO t1 VALUEs ('----'),('....'),('abcd'); Query OK, 3 rows affected (0.22 sec) Records: 3 Duplicates: 0 Warnings: 0
mysql> SELECT * FROM t1 WHERE MATCH a AGAINST ('----' IN BOOLEAN MODE); +------+ | a | +------+ | ---- | +------+ 1 row in set (0.00 sec) ```
