MongoDB + Doctrine:如何通过文本搜索得分排序查询 [英] MongoDB+Doctrine: How to sort the query by text search score
问题描述
我有一个这样的代码,通过文本索引搜索:
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.
我发现了一些信息 here 无法弄清楚如何使用Doctrine添加字段分数来搜索结果。
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');
推荐答案
我找不到相关文档,关于项目Github回购的此问题。该问题具有1.2.0版本的里程碑,但似乎已经在1.1.x分支中发布。该问题已通过此提交关闭。
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.
从提交开始,您似乎需要通过 textScore 元数据信息对结果进行排序,这是查询构建器上一个额外的方法调用:
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();
这篇关于MongoDB + Doctrine:如何通过文本搜索得分排序查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!