0

这是我的架构

          Column          |            Type            
--------------------------+----------------------------
 id                       | integer                    
 title                    | character varying(255)     
 summary                  | character varying(255)     
 readable_content         | text                       
 created_at               | timestamp without time zone
 updated_at               | timestamp without time zone
 textsearchable_index_col | tsvector                   

Indexes:
    "site_articles_pkey" PRIMARY KEY, btree (id)
    "index_site_articles_on_textsearchable_index_col" gin (textsearchable_index_col)
Triggers:
    site_articles_before_insert_update_row_tr BEFORE INSERT OR UPDATE ON site_articles FOR EACH ROW EXECUTE PROCEDURE site_articles_before_insert_update_row_tr()

这是触发功能:

CREATE FUNCTION site_articles_before_insert_update_row_tr() RETURNS trigger
    LANGUAGE plpgsql
    AS $$
BEGIN
    new.tsv := tsvector_update_trigger(textsearchable_index_col, 'pg_catalog.simple', title, summary, readable_content);
    new.tsv := setweight(to_tsvector('pg_catalog.simple', coalesce(new.title,'')), 'A') ||
                setweight(to_tsvector('pg_catalog.simple', coalesce(new.summary,'')), 'B') ||
                setweight(to_tsvector('pg_catalog.simple', coalesce(new.readable_content,'')), 'C');
    RETURN NEW;
END;
$$;

但是,当我更新这样的记录时:

UPDATE "site_articles" SET "updated_at" = '2013-12-13 05:43:59.802580' WHERE "site_articles"."id" = 1

我明白了

ERROR:  column "textsearchable_index_col" does not exist
LINE 1: SELECT tsvector_update_trigger(textsearchable_index_col, 'pg...
                                       ^
QUERY:  SELECT tsvector_update_trigger(textsearchable_index_col, 'pg_catalog.simple', title, summary, readable_content)

我很确定列名是正确的。不知道这是否重要,我在添加 tsvector 列后连接这样的行(我正在使用 Rails 迁移)

def up
  add_column :site_articles, :textsearchable_index_col, :tsvector

  sql = <<-SQL
    UPDATE site_articles SET textsearchable_index_col =
                   to_tsvector('simple', coalesce("site_articles"."title"::text,'')
                               || ' ' || coalesce("site_articles"."summary"::text, '')
                               || ' ' || coalesce("site_articles"."readable_content"::text, '')
                              );
  SQL
  execute sql

  add_index :site_articles, :textsearchable_index_col, using: 'gin'
end

我是否遗漏了某些内容,或者每一列都应该有自己的 tsvector 列(没有连接在一起)?

4

1 回答 1

0

Indeed. It should be new.textsearchable_index_col. The same for subsequent fields.

(Btw, there's little point in assigning tsv twice. Either use your first (corrected) statement, or your second. But not both, since the second overrides the first, and a tsv is expensive to compute.)

于 2013-12-12T23:38:13.530 回答