【问题标题】:Is there a way by default to require ALL words in full text searching?有没有办法默认要求全文搜索中的所有单词?
【发布时间】:2013-06-04 18:31:00
【问题描述】:

我正在尝试找到一种方法,以便在用户进行搜索时默认需要所有单词。

一开始这似乎很容易,只需将单词分解并在每个单词的开头添加一个+ 符号即可;但是,当您开始尝试实现其他运算符时,它会变得很复杂。

这是我目前所拥有的......

function prepareKeywords($str) {

    // Remove any + signs since we add them ourselves
    // Also remove any operators we don't allow
    // We don't allow some operators as they would have no use in the search since we don't order our results by relevance
    $str = str_replace(array('+','~','<','>'), '', $str);

    // Remove anything more than once space
    $str = preg_replace('/\s{2,}/', ' ', $str);

    // Now break up words into parts
    $str = str_getcsv($str, ' ', '"');

    // Now prepend a + sign to the front of each word

    foreach ($ks as $key => $word) {

        // First we check to make sure the start of the word doesn't already contain an operator before we add the + sign to the start

        if (in_array($word{0}, array('-','<','>','~'))) {

        } else {
            $ks[$key] = '+' . $word;   
        }

    }

    // Now put word back to string
    //$ks = implode(' ', $ks);    

}

正如您所看到的,目前它只到此为止,尊重引用的字符串,但后来我开始考虑不要分解(),然后如果它包含嵌套的双引号,反之亦然...... . 它开始变得非常多毛。

所以我想弄清楚是否有一种方法可以在不弄乱字符串的情况下做我想做的事,并且只制作默认需要的所有单词,除非用户专门用 - 否定它们。

【问题讨论】:

    标签: php mysql full-text-search boolean-search


    【解决方案1】:

    你肯定可以在模式中使用 preg_match() 和 \b 作为单词边界吗?

    然后您的搜索词可以用类似的东西分割

    preg_match_all('/\b(.*)\b/', $matches);
    

    我可能想错了,因为这里已经很晚了,但它可能会给你一些东西去继续

    【讨论】:

    • 啊,来晚了。值得一试:)
    猜你喜欢
    • 1970-01-01
    • 2015-10-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-15
    • 1970-01-01
    • 2022-10-07
    • 1970-01-01
    相关资源
    最近更新 更多