【问题标题】:Get Elasticsearch Score in Symfony在 Symfony 中获取 Elasticsearch 分数
【发布时间】:2018-11-08 03:11:54
【问题描述】:

如果您通过 cURL 进行 get 请求,Elasticsearch 会提供一个分数字段。

{
  "_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);

【问题讨论】:

    标签: php symfony elasticsearch elastica foselasticabundle


    【解决方案1】:

    使用 FOSElasticaBundle 搜索时,您会得到一个 Elastica\ResultSet,其中包含 Elastica\Result。您可以对这些结果进行迭代,它们有一个 getScore 方法来获得您所需要的。

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

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

    【讨论】:

      【解决方案2】:

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

      【讨论】:

        猜你喜欢
        • 2019-07-01
        • 2015-04-30
        • 1970-01-01
        • 2020-03-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-07-30
        • 2016-03-29
        相关资源
        最近更新 更多