diff --git a/src/Eloquent/Docs/ModelDocs.php b/src/Eloquent/Docs/ModelDocs.php index 2be7bbf..1439451 100644 --- a/src/Eloquent/Docs/ModelDocs.php +++ b/src/Eloquent/Docs/ModelDocs.php @@ -65,6 +65,7 @@ * @method static $this withHighlights(array $fields = [], string|array $preTag = '', string|array $postTag = '', 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) diff --git a/src/Query/Builder.php b/src/Query/Builder.php index 34fe324..30c68ee 100644 --- a/src/Query/Builder.php +++ b/src/Query/Builder.php @@ -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', @@ -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', @@ -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', @@ -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', @@ -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', @@ -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', @@ -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 //---------------------------------------------------------------------- @@ -1781,6 +1801,13 @@ private function _formatTimestamp($value): string|int } } + private function _ensureValueAsArray(&$value): void + { + if (! is_array($value)) { + $value = [$value]; + } + } + //---------------------------------------------------------------------- // Disabled Methods //----------------------------------------------------------------------