针对查询的通配符匹配导致得分= 0(php / sql)

时间:2012-04-15 19:15:17

标签: mysql match wildcard

我正在使用MySql版本5.0.45的全文查询,我正在尝试根据我的需要对其进行优化。得分系统工作正常,但是因为我现在在输入之前添加了一个词干分析器,所以我不得不在搜索词上使用通配符。问题是,现在一个词干会匹配,但返回得分为0.(即:“限制”被限制为“限制”,仍然会被识别为匹配但得分为0)

以下是查询:

$escaped_string = mysql_real_escape_string($string);
$query = "SELECT DISTINCT A1.item_ID, item, 
              4.0 * (match (`item_1`) against ('". $escaped_string."*'))
              + 3.5 * (match (`item_2`) against ('".$escaped_string."*'))
              + 3.0 * (match (`item_3`) against ('".$escaped_string."*')) 
              + 2.5 * (match (`item_4`) against ('".$escaped_string."*'))
              + 1.5 * (match (`item_5`) against ('".$escaped_string."*'))
            as score
          FROM Items A1 LEFT OUTER JOIN Inventory A2 ON A1.item_ID=A2.item_ID
          WHERE MATCH(`item_1`, `item_2`,`item_3`,`item_4`,`item_5`) AGAINST ('".$escaped_string."*' IN BOOLEAN MODE)
          ORDER BY score DESC
          LIMIT 200";

分数在('".$escaped_string."'))之前完全计算,但在添加通配符*时则不计算。在这两种情况下,匹配工作正常,问题是分数不计算是否存在通配符。

对此有任何帮助将不胜感激! (我希望我这样做大多是正确的)

1 个答案:

答案 0 :(得分:0)

对我而言,似乎您忘记将IN BOOLEAN MODE添加到分数计算中,因为搜索运算符是此模式专有的;如果我正确理解http://dev.mysql.com/doc/refman/5.0/en/fulltext-boolean.html

我认为以下内容应该有效:

$escaped_string = mysql_real_escape_string($string);
$query = "SELECT DISTINCT A1.item_ID, item, 
          4.0 * (match (`item_1`) against ('". $escaped_string."*' IN BOOLEAN MODE))
          + 3.5 * (match (`item_2`) against ('".$escaped_string."*' IN BOOLEAN MODE))
          + 3.0 * (match (`item_3`) against ('".$escaped_string."*' IN BOOLEAN MODE)) 
          + 2.5 * (match (`item_4`) against ('".$escaped_string."*' IN BOOLEAN MODE))
          + 1.5 * (match (`item_5`) against ('".$escaped_string."*' IN BOOLEAN MODE))
        as score
      FROM Items A1 LEFT OUTER JOIN Inventory A2 ON A1.item_ID=A2.item_ID
      WHERE MATCH(`item_1`, `item_2`,`item_3`,`item_4`,`item_5`) AGAINST ('".$escaped_string."*' IN BOOLEAN MODE)
      ORDER BY score DESC
      LIMIT 200";