Table "public.texts" Column | Type | Modifiers -------------+-----------------------------+---------------------------------------------------------- txt_id | bigint | not null default nextval(('gen_txt_id'::text)::regclass) user_id | integer | not null txt | text | not null fti_txt | tsvector | last_update | timestamp without time zone | default now() format | textformat | default 'wiki'::textformat Indexes: "texts_pkey" PRIMARY KEY, btree (txt_id) "texts_txt_id_key" UNIQUE CONSTRAINT, btree (txt_id) "fti_texts_idx" gist (fti_txt) "last_update_idx" btree (last_update) "texts_uid_idx" btree (user_id)
<?php class SearchAutocompleteAction extends CAction { public $model; public $attribute; public $fts_field; public function run() { // $_uid = Yii::app()->user->id; $_model = new $this->model; $_tableName = $_model->tableName(); // , // $_query_array = explode(' ', trim(Yii::app()->db->quoteValue($_GET['term']), " '\t\n\r\0\x0B")); $_word = array_pop($_query_array); $_preQuery = implode(' ', $_query_array); $_suggestions = array(); /* * tsvector . * , ( ). */ $_sub_sql = "SELECT $this->fts_field FROM $_tableName WHERE user_id=''$_uid''"; if (count($_query_array) > 0) $_sub_sql .= " AND $this->fts_field @@ to_tsquery(''russian'', ''$_preQuery'')"; /* * , , . * ts_stat tsearch2. , , * . ndoc, * , . */ $_sql = "SELECT word AS $this->attribute FROM ts_stat('$_sub_sql') WHERE word LIKE '$_word%' ORDER BY nentry DESC LIMIT 15;"; foreach(Yii::app()->db->createCommand($_sql)->query() as $_m) $_suggestions[] = count($_query_array) > 0 ? $_preQuery.' '.$_m[$this->attribute] : $_m[$this->attribute]; echo CJSON::encode($_suggestions); } }
SELECT word AS txt FROM ts_stat('SELECT fti_txt FROM texts WHERE user_id=''1'' AND fti_txt @@ to_tsquery(''russian'', '''')') WHERE word LIKE '%' ORDER BY nentry DESC LIMIT 15;
public function actions() { return array( ... 'acsearch' => array( 'class' => 'application.extensions.actions.SearchAutocompleteAction', 'model' => 'Texts', 'attribute' => 'txt', 'fts_field' => 'fti_txt', ), ... ); }
<?php echo CHtml::textField('sh', $search->sh, array('size' => 60,'maxlength' => 255)); ?>
<?php $this->widget('zii.widgets.jui.CJuiAutoComplete', array( 'attribute'=> 'sh', 'sourceUrl' => array('acsearch'), 'name' => 'sh', 'value' => $search->sh, 'options' => array( 'minLength' => '2', ), 'htmlOptions' => array( 'size' => 60, 'maxlength' => 255, ), )); ?>
Source: https://habr.com/ru/post/307858/
All Articles