Skip to content

Add support for unaccent extension #11

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
18 changes: 16 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ return [
'connection' => 'pgsql',
'maintain_index' => true,
'config' => 'english',
'unaccent' => false,
],
];
```
Expand Down Expand Up @@ -119,6 +120,9 @@ Specify the database connection that should be used to access indexed documents
// You can explicitly specify what PostgreSQL text search config to use by scout.
// Use \dF in psql to see all available configurations in your database.
'config' => 'english',
// See configuration instructions bellow
// For more information see https://www.postgresql.org/docs/current/static/unaccent.html
'unaccent' => false
],
...
```
Expand All @@ -133,6 +137,16 @@ To check the current value
SHOW default_text_search_config;
```

If the `unaccent`option is `true` you will need to create the extension.
```php
// On the up() function of a migration
DB::statement('CREATE EXTENSION unaccent');
// And on the down function
DB::statement('DROP EXTENSION IF EXISTS unaccent');
```

This options

### Prepare the Schema

By default the engine expects that parsed documents (model data) are stored in the same table as the Model in a column `searchable` of type `tsvector`. You'd need to create this column and an index in your schema. You can choose between `GIN` and `GiST` indexes in PostgreSQL.
Expand All @@ -149,13 +163,13 @@ class CreatePostsTable extends Migration
$table->integer('user_id');
$table->timestamps();
});

DB::statement('ALTER TABLE posts ADD searchable tsvector NULL');
DB::statement('CREATE INDEX posts_searchable_index ON posts USING GIN (searchable)');
// Or alternatively
// DB::statement('CREATE INDEX posts_searchable_index ON posts USING GIST (searchable)');
}

public function down()
{
Schema::drop('posts');
Expand Down
6 changes: 5 additions & 1 deletion src/PostgresEngine.php
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,11 @@ protected function toVector(Model $model)
// by the selected text search configuration which can be set globally in config/scout.php
// file or individually for each model in searchableOptions()
// See https://www.postgresql.org/docs/current/static/textsearch-controls.html
$vector = 'to_tsvector(COALESCE(?, get_current_ts_config()), ?)';
if ($this->config('unaccent') === true) {
$vector = 'to_tsvector(COALESCE(?, get_current_ts_config()), unaccent(?))';
} else {
$vector = 'to_tsvector(COALESCE(?, get_current_ts_config()), ?)';
}

$select = $fields->map(function ($value, $key) use ($model, $vector, $bindings) {
$bindings->push($this->searchConfig($model) ?: null)
Expand Down