在 Symfony 中获取 Elasticsearch 分数



Elasticsearch提供了一个分数字段,如果你通过cURL执行get请求。

{
"_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 ElasticaQueryBoolQuery();
$fieldQuery = new ElasticaQueryMatch();
$fieldQuery->setFieldQuery('title', 'I am a title string');
$fieldQuery->setFieldParam('title', 'analyzer', 'my_analyzer');
$boolQuery->addShould($fieldQuery);

使用 FOSElasticaBundle 搜索时,你会得到一个包含ElasticaResultElasticaResultSet。您可以迭代这些结果,它们有一种getScore方法来获得您需要的内容。

$resultSet = $this->store->search($query);
$results = $resultSet->getResults();
foreach ($results as $result) {
$score = $result->getScore();
}

或者,您可以通过以下方式获得分数:$result->getParam('_score');

如果尝试从扩展FOSElasticaBundleRepository的类中更改此设置,请考虑使用$this->findHybrid()。该方法返回一个包含HybridResult对象的数组。每个HybridResult依次包含转换后的实体和结果数据(包括分数(。

最新更新