Skip to content

Commit 6844679

Browse files
authored
1 parent 513015f commit 6844679

File tree

4 files changed

+47
-0
lines changed

4 files changed

+47
-0
lines changed

README.md

+8
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,14 @@ App\MyModel::search('*')
265265
->get();
266266
```
267267

268+
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):
269+
270+
```php
271+
App\MyModel::search('sales')
272+
->minScore(1.0)
273+
->get();
274+
```
275+
268276
At last, if you want to send a custom request, you can use the `searchRaw` method:
269277

270278
```php

src/Builders/FilterBuilder.php

+20
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,13 @@ class FilterBuilder extends Builder
4646
*/
4747
public $select = [];
4848

49+
/**
50+
* The min_score parameter.
51+
*
52+
* @var string
53+
*/
54+
public $minScore;
55+
4956
/**
5057
* FilterBuilder constructor.
5158
*
@@ -524,6 +531,19 @@ public function select($fields)
524531
return $this;
525532
}
526533

534+
/**
535+
* Set the min_score on the filter.
536+
*
537+
* @param float $score
538+
* @return $this
539+
*/
540+
public function minScore($score)
541+
{
542+
$this->minScore = $score;
543+
544+
return $this;
545+
}
546+
527547
/**
528548
* Get the count.
529549
*

src/ElasticEngine.php

+1
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,7 @@ public function buildSearchQueryPayloadCollection(Builder $builder, array $optio
138138
->setIfNotEmpty('body.sort', $builder->orders)
139139
->setIfNotEmpty('body.explain', $options['explain'] ?? null)
140140
->setIfNotEmpty('body.profile', $options['profile'] ?? null)
141+
->setIfNotEmpty('body.min_score', $builder->minScore)
141142
->setIfNotNull('body.from', $builder->offset)
142143
->setIfNotNull('body.size', $builder->limit);
143144

tests/Builders/FilterBuilderTest.php

+18
Original file line numberDiff line numberDiff line change
@@ -483,4 +483,22 @@ public function testOnlyTrashed()
483483
$builder->wheres
484484
);
485485
}
486+
487+
public function testMinScore()
488+
{
489+
$builder = (new FilterBuilder($this->mockModel()));
490+
491+
$this->assertSame(
492+
null,
493+
$builder->minScore
494+
);
495+
496+
$builder = (new FilterBuilder($this->mockModel()))
497+
->minScore(0.5);
498+
499+
$this->assertSame(
500+
0.5,
501+
$builder->minScore
502+
);
503+
}
486504
}

0 commit comments

Comments
 (0)