全文不是一个可行的解决方案时动态mysql搜索

时间:2013-02-26 19:56:52

标签: php mysql search full-text-search

许多文章都会为您提供全文索引,以便为mysql搜索提供简单的解决方案。在适当的情况下可能就是这种情况,但是当无法使用全文(例如,跨表)时,我还没有看到接近全文的解决方案。我正在寻找的解决方案最好是能够匹配句子中任何内容的解决方案。

因此,搜索James Woods或搜索Woods James,可能都会返回文本James Woods所在的同一行。基本搜索方法会使搜索词的“混合匹配”无效。

可能的答案是用REGEXPLIKE替换全文。然后将搜索字词中的“空格”替换为|%,以便James Woods可能会James|Woods,因此James和{{1}的任意组合将返回结果。或者成为'%James%Woods%',效率会降低,但仍会返回Woods的匹配。

示例SQL

aren't necessarily exact

这真的是最好的方法吗?是否有任何技巧可以使这种方法(或其他方法)更有效地工作?我真的在寻找一个mysql解决方案,所以如果你的答案是使用另一个数据库服务,那么就这样吧,我会接受这个答案,但真正的问题是mysql的最佳解决方案。感谢。

2 个答案:

答案 0 :(得分:0)

在MySQL中你尝试过使用

MATCH()和AGAINST()函数的组合

他们会产生你猜的结果。

e.g。

用于以下数据集::

mysql> select * from temp;
+----+---------------------------------------------+
| id | string                                      |
+----+---------------------------------------------+
|  1 | James Wood is the matyr.                    |
|  2 | Wood James is the saviour.                  |
|  3 | James thames are rhyming words.             |
|  4 | Wood is a natural product.                  |
|  5 | Don't you worry child - Swedish House Mafia |
+----+---------------------------------------------+
5 rows in set (0.00 sec)

如果您需要james或wood存在

,此查询将返回以下结果
mysql>  select string from temp where match (string) against ('james wood' in bo
olean mode);
+---------------------------------+
| string                          |
+---------------------------------+
| James Wood is the matyr.        |
| Wood James is the saviour.      |
| James thames are rhyming words. |
| Wood is a natural product.      |
+---------------------------------+
4 rows in set (0.00 sec)

如果你要求詹姆斯和伍德这两个词应该存在,那么这个查询就行了。注意单词前面的“+”符号。检查此Boolean mode

mysql> select string from temp where match (string) against ('+james +wood' in b
oolean mode);
+----------------------------+
| string                     |
+----------------------------+
| James Wood is the matyr.   |
| Wood James is the saviour. |
+----------------------------+
2 rows in set (0.00 sec)

查找带有任何后缀的单词,它的工作方式类似

mysql> select string from temp where match (string) against ('Jame*' in boolean
mode);
+---------------------------------+
| string                          |
+---------------------------------+
| James Wood is the matyr.        |
| Wood James is the saviour.      |
| James thames are rhyming words. |
+---------------------------------+
3 rows in set (0.02 sec)

但请注意,Mysql的全文搜索尚不支持前缀搜索

mysql> select string from temp where match (string) against ('*ame*' in boolean
mode);
Empty set (0.00 sec)

我希望这会有所帮助。

善意的是,这个回复很晚,但我很感兴趣回复。

要了解详情,请查看此链接http://dev.mysql.com/doc/refman/5.5/en//fulltext-search.html

答案 1 :(得分:0)

我有点迟到了 - 所以道歉。

你提到你不能使用全文功能,因为你正在使用连接 - 好吧,虽然这是种类的情况,但有一种流行的方法可以解决这个问题。

考虑使用带有连接的全文搜索:

SELECT
    article.articleID,
    article.title,
    topic.title
FROM articles AS article
-----
    INNER JOIN (
        SELECT articleID
        FROM articles
        WHERE MATCH (title, keywords) AGAINST ("cat" IN BOOLEAN MODE)
        ORDER BY postDate DESC
        LIMIT 0, 30
    ) AS ftResults ON article.articleID = ftResults.articleID
-----
LEFT JOIN topics AS topic ON article.topicID = topic.topicID
GROUP BY article.id
ORDER BY article.postDate DESC

注意我是如何通过在另一个查询中运行全文搜索并按ID加入/匹配结果来设法保持topics加入完整的。

如果您不在共享托管上,请考虑使用SphinxLucene Solr与MySQL一起进行快速全文搜索。我使用过Sphinx并强烈推荐它。