MATCH ... AGAINST没有得出确切的结果

时间:2014-07-27 19:25:22

标签: php mysql sql full-text-search

我第一次使用MATCH ... AGAINST在PHP sql中工作,但有一个困扰我,我无法弄清楚如何解决它。这是我的代码:

SELECT * FROM m_artist WHERE match(artist_name) against('". $_POST['article_content'] ."' IN BOOLEAN MODE)

这是$_POST['article_content']:

Wildstylez Brothers Yeah Frontliner Waveliner

现在我的输出应该是:Wildstylez,Frontliner和Waveliner导致我的数据库。我这样做但除此之外我还得到了伏特加兄弟,2兄弟的Hardstyle以及兄弟这个词的更多原因。如何修复SQL只选择文字匹配?

1 个答案:

答案 0 :(得分:1)

全文搜索实际上是一个非常具有误导性的名称:您可以通过查询搜索全文(例如google),但不保证全文等于您的查询。

因此,根据documentation on Boolean Full-Text Searches,您的输入Wildstylez Brothers Yeah Frontliner Waveliner被解释为artist_name包含(至少)WildstylezBrothers,{{1}之一},YeahFrontliner为单词。这就是你得到的原因。 Waveliner,其中包含the Vodka Brothers。对于类似Google的目的,这正是您想要的,因为您希望获得有关您只知道Brothers上的“向我展示文章”的内容的详细信息。

您可能想要使用

music

artist_name LIKE '%name_part1%' OR artist_name LIKE '%name_part2%' ...

最简单的情况就是做

之类的事情
artist_name IN ('exact_name1', 'exact_name2', ...)

但由于名称本身包含空格,您将失去查找$names = explode(' ', $_POST['article_content']); $name_searches = array_map(function($a) {return 'artist_name = \''.mysql_real_escape_string($a).'\'';}, $names); $sql = "SELECT * FROM m_artist WHERE ".implode(" OR ", $name_searches); 的能力。

另一种方法可以是用'+'为所有单词加前缀并坚持2 Brothers of Hardstyle,你只能找到包含每个单词的艺术家。

如果这不是您想要的,请提供更多背景信息。