Skip to content

Commit

Permalink
Boost and $fields as arrays
Browse files Browse the repository at this point in the history
  • Loading branch information
pdphilip committed Sep 16, 2024
1 parent 8aa8e4b commit 64c3873
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
1 change: 1 addition & 0 deletions src/Eloquent/Docs/ModelDocs.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@
* @method static $this withHighlights(array $fields = [], string|array $preTag = '<em>', string|array $postTag = '</em>', array $globalOptions = [])
* @method static $this asFuzzy(?int $depth = null)
* @method static $this setMinShouldMatch(int $value)
* @method static $this setBoost(int $value)
*
* Query Executors --------------------------------------------
* @method static Model|null find($id)
Expand Down
29 changes: 28 additions & 1 deletion src/Query/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -648,7 +648,7 @@ public function searchFor($value, $columns = ['*'], $options = [], $boolean = 'a

public function searchTerm($term, $fields = ['*'], $options = [], $boolean = 'and'): static
{

$this->_ensureValueAsArray($fields);
$this->wheres[] = [
'column' => '*',
'type' => 'Search',
Expand All @@ -664,6 +664,7 @@ public function searchTerm($term, $fields = ['*'], $options = [], $boolean = 'an

public function searchTermMost($term, $fields = ['*'], $options = [], $boolean = 'and'): static
{
$this->_ensureValueAsArray($fields);
$this->wheres[] = [
'column' => '*',
'type' => 'Search',
Expand All @@ -679,6 +680,7 @@ public function searchTermMost($term, $fields = ['*'], $options = [], $boolean =

public function searchTermCross($term, $fields = ['*'], $options = [], $boolean = 'and'): static
{
$this->_ensureValueAsArray($fields);
$this->wheres[] = [
'column' => '*',
'type' => 'Search',
Expand All @@ -694,6 +696,7 @@ public function searchTermCross($term, $fields = ['*'], $options = [], $boolean

public function searchPhrase($phrase, $fields = ['*'], $options = [], $boolean = 'and'): static
{
$this->_ensureValueAsArray($fields);
$this->wheres[] = [
'column' => '*',
'type' => 'Search',
Expand All @@ -709,6 +712,7 @@ public function searchPhrase($phrase, $fields = ['*'], $options = [], $boolean =

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

public function searchBoolPrefix($phrase, $fields = ['*'], $options = [], $boolean = 'and'): static
{
$this->_ensureValueAsArray($fields);
$this->wheres[] = [
'column' => '*',
'type' => 'Search',
Expand Down Expand Up @@ -846,6 +851,21 @@ public function setMinShouldMatch(int $value): static
return $this;
}

public function setBoost(int $value): static
{
$wheres = $this->wheres;
if (! $wheres) {
throw new RuntimeException('No where clause found');
}
$lastWhere = end($wheres);
if ($lastWhere['type'] != 'Search') {
throw new RuntimeException('Min Should Match can only be applied to Search type queries');
}
$this->_attachOption('boost', $value);

return $this;
}

//----------------------------------------------------------------------
// Options
//----------------------------------------------------------------------
Expand Down Expand Up @@ -1781,6 +1801,13 @@ private function _formatTimestamp($value): string|int
}
}

private function _ensureValueAsArray(&$value): void
{
if (! is_array($value)) {
$value = [$value];
}
}

//----------------------------------------------------------------------
// Disabled Methods
//----------------------------------------------------------------------
Expand Down

0 comments on commit 64c3873

Please sign in to comment.