Permalink
Browse files

Add :rank_columns option to allow sort by rank only by specific field

  • Loading branch information...
1 parent 8d7e039 commit 17e304c63d02edb0276f8987e4d7faca94be01b0 @strikeroff strikeroff committed Aug 8, 2012
Showing with 10 additions and 2 deletions.
  1. +2 −0 README.md
  2. +7 −1 lib/pg_search_scope/model_helper.rb
  3. +1 −1 lib/pg_search_scope/version.rb
View
@@ -38,6 +38,8 @@ You can set additional search options:
:rank_function - Ranking function. Valid values are 'ts_rank' and 'ts_rank_cd'
+ :rank_columns - If you want to sort table by rank only by specific fields - input column names hear
@jlinder

jlinder Aug 8, 2012

'hear' should be spelled 'here'.

@strikeroff

strikeroff Aug 8, 2012

Owner

thanks. will fix

+
If you use `:language` option, you need to use the same option for `add_fulltext_index`
Examples:
@@ -33,6 +33,7 @@ module ModelHelper
#
# * <tt>:language</tt> - Search language, e.g. 'simple' (without magic), 'english'
# * <tt>:rank_function</tt> - Ranking function. Valid values are 'ts_rank' and 'ts_rank_cd'
+ # * <tt>:rank_columns</tt> - If you want to sort table by rank only by specific fields - input column names hear
#
# == Usage
#
@@ -70,8 +71,13 @@ def search_scope_for *column_names
tsvector = "to_tsvector('#{options[:language]}', #{document})"
tsquery = "to_tsquery('#{options[:language]}', '#{terms.join(" #{OPERATORS[options[:operator]]} ")}')"
+ rank_tsvector = tsvector
+ if options[:rank_columns].present?
+ rank_document = options[:rank_columns].map { |n| n = "#{prefix}.#{n}" unless n['.']; "coalesce(#{n}, '')" }.join(" || ' ' || ")
+ rank_tsvector = "to_tsvector('#{options[:language]}', #{rank_document})"
+ end
- rank = "#{scope_options[:rank_function]}(#{tsvector}, #{tsquery}, #{options[:normalization]})"
+ rank = "#{scope_options[:rank_function]}(#{rank_tsvector}, #{tsquery}, #{options[:normalization]})"
search_scope = scoped
@@ -1,3 +1,3 @@
module PgSearchScope
- VERSION = "0.1.6"
+ VERSION = "0.1.7"
end

0 comments on commit 17e304c

Please sign in to comment.