Skip to content

Commit 8c1f6ce

Browse files
tpetrytaylorotwell
andauthored
[8.x] Fulltext index for PostgreSQL (#39875)
* fulltext index support for PostgreSQL * use fluent language declarationgs Co-authored-by: Taylor Otwell <[email protected]>
1 parent c51ad0f commit 8c1f6ce

File tree

2 files changed

+68
-0
lines changed

2 files changed

+68
-0
lines changed

src/Illuminate/Database/Schema/Grammars/PostgresGrammar.php

+38
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Illuminate\Database\Schema\Blueprint;
66
use Illuminate\Support\Fluent;
7+
use RuntimeException;
78

89
class PostgresGrammar extends Grammar
910
{
@@ -176,6 +177,31 @@ public function compileIndex(Blueprint $blueprint, Fluent $command)
176177
);
177178
}
178179

180+
/**
181+
* Compile a fulltext index key command.
182+
*
183+
* @param \Illuminate\Database\Schema\Blueprint $blueprint
184+
* @param \Illuminate\Support\Fluent $command
185+
* @return string
186+
*
187+
* @throws \RuntimeException
188+
*/
189+
public function compileFulltext(Blueprint $blueprint, Fluent $command)
190+
{
191+
$language = $command->language ?: 'english';
192+
193+
if (count($command->columns) > 1) {
194+
throw new RuntimeException('The PostgreSQL driver does not support fulltext index creation using multiple columns.');
195+
}
196+
197+
return sprintf('create index %s on %s using gin (to_tsvector(%s, %s))',
198+
$this->wrap($command->index),
199+
$this->wrapTable($blueprint),
200+
$this->quoteString($language),
201+
$this->wrap($command->columns[0])
202+
);
203+
}
204+
179205
/**
180206
* Compile a spatial index key command.
181207
*
@@ -359,6 +385,18 @@ public function compileDropIndex(Blueprint $blueprint, Fluent $command)
359385
return "drop index {$this->wrap($command->index)}";
360386
}
361387

388+
/**
389+
* Compile a drop fulltext index command.
390+
*
391+
* @param \Illuminate\Database\Schema\Blueprint $blueprint
392+
* @param \Illuminate\Support\Fluent $command
393+
* @return string
394+
*/
395+
public function compileDropFulltext(Blueprint $blueprint, Fluent $command)
396+
{
397+
return $this->compileDropIndex($blueprint, $command);
398+
}
399+
362400
/**
363401
* Compile a drop spatial index command.
364402
*

tests/Database/DatabasePostgresSchemaGrammarTest.php

+30
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,36 @@ public function testAddingIndexWithAlgorithm()
262262
$this->assertSame('create index "baz" on "users" using hash ("foo", "bar")', $statements[0]);
263263
}
264264

265+
public function testAddingFulltextIndex()
266+
{
267+
$blueprint = new Blueprint('users');
268+
$blueprint->fulltext('body');
269+
$statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
270+
271+
$this->assertCount(1, $statements);
272+
$this->assertSame('create index "users_body_fulltext" on "users" using gin (to_tsvector(\'english\', "body"))', $statements[0]);
273+
}
274+
275+
public function testAddingFulltextIndexWithLanguage()
276+
{
277+
$blueprint = new Blueprint('users');
278+
$blueprint->fulltext('body')->language('spanish');
279+
$statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
280+
281+
$this->assertCount(1, $statements);
282+
$this->assertSame('create index "users_body_fulltext" on "users" using gin (to_tsvector(\'spanish\', "body"))', $statements[0]);
283+
}
284+
285+
public function testAddingFulltextIndexWithFluency()
286+
{
287+
$blueprint = new Blueprint('users');
288+
$blueprint->string('body')->fulltext();
289+
$statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
290+
291+
$this->assertCount(2, $statements);
292+
$this->assertSame('create index "users_body_fulltext" on "users" using gin (to_tsvector(\'english\', "body"))', $statements[1]);
293+
}
294+
265295
public function testAddingSpatialIndex()
266296
{
267297
$blueprint = new Blueprint('geo');

0 commit comments

Comments
 (0)