diff --git a/src/Engines/Modes/Boolean.php b/src/Engines/Modes/Boolean.php index 6da0a16..ef5f5e6 100644 --- a/src/Engines/Modes/Boolean.php +++ b/src/Engines/Modes/Boolean.php @@ -20,6 +20,13 @@ public function buildWhereRawString(Builder $builder) return $queryString; } + public function buildSelectColumns(Builder $builder) + { + $indexFields = implode(',', $this->modelService->setModel($builder->model)->getFullTextIndexFields()); + + return "*, MATCH($indexFields) AGAINST(? IN NATURAL LANGUAGE MODE) AS relevance"; + } + public function buildParams(Builder $builder) { $this->whereParams[] = $builder->query; diff --git a/src/Engines/Modes/NaturalLanguage.php b/src/Engines/Modes/NaturalLanguage.php index 63a1ab8..adf8178 100644 --- a/src/Engines/Modes/NaturalLanguage.php +++ b/src/Engines/Modes/NaturalLanguage.php @@ -25,6 +25,13 @@ public function buildWhereRawString(Builder $builder) return $queryString; } + public function buildSelectColumns(Builder $builder) + { + $indexFields = implode(',', $this->modelService->setModel($builder->model)->getFullTextIndexFields()); + + return "*, MATCH($indexFields) AGAINST(? IN NATURAL LANGUAGE MODE) AS relevance"; + } + public function buildParams(Builder $builder) { $this->whereParams[] = $builder->query; diff --git a/src/Engines/MySQLEngine.php b/src/Engines/MySQLEngine.php index 91c22eb..fb0b12b 100644 --- a/src/Engines/MySQLEngine.php +++ b/src/Engines/MySQLEngine.php @@ -4,6 +4,7 @@ use Yab\MySQLScout\Engines\Modes\ModeContainer; use Illuminate\Database\Eloquent\Collection; +use Illuminate\Support\Facades\DB; use Laravel\Scout\Builder; use Laravel\Scout\Engines\Engine; @@ -65,6 +66,9 @@ public function search(Builder $builder) $model = $builder->model; $query = $model::whereRaw($whereRawString, $params); + if ($mode->isFullText()) { + $query = $query->selectRaw(DB::raw($mode->buildSelectColumns($builder)), $params); + } if($builder->callback){ $query = call_user_func($builder->callback, $query, $this);