2012-11-02 4 views
8

Railsで組み込みのPostgresQL全文検索を使用する場合は、Ryan Batesのexcellent tutorialをフォローしています。私は現在、pg_search gem un-indexedを問題なく使用していますが、パフォーマンスを向上させる必要があります。私は "英語"辞書を指定したtsvectorを使用しています。私はライアンの指示に従ってPostgreSQLのバージョン9.1.4rake dbを実行できません:Railscastサンプルを使用して、RailsでPostgreSQLのtsvector GINインデックスを移行してください

を使用してい

は、私が作成したい二つの新しいインデックスを指定して、このコードで新しい移行を実行しています。ここで最初のスキーマは次のとおりです。

create_table "references", :force => true do |t| 
    t.string "title" 
    t.string "type" 
    t.datetime "created_at",   :null => false 
    t.datetime "updated_at",   :null => false 
    t.string "public_url" 
    t.string "content_type" 
    t.integer "file_size" 
    t.text  "overview" 
    t.text  "body" 
    t.text  "full_text" 
    t.integer "folder_id" 
end 

私の移行は次のようになります。

def up 
    execute "create index references_title on references using gin(to_tsvector('english', title))" 
    execute "create index references_full_text on references using gin(to_tsvector('english', full_text))" 
end 

def down 
    execute "drop index references_title" 
    execute "drop index references_full_text" 
end 

は、私も先に行ってとコメント解除している:私は続けるapplication.rb

config.active_record.schema_format = :sql 

でSQLオプションを同じレイクを打ち切るエラー:

== AddSearchIndexesToReferences: migrating =================================== 
-- execute("CREATE INDEX references_title on references using gin(to_tsvector('english', title))") 
rake aborted! 
An error has occurred, this and all later migrations canceled: 

PG::Error: ERROR: syntax error at or near "references" 
LINE 1: CREATE INDEX references_title on references using gin(to_tsv... 
            ^
: CREATE INDEX references_title on references using gin(to_tsvector('english', title)) 

答えて

7

参照するようにあなたは、二重引用符にあなたない限り、テーブル名としてそれを使用することはできませんで使用するキーワードです:

def up 
    execute %q{create index references_title on "references" using gin(to_tsvector('english', title))} 
    execute %q{create index references_full_text on "references" using gin(to_tsvector('english', full_text))} 
end 

あなたはまた、どこでもあなたはSQLでそれを使用する引用そのテーブル名を倍増する必要がありますスニペット。 ActiveRecordはSQLを構築している場合には、あなたのために引用を行います。多くのSQLスニペットでテーブル名を使用する予定がある場合は、テーブルの名前を変更して引用問題を気にする必要がないようにすることをお勧めします。

関連する問題