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 ranking order to set higher priorities over rank #513

Open
wants to merge 1 commit 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
6 changes: 5 additions & 1 deletion lib/pg_search/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,10 @@ def order_within_rank
options[:order_within_rank]
end

def ranking_order
options[:ranking_order]
end

private

attr_reader :options
Expand All @@ -84,7 +88,7 @@ def default_options
end

VALID_KEYS = %w[
against ranked_by ignoring using query associated_against order_within_rank
against ranked_by ignoring using query associated_against order_within_rank ranking_order
].map(&:to_sym)

VALID_VALUES = {
Expand Down
16 changes: 13 additions & 3 deletions lib/pg_search/scope_options.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ def apply(scope)

scope
.joins(rank_join(rank_table_alias))
.order(Arel.sql("#{rank_table_alias}.rank DESC, #{order_within_rank}"))
.order(order_clause(rank_table_alias))
.extend(WithPgSearchRank)
.extend(WithPgSearchHighlight[feature_for(:tsearch)])
end
Expand Down Expand Up @@ -96,8 +96,18 @@ def conditions
.inject { |accumulator, expression| Arel::Nodes::Or.new(accumulator, expression) }
end

def order_within_rank
config.order_within_rank || "#{primary_key} ASC"
def order_clause(rank_table_alias)
pre_order = if (cfg = config.ranking_order).is_a?(Hash)
cfg.map { |field, od| "#{field} #{od}" }.join(", ")
else
cfg
end

rank_order = "#{rank_table_alias}.rank DESC"

post_order = config.order_within_rank || "#{primary_key} ASC"

Arel.sql([pre_order, rank_order, post_order].compact.join(", "))
end

def primary_key
Expand Down
40 changes: 40 additions & 0 deletions spec/integration/pg_search_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -756,6 +756,46 @@
end
end

describe "ranking with custom order" do
before do
{"Strip Down" => 1, "Down" => 2, "Down and Out" => 3, "Won't Let You Down" => 2}.each do |name, importance|
ModelWithPgSearch.create! content: name, importance: importance
end
end

context "with ranking_order importance DESC" do
before do
ModelWithPgSearch.pg_search_scope :search_content_with_ranking_order,
against: :content,
using: :tsearch,
ranking_order: "importance DESC"
end

it "returns results sorted by importance before rank" do
results = ModelWithPgSearch.search_content_with_ranking_order("down")

expect(results.map(&:content)).to eq(["Down and Out", "Down", "Won't Let You Down", "Strip Down"])
expect(results.map(&:importance)).to eq([3, 2, 2, 1])
end
end

context "with ranking_order importance DESC, id DESC" do
before do
ModelWithPgSearch.pg_search_scope :search_content_with_ranking_order,
against: :content,
using: :tsearch,
ranking_order: {importance: :desc, id: :desc}
end

it "returns results sorted by importance before rank" do
results = ModelWithPgSearch.search_content_with_ranking_order("down")

expect(results.map(&:content)).to eq(["Down and Out", "Won't Let You Down", "Down", "Strip Down"])
expect(results.map(&:importance)).to eq([3, 2, 2, 1])
end
end
end

context "when against columns ranked with arrays" do
before do
ModelWithPgSearch.pg_search_scope :search_weighted_by_array_of_arrays,
Expand Down