Skip to content

Commit

Permalink
Merge pull request #1 from undergroundwebdesigns/feature/disable_rank
Browse files Browse the repository at this point in the history
Add option to disable ranking of results entirely when using pg_search_scope.
  • Loading branch information
sumeetjain authored Jul 11, 2017
2 parents 1382153 + 83d07ae commit 81d907e
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 5 deletions.
2 changes: 1 addition & 1 deletion lib/pg_search/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,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)
.extend(WithPgSearchHighlight[feature_for(:tsearch)])
Expand Down Expand Up @@ -95,6 +96,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 @@ -1132,6 +1132,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

0 comments on commit 81d907e

Please sign in to comment.