zoukankan      html  css  js  c++  java
  • MySQL 全文检索(full_text) stopword 设置

    摘要:
           今天遇到一个奇怪的现象:通过全文检索的方法找不到关键"new"的数据,但是能找到"news"、"ne"”的记录。至于为什么找不到是以为没有"new"这个单词。之后在表里面看到是有new单词的。之后测试了好久,对全文索引(fulltext)知识点进行了复习[18章],结果还是不知道原因。最后发现了一个常常被忽视的知识点,刚好是处理问题的关键。

    方法:
    查看和全索引(fulltext)相关的变量:

    root@localhost : (none) 11:49:31>show variables like '%ft%';
    +--------------------------+----------------+
    | Variable_name            | Value          |
    +--------------------------+----------------+
    | ft_boolean_syntax        | + -><()~*:""&| |
    | ft_max_word_len          | 84             |
    | ft_min_word_len          | 2              |
    | ft_query_expansion_limit | 20             |
    | ft_stopword_file         | (built-in)     |
    +--------------------------+----------------+
    5 rows in set (0.00 sec)

    看到ft_stopword_file参数,怀疑"new"在stopword里面,所以检索不出来。而stopword 是有mysql built-in 自己控制的。尝试自己添加stopword,并且应用于服务器。
    在MySQL配置文件里面的[mysqld]选项组里添加:

    ft_stopword_file = /var/lib/mysql/stopword.txt

    修改stopword.txt文件的文件主和文件组:

     chown -R mysql:mysql /var/lib/mysql/stopword.txt

    编辑stopword.txt 文本,在里面添加(由于不知道格式,就测试了下,确认好了之后为)

    #关键字用单引号包含,并且用逗号隔开各个关键字。直接在一行里写入,不换行,如:
    'a','b','c',...

    因为不知道MySQL自己built-in它自己有哪写关键字,所以我就直接修改stopword.txt:

    #直接用空来取代之前built-in的stopword
    '',''

    最后重启服务器,再查看变量:

    root@localhost : (none) 11:52:39>show variables like '%ft%';
    +--------------------------+-----------------------------+
    | Variable_name            | Value                       |
    +--------------------------+-----------------------------+
    | ft_boolean_syntax        | + -><()~*:""&|              |
    | ft_max_word_len          | 84                          |
    | ft_min_word_len          | 2                           |
    | ft_query_expansion_limit | 20                          |
    | ft_stopword_file         | /var/lib/mysql/stopword.txt |
    +--------------------------+-----------------------------+
    5 rows in set (0.00 sec)

    重新制定成功,最后需要修复检索的表:

    repair table xx;

    最后,再运行之前的检索SQL,结果就出来了。另外,经过stopword 处理以后,索引文件体积可能会减少。

    SELECT *  FROM xx WHERE   MATCH (fulltext) AGAINST ('"new"' IN BOOLEAN MODE);
    244 rows in set (0.00 sec)

    相关网站:

    http://dev.mysql.com/doc/refman/5.5/en/fulltext-stopwords.html

    ~~~~~~~~~~~~~~~ 万物之中,希望至美 ~~~~~~~~~~~~~~~
  • 相关阅读:
    Selenium python 常用定位方法
    第四周总结
    python自然语言处理——提取关键词,标签
    python 调用百度地图api接口获取地理详细信息行政代码等
    python分词技术——jieba安装使用
    质量属性战术--6.易用性战术
    Kettle的使用——大数据清洗技术
    周总结2
    DataX的使用——大数据同步技术
    python编程:从入门到实践
  • 原文地址:https://www.cnblogs.com/zhoujinyi/p/3028320.html
Copyright © 2011-2022 走看看