php - 有没有办法默认要求全文搜索中的所有单词?

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

我正在尝试找到一种方法,以便当用户进行搜索时,默认情况下所有单词都是必需的。

一开始这看起来很简单,只需将单词分解并添加 + 即可。在每个单词的开头签名;然而,当您开始尝试实现其他运算符时,事情就会变得复杂。

这就是我到目前为止所拥有的..

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);    

}

正如你所看到的,目前它只能做到这一点,尊重引用的字符串,但后来我开始考虑不分手 ()还有,如果它包含嵌套双引号怎么办,反之亦然......它开始变得非常毛茸茸的。

所以我试图弄清楚是否有一种方法可以做我想做的事情,而不会弄乱字符串,并且默认情况下只需要所有单词,除非用户特别用 - 否定它们。 .

最佳答案

您当然可以使用模式中的 preg_match() 和\b 作为单词边界吗?

然后您可以使用诸如

之类的内容分割搜索词
preg_match_all('/\b(.*)\b/', $matches);

我的想法可能是错误的,因为现在已经晚了,但它可能会给你一些继续下去的机会

关于php - 有没有办法默认要求全文搜索中的所有单词?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17001226/

相关文章:

php - CodeIgniter 添加和更新数据库没有成功,没有错误,没有,只是不会工作

javascript - 如何使用ajax调用从数据库中删除表行

powershell从文本文件中导入字符串后的数据

java - 在联系人数据库中快速搜索子串的算法

postgresql - 包含 tsvector 字段的可能原因?

php - 使用 PHP 替换大文件中的字符

MYSQL 根据另一张表求和

mysql - 节点红色,MySQL 错误

php - 使用 php 中的函数创建数据库连接?

php - PHP namespace 是否与前导斜杠一起使用