From bcfb7f514613e13144d29b1e3915d71becacc340 Mon Sep 17 00:00:00 2001 From: Alex Willemsma Date: Wed, 12 Aug 2015 20:02:30 -0700 Subject: [PATCH 1/2] Add an option to disable ranking of search results entirely. --- lib/pg_search/configuration.rb | 2 +- lib/pg_search/scope_options.rb | 18 ++++++++++++++---- 2 files changed, 15 insertions(+), 5 deletions(-) diff --git a/lib/pg_search/configuration.rb b/lib/pg_search/configuration.rb index 36a3052a..17c78091 100644 --- a/lib/pg_search/configuration.rb +++ b/lib/pg_search/configuration.rb @@ -88,7 +88,7 @@ def default_options ].map(&:to_sym) VALID_VALUES = { - :ignoring => [:accents] + :ignoring => [:accents, :rank] } def assert_valid_options(options) diff --git a/lib/pg_search/scope_options.rb b/lib/pg_search/scope_options.rb index a8604182..abb6f91f 100644 --- a/lib/pg_search/scope_options.rb +++ b/lib/pg_search/scope_options.rb @@ -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 @@ -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 From 83d07aeb5dc4b1ad25babbbafaf16698cff58eaf Mon Sep 17 00:00:00 2001 From: Alex Willemsma Date: Mon, 17 Aug 2015 09:40:18 -0700 Subject: [PATCH 2/2] Add spec for option to disable ranking entirely. --- spec/integration/pg_search_spec.rb | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/spec/integration/pg_search_spec.rb b/spec/integration/pg_search_spec.rb index 1f3ad6ad..0657856a 100644 --- a/spec/integration/pg_search_spec.rb +++ b/spec/integration/pg_search_spec.rb @@ -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,