Skip to content
This repository has been archived by the owner on Jan 9, 2024. It is now read-only.

Commit

Permalink
Add relevance attribute to Models if Mode is FullText compatible (NAT…
Browse files Browse the repository at this point in the history
…URAL or BOOLEAN)
  • Loading branch information
Aurélien MENDES authored and msonowal committed Sep 20, 2019
1 parent b64f3bc commit 3686b2d
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src/Engines/Modes/Boolean.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
7 changes: 7 additions & 0 deletions src/Engines/Modes/NaturalLanguage.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
4 changes: 4 additions & 0 deletions src/Engines/MySQLEngine.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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);

This comment has been minimized.

Copy link
@flipersclub

flipersclub Apr 22, 2020

using all the $params has broken ->where()

This comment has been minimized.

Copy link
@flipersclub

flipersclub Apr 22, 2020

I suggest using $params[0] to get only the param for the search

}

if($builder->callback){
$query = call_user_func($builder->callback, $query, $this);
Expand Down

0 comments on commit 3686b2d

Please sign in to comment.