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
# 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
-
Rails 4 (github.com/rails/rails)
-
PostgreSQL
Test environments
-
ruby-2.1 (work)
-
ruby-1.9.2 (work)
Maintainers
-
Team Phatworx (github.com/phatworx)
-
Marco Scholl (github.com/traxanos)
Contributing to phat_pgsearch
-
Check out the latest master to make sure the feature hasn’t been implemented or the bug hasn’t been fixed yet
-
Check out the issue tracker to make sure someone already hasn’t requested it and/or contributed it
-
Fork the project
-
Start a feature/bugfix branch
-
Commit and push until you are happy with your contribution
-
Make sure to add tests for it. This is important so I don’t break it in a future version unintentionally.
-
Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
Copyright
Copyright © 2011 Marco Scholl. See LICENSE.txt for further details.