BartlomiejSkwira
6/25/2013 - 1:46 PM

PG_Search - add tsvector support for full text search

PG_Search - add tsvector support for full text search

Task.all.each { |m| m.send :update_tsvector; }
class Model < ActiveRecord::Base

  pg_search_scope :search_tours, against: :name,
    using: {
      tsearch: {
        dictionary: 'public.polish',
        prefix: true,
        tsvector_column: :tours_tsvector
      }
    },
    ignoring: :accents
  
  after_save :update_tsvector
  
  def update_tsvector
    query = "update tours set tours_tsvector = to_tsvector('public.polish', '#{self.name.gsub("'", "''")}' ) where id = #{self.id};"
    ActiveRecord::Base.connection.execute("select to_tsvector('public.polish', '#{self.name}')")
  end
end
class AddTsvectorColumnAndIndex < ActiveRecord::Migration
  def up
    add_column :tasks, :tsvector_name_tsearch, :tsvector
    execute %q{CREATE INDEX tsvector_name_tsearch_idx ON tasks USING GIN(TO_TSVECTOR('english', name));}
  end

  def down
    remove_column :tasks, :tsvector_name_tsearch
    execute %q{DROP INDEX tsvector_name_tsearch_idx;}
  end
end
#apt-get install postgresql-contrib-9.1
#login to psql console as admin and run
CREATE EXTENSION IF NOT EXISTS unaccent;