MongoDB+Doctrine: How to sort the query by text search score(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:如何按文本搜索分数对查询进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:MongoDB+Doctrine:如何按文本搜索分数对查询进行排序
基础教程推荐
- XAMPP 服务器不加载 CSS 文件 2022-01-01
- 在 Woocommerce 中根据运输方式和付款方式添加费用 2021-01-01
- 超薄框架REST服务两次获得输出 2022-01-01
- 通过 PHP SoapClient 请求发送原始 XML 2021-01-01
- Libpuzzle 索引数百万张图片? 2022-01-01
- WooCommerce 中选定产品类别的自定义产品价格后缀 2021-01-01
- 在 PHP 中强制下载文件 - 在 Joomla 框架内 2022-01-01
- mysqli_insert_id 是否有可能在高流量应用程序中返回 2021-01-01
- 如何在 PHP 中的请求之间持久化对象 2022-01-01
- 在多维数组中查找最大值 2021-01-01