Skip to content

Commit 359ee8f

Browse files
committed
Support a custom ts_rank function e.g. ts_rank_cd.
1 parent a85490c commit 359ee8f

2 files changed

Lines changed: 22 additions & 2 deletions

File tree

lib/pg_search/features/tsearch.rb

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ module PgSearch
77
module Features
88
class TSearch < Feature # rubocop:disable Metrics/ClassLength
99
def self.valid_options
10-
super + %i[dictionary prefix negation any_word normalization tsvector_column highlight]
10+
super + %i[dictionary prefix negation any_word normalization tsvector_column ts_rank_function highlight]
1111
end
1212

1313
def conditions
@@ -168,8 +168,12 @@ def normalization
168168
options[:normalization] || 0
169169
end
170170

171+
def ts_rank_function
172+
options[:ts_rank_function] || "ts_rank"
173+
end
174+
171175
def tsearch_rank
172-
Arel::Nodes::NamedFunction.new("ts_rank", [
176+
Arel::Nodes::NamedFunction.new(ts_rank_function, [
173177
arel_wrap(tsdocument),
174178
arel_wrap(tsquery),
175179
normalization

spec/lib/pg_search/features/tsearch_spec.rb

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,22 @@
2727
%{(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))}
2828
)
2929
end
30+
31+
it "supports an expression using the ts_rank_cd() function" do
32+
query = "query"
33+
columns = [
34+
PgSearch::Configuration::Column.new(:name, nil, Model),
35+
PgSearch::Configuration::Column.new(:content, nil, Model)
36+
]
37+
options = { ts_rank_function: 'ts_rank_cd' }
38+
config = instance_double("PgSearch::Configuration", :config, ignore: [])
39+
normalizer = PgSearch::Normalizer.new(config)
40+
41+
feature = described_class.new(query, options, columns, Model, normalizer)
42+
expect(feature.rank.to_sql).to eq(
43+
%{(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))}
44+
)
45+
end
3046
end
3147

3248
describe "#conditions" do

0 commit comments

Comments
 (0)