From 68446794dd4ea00a2c1a1216d1c53a63ee29c493 Mon Sep 17 00:00:00 2001 From: hoersten <2041942+hoersten@users.noreply.github.com> Date: Fri, 27 Mar 2020 14:14:26 -0500 Subject: [PATCH] Added support for [min_score](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-body.html#request-body-search-min-score). Requested in #122 and #101. (#350) --- README.md | 8 ++++++++ src/Builders/FilterBuilder.php | 20 ++++++++++++++++++++ src/ElasticEngine.php | 1 + tests/Builders/FilterBuilderTest.php | 18 ++++++++++++++++++ 4 files changed, 47 insertions(+) diff --git a/README.md b/README.md index 8fea9d8..cdb00fb 100644 --- a/README.md +++ b/README.md @@ -265,6 +265,14 @@ App\MyModel::search('*') ->get(); ``` +And filter out results with a score less than [min_score](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-body.html#request-body-search-min-score): + +```php +App\MyModel::search('sales') + ->minScore(1.0) + ->get(); +``` + At last, if you want to send a custom request, you can use the `searchRaw` method: ```php diff --git a/src/Builders/FilterBuilder.php b/src/Builders/FilterBuilder.php index 5b006f1..78f3e93 100644 --- a/src/Builders/FilterBuilder.php +++ b/src/Builders/FilterBuilder.php @@ -46,6 +46,13 @@ class FilterBuilder extends Builder */ public $select = []; + /** + * The min_score parameter. + * + * @var string + */ + public $minScore; + /** * FilterBuilder constructor. * @@ -524,6 +531,19 @@ public function select($fields) return $this; } + /** + * Set the min_score on the filter. + * + * @param float $score + * @return $this + */ + public function minScore($score) + { + $this->minScore = $score; + + return $this; + } + /** * Get the count. * diff --git a/src/ElasticEngine.php b/src/ElasticEngine.php index 8f0e3b6..a5e0788 100644 --- a/src/ElasticEngine.php +++ b/src/ElasticEngine.php @@ -138,6 +138,7 @@ public function buildSearchQueryPayloadCollection(Builder $builder, array $optio ->setIfNotEmpty('body.sort', $builder->orders) ->setIfNotEmpty('body.explain', $options['explain'] ?? null) ->setIfNotEmpty('body.profile', $options['profile'] ?? null) + ->setIfNotEmpty('body.min_score', $builder->minScore) ->setIfNotNull('body.from', $builder->offset) ->setIfNotNull('body.size', $builder->limit); diff --git a/tests/Builders/FilterBuilderTest.php b/tests/Builders/FilterBuilderTest.php index bfde7b0..6feb6cf 100644 --- a/tests/Builders/FilterBuilderTest.php +++ b/tests/Builders/FilterBuilderTest.php @@ -483,4 +483,22 @@ public function testOnlyTrashed() $builder->wheres ); } + + public function testMinScore() + { + $builder = (new FilterBuilder($this->mockModel())); + + $this->assertSame( + null, + $builder->minScore + ); + + $builder = (new FilterBuilder($this->mockModel())) + ->minScore(0.5); + + $this->assertSame( + 0.5, + $builder->minScore + ); + } }