Skip to content

Commit

Permalink
Support a custom ts_rank function e.g. ts_rank_cd.
Browse files Browse the repository at this point in the history
  • Loading branch information
Sutto committed Jun 6, 2020
1 parent a85490c commit 5fa8206
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 2 deletions.
8 changes: 6 additions & 2 deletions lib/pg_search/features/tsearch.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ module PgSearch
module Features
class TSearch < Feature # rubocop:disable Metrics/ClassLength
def self.valid_options
super + %i[dictionary prefix negation any_word normalization tsvector_column highlight]
super + %i[dictionary prefix negation any_word normalization tsvector_column ts_rank_function highlight]
end

def conditions
Expand Down Expand Up @@ -168,8 +168,12 @@ def normalization
options[:normalization] || 0
end

def ts_rank_function
options[:ts_rank_function] || "ts_rank"
end

def tsearch_rank
Arel::Nodes::NamedFunction.new("ts_rank", [
Arel::Nodes::NamedFunction.new(ts_rank_function, [
arel_wrap(tsdocument),
arel_wrap(tsquery),
normalization
Expand Down
16 changes: 16 additions & 0 deletions spec/lib/pg_search/features/tsearch_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,22 @@
%{(ts_rank((to_tsvector('simple', coalesce(#{Model.quoted_table_name}."name"::text, '')) || to_tsvector('simple', coalesce(#{Model.quoted_table_name}."content"::text, ''))), (to_tsquery('simple', ''' ' || 'query' || ' ''')), 0))}
)
end

it "supports an expression using the ts_rank_cd() function" do
query = "query"
columns = [
PgSearch::Configuration::Column.new(:name, nil, Model),
PgSearch::Configuration::Column.new(:content, nil, Model)
]
options = {ts_rank_function: 'ts_rank_cd'}
config = double(:config, :ignore => [])
normalizer = PgSearch::Normalizer.new(config)

feature = described_class.new(query, options, columns, Model, normalizer)
expect(feature.rank.to_sql).to eq(
%{(ts_rank_cd((to_tsvector('simple', coalesce(#{Model.quoted_table_name}."name"::text, '')) || to_tsvector('simple', coalesce(#{Model.quoted_table_name}."content"::text, ''))), (to_tsquery('simple', ''' ' || 'query' || ' ''')), 0))}
)
end
end

describe "#conditions" do
Expand Down

0 comments on commit 5fa8206

Please sign in to comment.