欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

MySQL-使用中文分词的全文索引

程序员文章站 2022-03-04 23:25:04
...

1.基本使用


向添加title和text字段的全文索引
ALTER TABLE content ADD FULLTEXT INDEX text_index(title,`text`) WITH PARSER ngram;


1.按自然语言搜索模式查询 (默认)

SELECT * FROM content WHERE MATCH (title,TEXT) AGAINST ('如何' IN NATURAL LANGUAGE MODE)

2.按布尔全文搜索模式查询 
(1)匹配既有管理又有数据库的记录 

SELECT * FROM content WHERE MATCH (title,TEXT) AGAINST ('+数据库 +管理' IN BOOLEAN MODE)2)匹配有数据库,但是没有管理的记录 

SELECT * FROM content WHERE MATCH (title,TEXT) AGAINST ('+数据库 -管理' IN BOOLEAN MODE)3)匹配MySQL,但是把数据库的相关性降低

SELECT * FROM content WHERE MATCH (title,TEXT) AGAINST ('>数据库 +MySQL' IN BOOLEAN MODE)

3.查询扩展模式,比如要搜索数据库,那么MySQL,oracle,DB2也都将会被搜索到,

SELECT * FROM content WHERE MATCH (title,TEXT) AGAINST ('数据库' WITH QUERY EXPANSION);

2.分页

SELECT * FROM content tc WHERE MATCH (title,TEXT) AGAINST ('美国' IN NATURAL LANGUAGE MODE) ORDER BY tc.heat_value DESC  LIMIT 2

3.结合spring jpa使用全文检索

@Query(value = "SELECT * FROM accounts WHERE (COALESCE(first_name,'') LIKE %:firstName% AND COALESCE(last_name,'') LIKE %:lastName%)", nativeQuery = true)
public List<Account> searchByFirstnameAndLastname(@Param("firstName")String firstName,@Param("lastName")String lastName);

参考链接
如何使用jpa进行mysql全文检索

相关标签: 全文检索