MySQL-FULLTEXT模式匹配

时间:2015-03-02 10:30:08

标签: php html mysql full-text-search

我有一个由FULLTEXT - 索引构成的MySQL查询,因为我需要一个相关性排序。 缺点是我需要像

那样匹配模式
  

SELECT * FROM table WHERE colum LIKE %word%

有没有办法在FULLTEXT - 搜索?

中实现这一点

2 个答案:

答案 0 :(得分:1)

以下是使用MySQL full Text search的方法:

在这里,您无需将较大的输入字符串分解为数组,以与每个单词进行比较。

请参考以下示例编写自己的示例:

  

双引号(“"”)字符中的短语匹配

     

仅包含按字面意思显示的短语的行。

我想解释一下Boolean Full Text Search;但我建议你也请通过Full Text Search using Query Expansion

让我们看一下示例表:

mysql> select * from articles;
+----+-----------------------+------------------------------------------+
| id | title                 | body                                     |
+----+-----------------------+------------------------------------------+
|  1 | PostgreSQL Tutorial   | DBMS stands for DataBase ...             |
|  2 | How To Use MySQL Well | After you went through a ...             |
|  3 | Optimizing MySQL      | In this tutorial we will show ...        |
|  4 | 1001 MySQL Tricks     | 1. Never run mysqld as root. 2. ...      |
|  5 | MySQL vs. YourSQL     | In the following database comparison ... |
|  6 | MySQL Security        | When configured properly, MySQL ...      |
+----+-----------------------+------------------------------------------+

mysql> SELECT * FROM articles WHERE MATCH (title,body)
     AGAINST ('"database comparison"' IN BOOLEAN MODE);

+----+-------------------+------------------------------------------+
| id | title             | body                                     |
+----+-------------------+------------------------------------------+
|  5 | MySQL vs. YourSQL | In the following database comparison ... |
+----+-------------------+------------------------------------------+

当引用单词时,顺序很重要:

mysql> SELECT * FROM articles WHERE MATCH (title,body)
     AGAINST ('"comparison database"' IN BOOLEAN MODE);

Empty set (0.01 sec)

当我们删除引号时,它会搜索包含单词" database"或"比较":

mysql> SELECT * FROM articles WHERE MATCH (title,body)
     AGAINST ('database comparison' IN BOOLEAN MODE);

+----+---------------------+------------------------------------------+
| id | title               | body                                     |
+----+---------------------+------------------------------------------+
|  1 | PostgreSQL Tutorial | DBMS stands for DataBase ...             |
|  5 | MySQL vs. YourSQL   | In the following database comparison ... |
+----+---------------------+------------------------------------------+

订单现在不重要:

mysql> SELECT * FROM articles WHERE MATCH (title,body)
     AGAINST ('comparison database' IN BOOLEAN MODE);

+----+---------------------+------------------------------------------+
| id | title               | body                                     |
+----+---------------------+------------------------------------------+
|  1 | PostgreSQL Tutorial | DBMS stands for DataBase ...             |
|  5 | MySQL vs. YourSQL   | In the following database comparison ... |
+----+---------------------+------------------------------------------+

如果我们想要获取行,包含单词" PostgreSQL"或短语"数据库比较",我们应该使用此请求:

mysql> SELECT * FROM articles WHERE MATCH (title,body)
     AGAINST ('PostgreSQL "database comparison"' IN BOOLEAN MODE);

+----+---------------------+------------------------------------------+
| id | title               | body                                     |
+----+---------------------+------------------------------------------+
|  1 | PostgreSQL Tutorial | DBMS stands for DataBase ...             |
|  5 | MySQL vs. YourSQL   | In the following database comparison ... |
+----+---------------------+------------------------------------------+

fiddle

确保您搜索的字词不在list of stopwords中,但会被忽略。

答案 1 :(得分:0)

是的,使用MATCH AGAINST FULLTEXT SEARCH就像这样

SELECT * FROM tablename WHERE MATCH(columnname)AGAINST(' value');

如果您想从多个表中搜索,请提及以逗号分隔的表名