MongoDB+Doctrine: How to sort the query by text se

2019-02-20 05:38发布

I have a code like this that searches by the text index:

$expr = $queryBuilder->expr()->operator('$text', ['$search' => $this->value]);
$result = $queryBuilder->equals($expr)->getQuery()->execute();

But the result is not sorted by the relevance, which I want.

I found some info here but could not figure out how to add field score to search result using Doctrine.

I guess it would be easy from there just adding:

$queryBuilder->sort('score');

1条回答
时光不老,我们不散
2楼-- · 2019-02-20 06:04

I could not find relevant documentation, but I did find this issue on the project's Github repo. The issue has a milestone of 1.2.0 release, but it seems it has already been released in the 1.1.x branch. The issue has been closed via this commit.

From the commit, it seems that all you need to sort your results by the textScore metadata info is one extra method call on the query builder:

$result = $queryBuilder
    ->equals($expr)
    ->sortMeta('fieldToSearch', 'textScore') // <- this
    ->getQuery()
    ->execute();
查看更多
登录 后发表回答