【问题标题】:mySQL LIKE / MATCH to return more accurate search resultsmySQL LIKE / MATCH 返回更准确的搜索结果
【发布时间】:2012-02-26 14:46:27
【问题描述】:

我正在尝试为我网站上的新闻部分构建更准确的搜索功能。我曾经在内容区域使用LIKE,但是,我的问题是当我在数据库中搜索“三搜索”时,它被写为“三词搜索”,它不会返回任何内容。

有人建议我使用 MATCH,但我无法使用它返回任何结果。我的查询从多个表返回各种字段。我的搜索区域有超过 1 个搜索块,如果需要我可以更改它,但我需要它返回所有结果,无论它们是否有低分。

这是我的 SQL:

$sql = "SELECT news.id, news.name, news.date, news_categories.name as catName, news.author, statuses.name as statusName, MATCH(news.name, news.summary, news.content, news.keywords) AGAINST ('".$content."') AS score FROM news, news_categories, statuses WHERE news.name LIKE '%".$name."%' AND MATCH(news.name, news.summary, news.content, news.keywords) AGAINST ('".$content."') AND news_categories.id LIKE '%".$category."%' AND news.status_id LIKE '%".$status."%' AND news.status_id=statuses.id AND news.category_id=news_categories.id";

【问题讨论】:

    标签: php mysql sql-like


    【解决方案1】:

    您必须在要搜索的字段上创建FULLTEXT 索引。例如:

    ALTER TABLE news
      ADD FULLTEXT(name, summary, content, keywords);
    

    现在,您可以在查询的MATCH() ... AGAINST 子句中使用这些字段。阅读全文搜索MySQL documentation了解更多信息。

    【讨论】:

      【解决方案2】:

      全文在这里并没有太大帮助,因为我的要求是始终获取值。对我来说,一种更简单有效的方法是对包含搜索词的变量执行 str_replace 并将所有“”替换为 %,这样该词实际上就是“%three%word%search%”

      【讨论】:

        猜你喜欢
        • 2014-12-16
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-01-24
        • 2013-10-01
        • 1970-01-01
        相关资源
        最近更新 更多