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

Fixes #292 #542

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
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1203,6 +1203,19 @@ shirt_brands = ShirtBrand.search_by_name("Penguin")
.group("shirt_brands.id, #{PgSearch::Configuration.alias('shirt_brands')}.rank")
```

#### Scoping search rank subqueries

It may be necessary to limit the scope of a rank subquery for performance reasons.
The subquery scope relation object is accessed by passing a block to the search
method.

```ruby
shirt_manufacturer = ShirtManufacturer.find_by(name: "Hypercolor Sportswear Co")
shirt_brands = ShirtBrand.search_by_name("Penguin") do |subquery_relation|
subquery_relation.where(shirt_manufacturer: shirt_manufacturer)
end.with_pg_search_rank
```

## ATTRIBUTIONS

PgSearch would not have been possible without inspiration from texticle (now renamed
Expand Down
4 changes: 2 additions & 2 deletions lib/pg_search/model.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ def pg_search_scope(name, options)
raise ArgumentError, "pg_search_scope expects a Hash or Proc"
end

define_singleton_method(name) do |*args|
define_singleton_method(name) do |*args, &block|
config = Configuration.new(options_proc.call(*args), self)
scope_options = ScopeOptions.new(config)
scope_options.apply(self)
scope_options.apply(self, &block)
end
end

Expand Down
12 changes: 7 additions & 5 deletions lib/pg_search/scope_options.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@ def initialize(config)
@feature_options = config.feature_options
end

def apply(scope)
def apply(scope, &block)
scope = include_table_aliasing_for_rank(scope)
rank_table_alias = scope.pg_search_rank_table_alias(include_counter: true)

scope
.joins(rank_join(rank_table_alias))
.joins(rank_join(rank_table_alias, &block))
.order(Arel.sql("#{rank_table_alias}.rank DESC, #{order_within_rank}"))
.extend(WithPgSearchRank)
.extend(WithPgSearchHighlight[feature_for(:tsearch)])
Expand Down Expand Up @@ -79,14 +79,16 @@ def increment_counter
delegate :connection, :quoted_table_name, to: :model

def subquery
model
relation = model
.unscoped
.select("#{primary_key} AS pg_search_id")
.select("#{rank} AS rank")
.joins(subquery_join)
.where(conditions)
.limit(nil)
.offset(nil)

block_given? ? yield(relation) : relation
end

def conditions
Expand Down Expand Up @@ -162,8 +164,8 @@ def rank
end
end

def rank_join(rank_table_alias)
"INNER JOIN (#{subquery.to_sql}) AS #{rank_table_alias} ON #{primary_key} = #{rank_table_alias}.pg_search_id"
def rank_join(rank_table_alias, &block)
"INNER JOIN (#{subquery(&block).to_sql}) AS #{rank_table_alias} ON #{primary_key} = #{rank_table_alias}.pg_search_id"
end

def include_table_aliasing_for_rank(scope)
Expand Down
14 changes: 14 additions & 0 deletions spec/integration/pg_search_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -435,6 +435,20 @@
expect(results).to eq([winner, loser])
end

it "is filterable in sub-select when sorted by rank" do
loser = ModelWithPgSearch.create!(content: "foo", parent_model_id: 1)
winner = ModelWithPgSearch.create!(content: "foo foo", parent_model_id: 1)
filtered = ModelWithPgSearch.create!(content: "foo foo foo", parent_model_id: 2)


results = ModelWithPgSearch.search_content("foo") do |subquery_relation|
subquery_relation.where(parent_model_id: 1)
end.with_pg_search_rank

expect(results).to_not include(filtered)
expect(results).to eq([winner, loser])
end

it "preserves column selection when with_pg_search_rank is chained after a select()" do
ModelWithPgSearch.create!(title: "foo", content: "bar")

Expand Down