As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center提供指导。




已关闭8年。




我们已经使用MySQL全文搜索已有好几年了,但是要求已经改变。我们想将AND/OR/NOT参数解析为MySQL可以理解的形式。我已经编写了一个单元测试,很明显这很复杂。

我敢肯定会有更多的人遇到这个问题,所以我想必须有某种可以为我做这件事的图书馆。我已经尝试过Google,但是不幸的是,我找不到这样的库。有人认识一个好人吗?

该库应该能够处理引号,括号,AND/OR/NOT运算符,并且在我们的情况下,它应该默认为AND而不是OR(如果未设置运算符)。这是我的一些预期结果:
  • 'ict'变为'+ ict'
  • 'ict it'变为'+ ict + it'
  • 'ict OR it'变成'ict it'
  • 'NOT ict'变为'-ict'
  • 'it NOT ict'变为'+ it -ict'
  • 'web AND(ict OR it)'变为'+ web +(ict it)'
  • 'ict OR(it AND web)'变成'ict(+ it + web)'
  • 'ict NOT(ict AND it AND web)'变为'+ ict-(+ ict + it + web)'
  • 'php OR(NOT WEB NOT NOT NOT ICT)成为'PHP(-Web-嵌入式ITT IT)'
  • '(web嵌入)(ict或它)'变成'+(web嵌入)+(ict)'
  • 开发AND(web OR(ict和php))'变为'+ develop +(web(+ ict + php))'
  • '“ict”变为'+“ict”'
  • '“ict OR it”'保持为'+“ict OR it”'

  • 这是我们最近几年使用的功能(无法正常运行):
    /**
     * Parses search string.
     * @param string $s The unparsed search string.
     * @return string $s The parsed search string.
     */
    public function parseSearchString( $s )
    {
        // Place a space at the beginning.
        $s = ' ' . $s;
    
        // AND - Remove multiple spaces, AND, &.
        $s = preg_replace( '/\s\s+/', ' ', $s );
        $s = preg_replace( '/\sAND\s/i', ' ', $s );
        $s = preg_replace( '/\s&\s/', ' ', $s );
    
        // OR - Make replacements. Execute double, so we replace all occurences.
        $s = preg_replace( '/(\w+)\s(?:OR|\|)\s(\|?\w+)/i', '|\\1|\\2', $s );
        $s = preg_replace( '/(\w+)\s(?:OR|\|)\s(\|?\w+)/i', '|\\1|\\2', $s );
        $s = preg_replace( '/(\w+)\s*(?:\\\|\\/)\s*(\|?\w+)/i', '|\\1|\\2', $s );
        $s = preg_replace( '/(\w+)\s*(?:\\\|\\/)\s*(\|?\w+)/i', '|\\1|\\2', $s );
    
        // NOT
        $s = preg_replace( '/\bNOT\s(\w+)/i', '|-\\1', $s );
    
        // Quoted strings.
        $s = preg_replace( '/\s"/', ' +"', $s );
    
        // Place + in front of words.
        $s = preg_replace( '/\s(\w+)/', ' +\\1', $s );
    
        // Replace | to spaces.
        $s = preg_replace( '/\|/', ' ', $s );
    
        return trim( $s );
    }
    

    最佳答案

    这可能无法回答您的问题,但是如果您需要更高级的全文本搜索功能,我建议您使用诸如狮身人面像或apache solr之类的工具。 https://stackoverflow.com/a/1297561/2170561

    关于php - 是否有一个好的PHP库可用于将 bool 搜索运算符解析为MySQL全文运算符?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/16016723/

    10-11 05:20