From d8a9b9b408666c281961ec948cb7615335b1411e Mon Sep 17 00:00:00 2001 From: Cassio Godinho Date: Thu, 27 Apr 2017 16:06:52 -0300 Subject: [PATCH] Add support for unaccent extension --- README.md | 18 ++++++++++++++++-- src/PostgresEngine.php | 6 +++++- 2 files changed, 21 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 5b84ba0..414f9f2 100644 --- a/README.md +++ b/README.md @@ -88,6 +88,7 @@ return [ 'connection' => 'pgsql', 'maintain_index' => true, 'config' => 'english', + 'unaccent' => false, ], ]; ``` @@ -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 ], ... ``` @@ -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. @@ -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'); diff --git a/src/PostgresEngine.php b/src/PostgresEngine.php index a6c1a44..17c8a51 100644 --- a/src/PostgresEngine.php +++ b/src/PostgresEngine.php @@ -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)