2012-11-02 18 views
8

我正在关注在Ryan Bates的excellent tutorial上使用Rails中的内置PostgresQL全文搜索。我目前使用pg_search gem un-indexed没有问题,但我需要提高性能。我正在使用指定了“english”字典的tsvector。无法运行rake db:使用Railscast示例在Rails中为Postgresql迁移tsvector GIN索引

我使用PostgreSQL 9.1.4版本

每瑞恩的指示,我已经运行这段代码指定了两个新的指标,我想创建一个新的迁移。下面是模式第一:

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,ActiveRecord会为你做引用。如果您希望在很多SQL片段中使用表名,那么我建议重命名该表,以便您不必关心引用问题。

相关问题