MongoDB+Doctrine:如何按文本搜索分数对查询进行排序 [英] MongoDB+Doctrine: How to sort the query by text search score

查看:22
本文介绍了MongoDB+Doctrine:如何按文本搜索分数对查询进行排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这样一个按文本索引搜索的代码:

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.

我在这里找到了一些信息,但不知道怎么做使用 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');

推荐答案

我找不到相关文档,但我确实找到了 this issue 在项目的 Github repo 上.该问题具有 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆