MySQL 全文搜索支持

原文地址:http://www.javaarch.net/jiagoushi/700.htm

MySQL 全文搜索支持

从MySQL 4.0以上 myisam引擎就支持了full text search 全文搜索,在一般的小网站或者blog上可以使用这个特性支持搜索。
那么怎么使用了,简单看看:

1.创建一个表,指定支持fulltext的列

	CREATE TABLE articles (
		id INT UNSIGNED AUTO_INCREMENT NOT NULL PRIMARY KEY,
		title VARCHAR(200),
		body TEXT,
		FULLTEXT (title,body)
	);
	
2.插入一些数据作为测试

	INSERT INTO articles (title,body) VALUES
		('MySQL Tutorial','DBMS stands for DataBase ...'),
		('How To Use MySQL Well','After you went through a ...'),
		('Optimizing MySQL','In this tutorial we will show ...'),
		('1001 MySQL Tricks','1. Never run mysqld as root. 2. ...'),
		('MySQL vs. YourSQL','In the following database comparison ...'),
		('MySQL Security','When configured properly, MySQL ...');
		
3.select查询fulltext的列 

	SELECT * FROM articles
		WHERE MATCH (title,body) AGAINST ('database');
		
查询结果:

	5        MySQL vs. YourSQL        In the following database comparison ...    
	1        MySQL Tutorial               DBMS stands for DataBase ...   
	
全文查询中的boolean语句, + -跟普通的搜索引擎语法一样

	SELECT * FROM articles WHERE MATCH (title,body)
		 AGAINST ('+MySQL -YourSQL' IN BOOLEAN MODE);
		 
innodb不支持fulltext,当然可以使用http://sphinxsearch.com/ sphinx来dump数据库数据支持全文搜索。


posted @ 2013-06-02 16:50  zhwj184  阅读(189)  评论(0编辑  收藏  举报