3

I am using a full text query with MySql version 5.0.45 and I am trying to optimize it for my needs. The score system was working fine, however because I have added a stemmer before the input now, I had to use a wildcard on the search term. The problem is that now a stemmed word will match but return a score of 0. (ie: "restriction" gets stemmed to "restrict" and will still get recognized as a match but with a score of 0)

Here's the query:

$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";

The score calculates perfectly before when it's ('".$escaped_string."')) but not when you add the wildcard *. In both cases the matching works fine, the problem is that the score does not calculate if there is a wildcard.

Any help on this would be appreciated! (I hope I'm doing this mostly right)

4

1 回答 1

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";
于 2012-11-08T20:51:51.650 回答