Full text query with a single quote

后端 未结 3 571
时光说笑
时光说笑 2020-12-20 19:37

I\'m having a problem when I try to do a full text search in boolean mode using a string with a single quote and an asterisk wildcard, i.e. \"levi\'s*\": it seems to search

相关标签:
3条回答
  • 2020-12-20 19:51

    I guess you should double quote the string you need to search for if it contains single quotes

    Eg: MATCH(value) AGAINST ('"levi\'s"* lacost*' IN BOOLEAN MODE)

    0 讨论(0)
  • 2020-12-20 19:55

    Piggy backing on Bart's comment to handle the single quote and still have the search function as a like, I treated each term separately. So the logic is - if a term has a single quote, wrap it with parenthesis, otherwise leave it. Here is some php code that may help

    $term = preg_replace("/[']/", "\'", $term);
    $terms = explode(' ',$term);
    foreach ($terms as &$t) {
        if (strpos($t, "'")) {
            $t = "(".$t.")";
        }
    }
    $term = implode(' ',$terms);
    

    my match is AGAINST('$term' IN BOOLEAN MODE

    0 讨论(0)
  • 2020-12-20 20:03

    this gives you the two rows from your example:

    SELECT  *
    FROM    ft
    WHERE   MATCH(value) AGAINST ('"levi\'s" lacost*' IN BOOLEAN MODE)
    

    In http://dev.mysql.com/doc/refman/5.5/en/fulltext-boolean.html at the end, it talks about exact matches in double quotes. You then just escape the single quote and you are done.

    Using parentheses, you can add the asterisk:

    WHERE   MATCH(value) AGAINST ('(levi\'s)* lacost*' IN BOOLEAN MODE)
    
    0 讨论(0)
提交回复
热议问题