1

I noticed that MATCH AGAINST cannot search words that have 3 characters or less. So I added LIKE along with MATCH AGAINST in one query.

Search query: css and javascript tip

Database entries:

id | subject
------------------------------------
1  | ten useful css tricks
------------------------------------
2  | performance tip for javascript

MySQL query:

3-character words: css, tip

SELECT id, MATCH (subject) AGAINST ('css and javascript tip' IN BOOLEAN MODE)
FROM articles WHERE MATCH (subject) AGAINST ('css and javascript tip')
OR subject LIKE '%css%' OR subject LIKE '%tip%'

This query works fine on my application (with a small database). I'm not sure if it's gonna cause any problems as the database grows.

Is this the correct way to search 3-character words while using MATCH AGAINST? If not, as for the example I provided, how should I seach for css and javascript at the same time?

4

2 回答 2

1

我用大约 100K 行来做这件事,而且速度并不慢。我认为还有其他选择,但对我来说这仍然足够:)

这是一个例子:

//...
$tokens = explode(' ',$s));
$squery = '';
array_map('trim', $tokens);

foreach($tokens as $token){
    if(strlen($token)>3)
        $squery .= "+$token* ";
    else
        $where .= " AND title LIKE '%$token%'";
if(!empty($squery))
    $select .= " , MATCH (title) AGAINST ('$squery' IN BOOLEAN MODE) AS score "
//...
于 2012-09-21T17:54:28.790 回答
0

对于 Windows 用户,您可以更改 Mysql 系统变量中的最小字数。编辑通常位于 programdata/mysql/mysql server 5.x 目录中的 my.ini 文件。有时您必须将 my.ini 文件复制到程序 files/mysql/mysql server ver 5.x 中才能正常工作。正如我在 Windows Server 2003 和 2008 中所经历的那样,而第一个在 Windows 7 中工作。

在 [mysqld] 下方插入以下内容:

ft_min_word_len = 2  
ft_stopword_file = ""

重新启动 Mysql,你可以在服务中执行此操作,停止并播放或任何你知道的。

然后重新索引你的表,确保引擎是 MISAM。虽然最近 InnoDB 也可以工作。

ft_stop_word_file 是导致某些单词被排除在匹配之外的文件。如果您有停用词文件,您可以将其等同于“”或指向您想要的纯文本文件。您可以创建自己的停用词文件,例如 mystopword.txt,里面将是您不想在匹配中包含的用空格分隔的单词。

于 2013-08-01T17:36:26.330 回答