From 0c945cb8362a946d3c18b8af4f76c38db63cf174 Mon Sep 17 00:00:00 2001 From: Darcy Laycock Date: Tue, 10 Apr 2018 18:03:52 +1000 Subject: [PATCH] Support a custom ts_rank function e.g. ts_rank_cd. --- lib/pg_search/features/tsearch.rb | 8 ++++++-- spec/lib/pg_search/features/tsearch_spec.rb | 16 ++++++++++++++++ 2 files changed, 22 insertions(+), 2 deletions(-) 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