首页 > 解决方案 > 在 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);

标签: phpsymfonyelasticsearchelasticafoselasticabundle

解决方案


使用 FOSElasticaBundle 搜索时,您会得到一个Elastica\ResultSetwith Elastica\Resultinside。您可以迭代这些结果,它们有一种getScore方法可以得到您需要的东西。

$resultSet = $this->store->search($query);

$results = $resultSet->getResults();

foreach ($results as $result) {
    $score = $result->getScore();
}

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


推荐阅读