Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support custom rank functions other than ts_rank #442

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 = 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
Expand Down