diff --git a/lib/pg_search/features/tsearch.rb b/lib/pg_search/features/tsearch.rb index aeee19ed..d336521d 100644 --- a/lib/pg_search/features/tsearch.rb +++ b/lib/pg_search/features/tsearch.rb @@ -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 @@ -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 diff --git a/spec/lib/pg_search/features/tsearch_spec.rb b/spec/lib/pg_search/features/tsearch_spec.rb index 5af7610e..4d0a3726 100644 --- a/spec/lib/pg_search/features/tsearch_spec.rb +++ b/spec/lib/pg_search/features/tsearch_spec.rb @@ -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 = instance_double("PgSearch::Configuration", :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