PHP and Elasticsearch include score/relevance in returned object

317 Views Asked by At

So I'm using PHP Symfony and the Ongr-Elasticsearch bundle, to query my documents and return matching objects. The results seem to be sorted by relevance/score, but the actual relevance/score isn't included in the objects themselves.

Is this possible to do? I'd like to include the score in what I send to the frontend to be able to do stuff with it.

Code:

$search = $this->esRepository->createSearch();
$search->setSize(30);
$queryFields = array(....);
$queryStringQuery = new QueryStringQuery($queryString, ["fields" => $queryFields]);
$search->addQuery($queryStringQuery);
$esResults = $this->esRepository->execute($search, Repository::RESULTS_ARRAY);
1

There are 1 best solutions below

0
On

When you are executing query with Repository::RESULTS_ARRAY it only returns _source from hits. What you want to use is Repository::RESULTS_RAW_ITERATOR ant it will return whole hit which will include _score.