phat_pgsearch

a plugin for postgresql tssearch support

Installation

add to Gemfile

gem "phat_pgsearch"

Configuration

PhatPgsearch.setup do |config|
  # default catalog
  # config.catalog = :english
end

Using

create table columns

create_table :sample_headers, :force => true do |t|
  t.string :title
  t.tsvector :tsv
end
create_table :sample_items, :force => true do |t|
  t.integer :sample_header_id
  t.text :content
  t.tsvector :tsv
  t.tsvector :tsv_full
end
create_table :sample_comments, :force => true do |t|
  t.integer :sample_item_id
  t.text :comment
  t.tsvector :tsv
end

add_index :sample_headers, :tsv, using: :gin
add_index :sample_comments, :tsv, using: :gist

define index

class SampleHeader < ActiveRecord::Base
  has_many :sample_items

  pgsearch_index :tsv do
    field :title, :weight => :a
  end

end
class SampleItem < ActiveRecord::Base
  has_many :sample_comments
  belongs_to :sample_header

  pgsearch_index :tsv, :catalog => :english do
    field :content
  end

  pgsearch_index :tsv_full, :catalog => :german do
    field :title, :weight => :a
    field :content, :weight => :b
    field :comment_texts, :weight => :d
  end

  def title
    sample_header.title
  end

  def comment_texts
    sample_comments.collect{ |comment| comment.comment }.join(' ')
  end

end

class SampleComment < ActiveRecord::Base
  belongs_to :sample_item
end
# search on field :tsv_full
SampleItem.pgsearch(:tsv_full, 'test test2')

# search on field :tsv with custom catalog
SampleItem.pgsearch(:tsv, 'test test2', :catalog => :english)

# disable auto sorting and use own select and sorting
SampleItem.pgsearch(:tsv_full, 'test test2', :rank => false).
  select("sample_items.*, ts_rank_cd('german', #{SampleItem.pgsearch_query(:tsv_full, 'test test2')}, 32) AS rank").
  order(:rank)

Requirements

Test environments

Maintainers

Contributing to phat_pgsearch

Copyright © 2011 Marco Scholl. See LICENSE.txt for further details.