Skip to content

Commit

Permalink
No extra inner join for rank (Casecommons#2)
Browse files Browse the repository at this point in the history
* Fix the spec `where rank > 0.7` by different logic
  • Loading branch information
LeonidMorozov authored and larsburgess committed Oct 7, 2016
1 parent 8656e69 commit 466c509
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 16 deletions.
29 changes: 14 additions & 15 deletions lib/pg_search/scope_options.rb
Original file line number Diff line number Diff line change
Expand Up @@ -60,17 +60,26 @@ def with_pg_search_rank
scope = scope.select("#{table_name}.*") unless scope.select_values.any?
scope.select("#{pg_search_rank_table_alias}.rank AS pg_search_rank")
end

def where_pg_search_rank(value)
scope = self
scope.where("#{PgSearch::Configuration.alias(table_name)}.rank#{value}")
end
end

module WithPgSearchRankNoInnerJoin
def self.[](rank_field)
def self.[](rank_value)
Module.new do
include WithPgSearchRankNoInnerJoin
define_method(:rank_field) { rank_field }
define_method(:rank_value) { rank_value }
end
end

def rank_field
"#{rank_value} AS pg_search_rank"
end

def rank_value
raise TypeError.new("You need to instantiate this module with []")
end

Expand All @@ -79,12 +88,10 @@ def with_pg_search_rank
scope = scope.select("#{table_name}.*") unless scope.select_values.any?
scope.select(rank_field)
end
end

module WithNoInnerJoin
def with_no_inner_join
def where_pg_search_rank(value)
scope = self
scope.select("#{table_name}.*") unless scope.select_values.any?
scope.where("#{rank_value}#{value}")
end
end

Expand Down Expand Up @@ -133,7 +140,7 @@ def apply_without_inner_join(scope)
.where(conditions)
.order("#{rank_order}, #{order_within_rank}")
.extend(DisableEagerLoading)
.extend(WithPgSearchRankNoInnerJoin[rank_field])
.extend(WithPgSearchRankNoInnerJoin[rank])
.extend(WithPgSearchHighlight[feature_for(:tsearch)])
end

Expand Down Expand Up @@ -168,10 +175,6 @@ def order_within_rank
config.order_within_rank || "#{primary_key} ASC"
end

def has_associations?
config.associations.any?
end

def primary_key
"#{quoted_table_name}.#{connection.quote_column_name(model.primary_key)}"
end
Expand Down Expand Up @@ -217,10 +220,6 @@ def rank_join(rank_table_alias)
"INNER JOIN (#{subquery.to_sql}) AS #{rank_table_alias} ON #{primary_key} = #{rank_table_alias}.pg_search_id"
end

def rank_field
"#{rank} AS pg_search_rank"
end

def rank_order
"#{rank} DESC"
end
Expand Down
2 changes: 1 addition & 1 deletion spec/integration/pg_search_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -344,7 +344,7 @@
twice = ModelWithPgSearch.create!(:content => 'foo foo')

records = ModelWithPgSearch.search_content('foo')
.where("#{PgSearch::Configuration.alias(ModelWithPgSearch.table_name)}.rank > 0.07")
.where_pg_search_rank(' > 0.07')

expect(records).to eq [twice]
end
Expand Down

0 comments on commit 466c509

Please sign in to comment.