在Rails 5中为Postgres表添加trigram索引

时间:2018-04-03 22:12:56

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

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

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

1 个答案:

答案 0 :(得分:2)

根据此unit test,可以像这样添加索引:

string input = Console.ReadLine();
int parsed;
while (!int.TryParse(input, out parsed))
{
     Console.WriteLine("Please enter a number!");
     input = Console.ReadLine();
}

//do something with parsed