Last active
January 17, 2018 06:24
-
-
Save Insolita/b00ef3e97190ded167db9c6a80f980ce to your computer and use it in GitHub Desktop.
Полнотекстовый поиск postgres+yii2 - миграция
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
use yii\db\Migration; | |
class m170105_004305_add_fts extends Migration | |
{ | |
public function safeUp() | |
{ | |
/* | |
* PREPARE SEARCH CONFIGURATION | |
*---------------------------- | |
*/ | |
$this->getDb()->createCommand( | |
' | |
CREATE TEXT SEARCH DICTIONARY ispell_ru ( | |
template = ispell, | |
dictfile = ru, | |
afffile = ru, | |
stopwords = russian | |
); | |
' | |
)->execute(); | |
$this->getDb()->createCommand( | |
' | |
CREATE TEXT SEARCH DICTIONARY ispell_en ( | |
template = ispell, | |
dictfile = en, | |
afffile = en, | |
stopwords = english | |
); | |
' | |
)->execute(); | |
$this->getDb()->createCommand('CREATE TEXT SEARCH CONFIGURATION ru ( COPY = russian );')->execute(); | |
$this->getDb()->createCommand( | |
'ALTER TEXT SEARCH CONFIGURATION ru | |
ALTER MAPPING | |
FOR word, hword, hword_part | |
WITH ispell_ru, russian_stem; | |
' | |
)->execute(); | |
$this->getDb()->createCommand( | |
'ALTER TEXT SEARCH CONFIGURATION ru | |
ALTER MAPPING | |
FOR asciiword, asciihword, hword_asciipart | |
WITH ispell_en, english_stem;' | |
)->execute(); | |
$this->getDb()->createCommand('SET default_text_search_config = \'ru\';')->execute(); | |
/** ADD tsvector column **/ | |
$this->getDb()->createCommand( | |
' | |
ALTER TABLE {{%tovar}} ADD COLUMN fts tsvector; | |
' | |
)->execute(); | |
$this->getDb()->createCommand( | |
' | |
UPDATE {{%tovar}} SET fts= | |
setweight( coalesce( to_tsvector(\'ru\', [[name]]),\'\'),\'A\') || \' \' || | |
setweight( coalesce( to_tsvector(\'ru\', [[description]]),\'\'),\'B\') || \' \'; | |
' | |
)->execute(); | |
$this->getDb()->createCommand('create index fts_index on {{%tovar}} using gin (fts);')->execute(); | |
/** | |
* --- ADD AUTO FILL fts TRIGGER ON INSERT NEW RECORD | |
* (in my case 'on update' trigger not neccessary) | |
**/ | |
$this->getDb()->createCommand( | |
' | |
CREATE FUNCTION fts_vector_update() RETURNS TRIGGER AS $$ | |
BEGIN | |
NEW.fts=setweight( coalesce( to_tsvector(\'ru\', NEW.name),\'\'),\'A\') || \' \' || | |
setweight( coalesce( to_tsvector(\'ru\', NEW.description),\'\'),\'B\') || \' \'; | |
RETURN NEW; | |
END; | |
$$ LANGUAGE \'plpgsql\'; | |
CREATE TRIGGER tovar_fts_update BEFORE INSERT ON {{%tovar}} | |
FOR EACH ROW EXECUTE PROCEDURE fts_vector_update(); | |
' | |
)->execute(); | |
} | |
public function safeDown() | |
{ | |
$this->dropIndex('fts_index', '{{%tovar}}'); | |
$this->dropColumn('{{%tovar}}', 'fts'); | |
$this->getDb()->createCommand('DROP TRIGGER tovar_fts_update ON {{%tovar}}')->execute(); | |
$this->getDb()->createCommand('DROP FUNCTION IF EXISTS fts_vector_update()')->execute(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment