Skip to content

Commit 64c3873

Browse files
committed
Boost and $fields as arrays
1 parent 8aa8e4b commit 64c3873

File tree

2 files changed

+29
-1
lines changed

2 files changed

+29
-1
lines changed

src/Eloquent/Docs/ModelDocs.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@
6565
* @method static $this withHighlights(array $fields = [], string|array $preTag = '<em>', string|array $postTag = '</em>', array $globalOptions = [])
6666
* @method static $this asFuzzy(?int $depth = null)
6767
* @method static $this setMinShouldMatch(int $value)
68+
* @method static $this setBoost(int $value)
6869
*
6970
* Query Executors --------------------------------------------
7071
* @method static Model|null find($id)

src/Query/Builder.php

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -648,7 +648,7 @@ public function searchFor($value, $columns = ['*'], $options = [], $boolean = 'a
648648

649649
public function searchTerm($term, $fields = ['*'], $options = [], $boolean = 'and'): static
650650
{
651-
651+
$this->_ensureValueAsArray($fields);
652652
$this->wheres[] = [
653653
'column' => '*',
654654
'type' => 'Search',
@@ -664,6 +664,7 @@ public function searchTerm($term, $fields = ['*'], $options = [], $boolean = 'an
664664

665665
public function searchTermMost($term, $fields = ['*'], $options = [], $boolean = 'and'): static
666666
{
667+
$this->_ensureValueAsArray($fields);
667668
$this->wheres[] = [
668669
'column' => '*',
669670
'type' => 'Search',
@@ -679,6 +680,7 @@ public function searchTermMost($term, $fields = ['*'], $options = [], $boolean =
679680

680681
public function searchTermCross($term, $fields = ['*'], $options = [], $boolean = 'and'): static
681682
{
683+
$this->_ensureValueAsArray($fields);
682684
$this->wheres[] = [
683685
'column' => '*',
684686
'type' => 'Search',
@@ -694,6 +696,7 @@ public function searchTermCross($term, $fields = ['*'], $options = [], $boolean
694696

695697
public function searchPhrase($phrase, $fields = ['*'], $options = [], $boolean = 'and'): static
696698
{
699+
$this->_ensureValueAsArray($fields);
697700
$this->wheres[] = [
698701
'column' => '*',
699702
'type' => 'Search',
@@ -709,6 +712,7 @@ public function searchPhrase($phrase, $fields = ['*'], $options = [], $boolean =
709712

710713
public function searchPhrasePrefix($phrase, $fields = ['*'], $options = [], $boolean = 'and'): static
711714
{
715+
$this->_ensureValueAsArray($fields);
712716
$this->wheres[] = [
713717
'column' => '*',
714718
'type' => 'Search',
@@ -724,6 +728,7 @@ public function searchPhrasePrefix($phrase, $fields = ['*'], $options = [], $boo
724728

725729
public function searchBoolPrefix($phrase, $fields = ['*'], $options = [], $boolean = 'and'): static
726730
{
731+
$this->_ensureValueAsArray($fields);
727732
$this->wheres[] = [
728733
'column' => '*',
729734
'type' => 'Search',
@@ -846,6 +851,21 @@ public function setMinShouldMatch(int $value): static
846851
return $this;
847852
}
848853

854+
public function setBoost(int $value): static
855+
{
856+
$wheres = $this->wheres;
857+
if (! $wheres) {
858+
throw new RuntimeException('No where clause found');
859+
}
860+
$lastWhere = end($wheres);
861+
if ($lastWhere['type'] != 'Search') {
862+
throw new RuntimeException('Min Should Match can only be applied to Search type queries');
863+
}
864+
$this->_attachOption('boost', $value);
865+
866+
return $this;
867+
}
868+
849869
//----------------------------------------------------------------------
850870
// Options
851871
//----------------------------------------------------------------------
@@ -1781,6 +1801,13 @@ private function _formatTimestamp($value): string|int
17811801
}
17821802
}
17831803

1804+
private function _ensureValueAsArray(&$value): void
1805+
{
1806+
if (! is_array($value)) {
1807+
$value = [$value];
1808+
}
1809+
}
1810+
17841811
//----------------------------------------------------------------------
17851812
// Disabled Methods
17861813
//----------------------------------------------------------------------

0 commit comments

Comments
 (0)