Skip to content
This repository has been archived by the owner on Nov 4, 2021. It is now read-only.

Commit

Permalink
Added support for [min_score](https://www.elastic.co/guide/en/elastic…
Browse files Browse the repository at this point in the history
  • Loading branch information
hoersten authored Mar 27, 2020
1 parent 513015f commit 6844679
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 0 deletions.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
20 changes: 20 additions & 0 deletions src/Builders/FilterBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,13 @@ class FilterBuilder extends Builder
*/
public $select = [];

/**
* The min_score parameter.
*
* @var string
*/
public $minScore;

/**
* FilterBuilder constructor.
*
Expand Down Expand Up @@ -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.
*
Expand Down
1 change: 1 addition & 0 deletions src/ElasticEngine.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
18 changes: 18 additions & 0 deletions tests/Builders/FilterBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
);
}
}

0 comments on commit 6844679

Please sign in to comment.