From 50c38b48ee50e76ca908dc89beb2b96d6b8dd9db Mon Sep 17 00:00:00 2001 From: Michael Deck Date: Thu, 25 Mar 2021 13:53:17 -0400 Subject: [PATCH 1/4] Add _score to the returned model --- src/Builders/FilterBuilder.php | 20 ++++++++++++++++++++ src/ElasticEngine.php | 8 +++++++- src/Searchable.php | 9 ++++++++- 3 files changed, 35 insertions(+), 2 deletions(-) diff --git a/src/Builders/FilterBuilder.php b/src/Builders/FilterBuilder.php index 5f47fa53..442b2ef3 100644 --- a/src/Builders/FilterBuilder.php +++ b/src/Builders/FilterBuilder.php @@ -53,6 +53,13 @@ class FilterBuilder extends Builder */ public $minScore; + /** + * Determines if the score should be returned with the model + * + * @var boolean - false + */ + public $withScores = false; + /** * FilterBuilder constructor. * @@ -593,6 +600,19 @@ public function minScore($score) return $this; } + /** + * Set the withScores property. + * + * @param boolean $score - true + * @return $this + */ + public function withScores($withScores = true) + { + $this->withScores = $withScores; + + return $this; + } + /** * Get the count. * diff --git a/src/ElasticEngine.php b/src/ElasticEngine.php index 4cd2af2c..9da84679 100755 --- a/src/ElasticEngine.php +++ b/src/ElasticEngine.php @@ -306,6 +306,8 @@ public function map(Builder $builder, $results, $model) $columns[] = $scoutKeyName; } + $withScores = $builder->withScores; + $ids = $this->mapIds($results)->all(); $query = $model::usesSoftDelete() ? $model->withTrashed() : $model->newQuery(); @@ -319,12 +321,16 @@ public function map(Builder $builder, $results, $model) ->keyBy($scoutKeyName); $values = Collection::make($results['hits']['hits']) - ->map(function ($hit) use ($models) { + ->map(function ($hit) use ($models, $withScores) { $id = $hit['_id']; if (isset($models[$id])) { $model = $models[$id]; + if($withScores && isset($hit['_score'])) { + $model->_score = $hit['_score']; + } + if (isset($hit['highlight'])) { $model->highlight = new Highlight($hit['highlight']); } diff --git a/src/Searchable.php b/src/Searchable.php index bd5da477..4d07957e 100644 --- a/src/Searchable.php +++ b/src/Searchable.php @@ -15,12 +15,19 @@ trait Searchable } /** - * The highligths. + * The highlights. * * @var \ScoutElastic\Highlight|null */ private $highlight = null; + /** + * The score returned from elasticsearch + * + * @var float|null + */ + public $_score; + /** * Get the index configurator. * From 9ba24baf1da9f48eaabcd9022c3fbc7ec5fa31de Mon Sep 17 00:00:00 2001 From: Michael Deck Date: Thu, 25 Mar 2021 14:08:43 -0400 Subject: [PATCH 2/4] Add withScores and _score to documentation --- README.md | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/README.md b/README.md index 91589cfb..d953bf5b 100644 --- a/README.md +++ b/README.md @@ -294,6 +294,17 @@ $model = App\MyModel::search('sales') $model->sortPayload; ``` +And return the score assigned by ElasticSearch with the models: + +```php +$model = App\MyModel::search('sales') + ->withScores() + ->get(); + +// To retrieve the score, use model \`_score\` attribute: +$model->_score; +``` + At last, if you want to send a custom request, you can use the `searchRaw` method: From 2991b42c5ef3a29631f6991fbbf9c3d77da6716e Mon Sep 17 00:00:00 2001 From: Michael Deck Date: Thu, 25 Mar 2021 14:13:56 -0400 Subject: [PATCH 3/4] StyleCI changes --- src/Builders/FilterBuilder.php | 4 ++-- src/ElasticEngine.php | 2 +- src/Searchable.php | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/Builders/FilterBuilder.php b/src/Builders/FilterBuilder.php index 442b2ef3..881e51fc 100644 --- a/src/Builders/FilterBuilder.php +++ b/src/Builders/FilterBuilder.php @@ -54,7 +54,7 @@ class FilterBuilder extends Builder public $minScore; /** - * Determines if the score should be returned with the model + * Determines if the score should be returned with the model. * * @var boolean - false */ @@ -603,7 +603,7 @@ public function minScore($score) /** * Set the withScores property. * - * @param boolean $score - true + * @param bool $withScores - true * @return $this */ public function withScores($withScores = true) diff --git a/src/ElasticEngine.php b/src/ElasticEngine.php index 9da84679..c38cbc55 100755 --- a/src/ElasticEngine.php +++ b/src/ElasticEngine.php @@ -327,7 +327,7 @@ public function map(Builder $builder, $results, $model) if (isset($models[$id])) { $model = $models[$id]; - if($withScores && isset($hit['_score'])) { + if ($withScores && isset($hit['_score'])) { $model->_score = $hit['_score']; } diff --git a/src/Searchable.php b/src/Searchable.php index 4d07957e..ec448a02 100644 --- a/src/Searchable.php +++ b/src/Searchable.php @@ -22,7 +22,7 @@ trait Searchable private $highlight = null; /** - * The score returned from elasticsearch + * The score returned from elasticsearch. * * @var float|null */ From 6b2d8102fc792d9d4caeaedceb116ccd2bbf0e68 Mon Sep 17 00:00:00 2001 From: Michael Deck Date: Thu, 25 Mar 2021 14:15:13 -0400 Subject: [PATCH 4/4] StyleCI changes --- src/Builders/FilterBuilder.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Builders/FilterBuilder.php b/src/Builders/FilterBuilder.php index 881e51fc..e385730e 100644 --- a/src/Builders/FilterBuilder.php +++ b/src/Builders/FilterBuilder.php @@ -56,7 +56,7 @@ class FilterBuilder extends Builder /** * Determines if the score should be returned with the model. * - * @var boolean - false + * @var bool - false */ public $withScores = false;