Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP and Elasticsearch include score/relevance in returned object

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);
like image 469
joakimnorberg Avatar asked Dec 15 '25 02:12

joakimnorberg


1 Answers

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.

like image 122
martiis Avatar answered Dec 16 '25 19:12

martiis



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!