zoukankan      html  css  js  c++  java
  • MySQL 8中使用全文检索示例

    首先建议张册测试用的表test,并使用fulltext说明将titlebody两列的数据加入全文检索的索引列中:

    drop table if exists test;
    create table test (
        id integer not null primary key auto_increment,
        title varchar(50),
        body longtext,
        fulltext(title, body) with parser ngram
    );
    

    然后往其中插入数据:

    insert into test (title, body) values 
    ('金缕衣', '劝君莫惜金缕衣,劝君惜取少年时。花开堪折直须折,莫待无花空折枝。'),
    ('送别', '山中相送罢,日暮掩柴扉。春草明年绿,王孙归不归。'),
    ('春晓', '春眠不觉晓,处处闻啼鸟。夜来风雨声,花落知多少。'),
    ('江雪', '千山鸟飞绝,万径人踪灭。孤舟蓑笠翁,独钓寒江雪。'),
    ('何满子', '故国三千里,深宫二十年。一声何满子,双泪落君前。');
    

    然后我们可以执行如下语句来查询“山”这个字:

    select * from test where match(title, body) against ('金缕衣' in natural language mode);
    

    结果如下:

    mysql> select * from test where match(title, body) against ('金缕衣' in natural language mode);
    +----+-----------+--------------------------------------------------------------------------------------------------+
    | id | title     | body                                                                                             |
    +----+-----------+--------------------------------------------------------------------------------------------------+
    |  1 | 金缕衣    | 劝君莫惜金缕衣,劝君惜取少年时。花开堪折直须折,莫待无花空折枝。                                 |
    +----+-----------+--------------------------------------------------------------------------------------------------+
    1 row in set (0.00 sec)
    

    或者我们可以用bool模式查看即包含“三千”又包含“二十”的数据:

    select * from articles where match(title, body) against ('+三千 +二十' in boolean mode);
    

    结果如下:

    mysql> select * from test where match(title, body) against ('+三千 +二十' in boolean mode);
    +----+-----------+--------------------------------------------------------------------------+
    | id | title     | body                                                                     |
    +----+-----------+--------------------------------------------------------------------------+
    |  5 | 何满子    | 故国三千里,深宫二十年。一声何满子,双泪落君前。                         |
    +----+-----------+--------------------------------------------------------------------------+
    1 row in set (0.00 sec)
    

    更多信息请参考这篇博客,写的非常好!:https://www.jianshu.com/p/645402711dac

  • 相关阅读:
    试用solace 消息平台
    mqtt5 share subscription 简单说明
    文件批量重命名神器:Bulk Rename Utility
    Elasticsearch入门,这一篇就够了
    burp suite使用(一) --- 抓包,截包,改包
    BurpSuite安装和配置
    ORA-01779: 无法修改与非键值保存表对应的列”中涉及的概念和解决方法
    一个非常有用的函数——COALESCE
    ORA-01779: cannot modify a column which maps to a non-key-preserved table
    Oracle批量、大量Update方法总结
  • 原文地址:https://www.cnblogs.com/zifeiy/p/10345420.html
Copyright © 2011-2022 走看看