php - 在 Symfony 中获取 Elasticsearch 分数
问题描述
如果您通过 cURL 发出 get 请求,Elasticsearch 会提供一个 score 字段。
{
"_index": "twitter",
"_type": "tweet",
"_id": "123",
"_score": 4.2,
"firstName": "Max"
"lastName": "Mustermann"
}
有没有办法在 symfony 中获得这个分数。我想知道 FOSElasticaBundle 是否提供类似于下面的函数来获取分数。
$finder = $this->container->get('fos_elastica.finder.app.article');
$boolQuery = new \Elastica\Query\BoolQuery();
$fieldQuery = new \Elastica\Query\Match();
$fieldQuery->setFieldQuery('title', 'I am a title string');
$fieldQuery->setFieldParam('title', 'analyzer', 'my_analyzer');
$boolQuery->addShould($fieldQuery);
解决方案
使用 FOSElasticaBundle 搜索时,您会得到一个Elastica\ResultSet
with Elastica\Result
inside。您可以迭代这些结果,它们有一种getScore
方法可以得到您需要的东西。
$resultSet = $this->store->search($query);
$results = $resultSet->getResults();
foreach ($results as $result) {
$score = $result->getScore();
}
或者,您可以通过以下方式获得分数:$result->getParam('_score');
推荐阅读
- python - Keras:如何使用 fit_generator 输入多个图像和一个输出?
- reactjs - 如何使用 framer-motion 和 reactjs 用 svg 线条错开孩子?
- javascript - 如何将 Async.js 并行转换为 Bluebird
- google-chrome - Chrome pdf 查看器在 5 - 10 秒后崩溃
- amazon-web-services - 连接 AWS EC2 实例时超时
- python - 扩展 'cogs.reddit' 引发错误:TypeError: __init__() 缺少 1 个必需的位置参数:'client',我该如何解决这个问题?
- angular - 从“createAction”函数获取类型
- flutter - 在 onChanged TextForm 字段值中将字符串更改为 int
- c# - 是否可以在列表视图单元格上禁用/设置单击效果的颜色?
- php - Laravel 8.x - 在 Cloudflare 和负载均衡器后面运行时出现 403 无效签名