【问题标题】:Adding trigram index to Postgres table in Rails 5在 Rails 5 中向 Postgres 表添加三元组索引
【发布时间】:2018-04-03 22:12:56
【问题描述】:

Postgres 允许使用 pg_trgm module 的三元组索引。

这是他们在“索引支持”部分提供的示例代码:

CREATE TABLE test_trgm (t text);
CREATE INDEX trgm_idx ON test_trgm USING GIST (t gist_trgm_ops);

这是我想出的迁移:

class AddTitleTrigramIndexToContacts < ActiveRecord::Migration[5.1]
  def change
     enable_extension 'pg_trgm'
     execute "CREATE INDEX contacts_title_trigram_ix ON contacts USING GIST (title gist_trgm_ops);"
  end
end

有没有更好的方法来添加这个迁移?我什至不确定这是否正确。

【问题讨论】:

    标签: ruby-on-rails postgresql pg-trgm


    【解决方案1】:

    根据这个unit test可以这样添加索引:

    add_index :contacts, :title, using: :gist, opclass: {title: :gist_trgm_ops}
    

    【讨论】:

    • 注意:opclass 选项需要 Rails 5.2+
    • 注意:如果您在执行迁移添加时收到 PG::UndefinedObject: ERROR: data type character varying has no default operator class for access method "gist" 错误。 enable_extension 'btree_gist'add_index 方法之前
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-02
    • 1970-01-01
    • 2013-03-04
    • 2011-12-24
    • 1970-01-01
    • 2022-08-04
    相关资源
    最近更新 更多