Skip to content

Commit

Permalink
Add sorting parameter to perform_search method
Browse files Browse the repository at this point in the history
add date sort to perform_search method to use searchable_created_at date instead of the default pg_search rank sorting.
  • Loading branch information
sascha-karnatz committed Nov 7, 2024
1 parent 27e7f94 commit d2f4327
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
6 changes: 6 additions & 0 deletions app/services/alchemy/search/search_page.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@ module SearchPage
def self.perform_search(params, ability: nil)
search_results = Alchemy.search_class.search(params[:query], ability:)
search_results = search_results&.page(params[:page])&.per(paginate_per) if paginate_per.present?

# order the documents by searchable_created_at and use the ranking order as second order argument
if params[:sort] == "date"
search_results.order_values.unshift("pg_search_documents.searchable_created_at DESC")
end

search_results
end

Expand Down
23 changes: 23 additions & 0 deletions spec/services/alchemy/search/search_page_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,29 @@
end
end
end

context "sort" do
let(:query) { "page" }
let(:params) { {query:, sort:} }
let!(:first_page) { create(:alchemy_page, :public, published_at: "1999-08-01 00:00") }
let!(:second_page) { create(:alchemy_page, :public, title: "Page 2") }

context "by relevance" do
let(:sort) { "relevance" }

it "sorts by pg_search ranking" do
expect(subject.first.searchable).to eq(first_page)
end
end

context "by date" do
let(:sort) { "date" }

it "sorts by searchable_created_at" do
expect(subject.first.searchable).to eq(second_page)
end
end
end
end

context '#paginate_per' do
Expand Down

0 comments on commit d2f4327

Please sign in to comment.