Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[8.x] Expand Full-Text Search capabilities #40583

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions src/Illuminate/Database/Query/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,23 @@ public function selectRaw($expression, array $bindings = [])
return $this;
}

/**
* @param string|string[] $columns
* @param string $value
* @param string $as
* @return $this
*/
public function selectFullText($columns, $value, $as, array $options = [])
{
$columns = (array) $columns;

$query = $this->grammar->compileFullText($columns, $value, $options);

return $this->selectRaw(
'('.$query.') as '.$this->grammar->wrap($as), [$value]
);
}

/**
* Makes "from" fetch from a subquery.
*
Expand Down Expand Up @@ -415,6 +432,31 @@ public function addSelect($column)
return $this;
}

/**
* Add a fulltext select to the query.
*
* @param string|string[] $columns
* @param string $value
* @param string $as
* @return $this
*/
public function addSelectFullText($columns, $value, $as, array $options = [])
{
if (is_null($this->columns)) {
$this->select($this->from.'.*');
}

$columns = (array) $columns;

$query = $this->grammar->compileFullText($columns, $value, $options);

$this->selectSub($query, $as);

$this->addBinding([$value], 'select');

return $this;
}

/**
* Force the query to only return distinct results.
*
Expand Down
15 changes: 15 additions & 0 deletions src/Illuminate/Database/Query/Grammars/Grammar.php
Original file line number Diff line number Diff line change
Expand Up @@ -667,6 +667,21 @@ public function whereFullText(Builder $query, $where)
throw new RuntimeException('This database engine does not support fulltext search operations.');
}

/**
* Compile a "fulltext" clause.
*
* @param array $columns
* @param string $value
* @param array $options
* @return string
*
* @throws \RuntimeException
*/
public function compileFullText($columns, $value, $options)
{
throw new RuntimeException('This database engine does not support fulltext search operations.');
}

/**
* Compile the "group by" portions of the query.
*
Expand Down
23 changes: 18 additions & 5 deletions src/Illuminate/Database/Query/Grammars/MySqlGrammar.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,23 +51,36 @@ protected function whereNotNull(Builder $query, $where)
}

/**
* Compile a "where fulltext" clause.
* Add a "where fulltext" clause to the query.
*
* @param \Illuminate\Database\Query\Builder $query
* @param array $where
* @return string
*/
public function whereFullText(Builder $query, $where)
{
$columns = $this->columnize($where['columns']);
return $this->compileFullText($where['columns'], $where['value'], $where['options']);
}

/**
* Compile a "fulltext" clause.
*
* @param array $columns
* @param string $value
* @param array $options
* @return string
*/
public function compileFullText($columns, $value, $options)
{
$columns = $this->columnize($columns);

$value = $this->parameter($where['value']);
$value = $this->parameter($value);

$mode = ($where['options']['mode'] ?? []) === 'boolean'
$mode = ($options['mode'] ?? []) === 'boolean'
? ' in boolean mode'
: ' in natural language mode';

$expanded = ($where['options']['expanded'] ?? []) && ($where['options']['mode'] ?? []) !== 'boolean'
$expanded = ($options['expanded'] ?? []) && ($options['mode'] ?? []) !== 'boolean'
? ' with query expansion'
: '';

Expand Down