1

Postgres 允许使用pg_trgm 模块进行三元索引。

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

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

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

4

1 回答 1

6

根据这个单元测试,可以像这样添加索引:

add_index :contacts, :title, using: :gist, opclass: {title: :gist_trgm_ops}
于 2018-04-03T22:24:11.673 回答