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

Add option to disable ranking of results entirely when using pg_search_scope. #259

Open
wants to merge 2 commits 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
2 changes: 1 addition & 1 deletion lib/pg_search/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ def default_options
].map(&:to_sym)

VALID_VALUES = {
:ignoring => [:accents]
:ignoring => [:accents, :rank]
}

def assert_valid_options(options)
Expand Down
18 changes: 14 additions & 4 deletions lib/pg_search/scope_options.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,13 @@ def initialize(config)
end

def apply(scope)
scope = include_table_aliasing_for_rank(scope)
rank_table_alias = scope.pg_search_rank_table_alias(:include_counter)
if @config.ignore.include? :rank
scope = scope.where(conditions)
else
scope = apply_ranked(scope)
end

scope
.joins(rank_join(rank_table_alias))
.order("#{rank_table_alias}.rank DESC, #{order_within_rank}")
.extend(DisableEagerLoading)
.extend(WithPgSearchRank)
end
Expand Down Expand Up @@ -67,6 +68,15 @@ def pg_search_scope_application_count_plus_plus

delegate :connection, :quoted_table_name, :to => :model

def apply_ranked(scope)
scope = include_table_aliasing_for_rank(scope)
rank_table_alias = scope.pg_search_rank_table_alias(:include_counter)

scope
.joins(rank_join(rank_table_alias))
.order("#{rank_table_alias}.rank DESC, #{order_within_rank}")
end

def subquery
model
.unscoped
Expand Down
13 changes: 13 additions & 0 deletions spec/integration/pg_search_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1097,6 +1097,19 @@
end
end

context "ignoring rank" do
before do
ModelWithPgSearch.pg_search_scope :search_title_without_rank,
:against => :title,
:ignoring => :rank
end

it "does not use ts_rank in the query" do
sql = ModelWithPgSearch.search_title_without_rank("foobar").to_sql
expect(sql).to_not include "ts_rank"
end
end

context "when passed a :ranked_by expression" do
before do
ModelWithPgSearch.pg_search_scope :search_content_with_default_rank,
Expand Down