Symfony 3:全文搜索

时间:2017-10-29 12:10:50

标签: symfony doctrine-orm

对于我的全文搜索,我使用了https://gist.github.com/ZeBigDuck/1234419/edaa13a851b1ea1e9926ec9c003ad762876ffe1d

它返回一个带有分数的数组:

array:1 [▼
0 => array:1 [▼
"score" => "2.181887149810791"
 ]
]

但我希望它返回一个实体集合。怎么样?

MatchAgainst类:

`
    

use Doctrine\ORM\Query\Lexer;
use Doctrine\ORM\Query\AST\Functions\FunctionNode;

class MatchAgainst extends FunctionNode
{    
public $columns = array();
public $needle;

public function parse(\Doctrine\ORM\Query\Parser $parser)
{
    $parser->match(Lexer::T_IDENTIFIER);
    $parser->match(Lexer::T_OPEN_PARENTHESIS);

    do {
        $this->columns[] = $parser->StateFieldPathExpression();
        $parser->match(Lexer::T_COMMA);
    } while (!$parser->getLexer()->isNextToken(Lexer::T_INPUT_PARAMETER));

    // Got an input parameter
    $this->needle = $parser->InputParameter();

    $parser->match(Lexer::T_CLOSE_PARENTHESIS);
}

public function getSql(\Doctrine\ORM\Query\SqlWalker $sqlWalker)
{
    $haystack = null;

    $first = true;
    foreach ($this->columns as $column) {
        $first ? $first = false : $haystack .= ', ';
        $haystack .= $column->dispatch($sqlWalker);
    }

    return "MATCH(" .
        $haystack .
        ") AGAINST (" .
        $this->needle->dispatch($sqlWalker) .
        " IN BOOLEAN MODE )";
}
}

`

在我的存储库中:

    $query = $this->getEntityManager()->createQuery('
        SELECT
        MATCH_AGAINST (d.title, :searchTerms) as score
        FROM AppBundle:Doc d
        WHERE MATCH_AGAINST (d.title, :searchTerms) > 1
        ORDER BY score desc
    ')
    ->setParameter('searchTerms', $q)
    ;

如果我将我的存储库功能改为像

那样
    $query = $this->getEntityManager()->createQuery('
        SELECT d
        FROM AppBundle:Doc d
        MATCH_AGAINST (d.title, :searchTerms)        
    ')
    ->setParameter('searchTerms', $q)
    ;

我得到了

  

错误:字符串的预期结束,得到'MATCH_AGAINST'

1 个答案:

答案 0 :(得分:0)

我必须这样做:

->select("d") ->andWhere("MATCH_AGAINST (d.title, :searchTerms) > 1") ->setParameter('searchTerms', $q)