【问题标题】:MySql fulltext search with multiple containing/starts with/ends with words or partial matching wordsMySql全文搜索具有多个包含/开头/结尾的单词或部分匹配的单词
【发布时间】:2017-11-19 04:49:32
【问题描述】:

每一个。

我正在使用 MySQL 数据库并面临全文搜索的问题。我将我的表转换为 MyISAM。

这是我的查询..

$inputwords=" balu text ama som" (// input words by user in a single input box)
$result=SELECT * FROM my table Where MATCH (data, remark, purpose) AGAINST ('$inputwords' IN BOOLEAN MODE);

它给出与任何输入词完全匹配的所有行。

  1. 我的要求是“balu”- baluch、balusahi、hotbalu 等。通配符等所有其他词也是如此。
  2. 我再次要求包含上述所有单词的唯一行。

任何帮助......请。

【问题讨论】:

    标签: mysql search full-text-search


    【解决方案1】:

    要获得全文MATCH() AGAINST() 中的LIKE 行为,您必须使用* 表示反对,这相当于使用% 表示LIKE。这个想法是让整个字符串像这样:

    $inputWords = "*balu* *text* *ama* *som*";
    

    代码如下:

    $inputWords = "balu text ama som"; // input words by user in a single input box
    
    // Create array of words by splitting with space char
    $keywords = explode(" ", $inputWords);
    
    $words = '';
    
    foreach($keywords as $key => $value) {
        // concatenate all words following by space, and add * at start and end of the word
        $words .= '*'.$value.'*';
    
        // add space only if it is not the last word
        if($key < count($keywords) - 1)
            $words .= ' ';
    }
    

    然后你就可以拥有你的:

    $result=SELECT * FROM my table Where MATCH (data, remark, purpose) AGAINST ('$inputwords' IN BOOLEAN MODE);
    

    单词“balu”将匹配baluch、balusahi、hotbalu。

    我不知道它是如何降低性能的,但它仍然比我的服务器中的 Like '%word%' 快得多。

    【讨论】:

    • * 仅用作截断运算符,因此它是“开头为”;它不能做“以”结尾,所以像 against('*text') 这样的匹配不起作用
    • @lunadir 正确。但是使用 MyISAM,在自然语言模式下的默认全文搜索不会匹配 50% 或更多的表行中存在的单词。因此,要么传递给 InnoDB 引擎,要么在 BOOLEAN 模式下使用 FULL TEXT 和这个后备。
    • 是的,我没有注意到 MyISAM 部分
    猜你喜欢
    • 1970-01-01
    • 2023-01-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-12
    • 2020-01-01
    • 1970-01-01
    相关资源
    最近更新 更多