Skip to content

Commit

Permalink
Merge pull request #59 from sascha-karnatz/add-searchable-created-at
Browse files Browse the repository at this point in the history
Add searchable_created_at field
  • Loading branch information
tvdeyen authored Nov 7, 2024
2 parents 21049ae + d2f4327 commit 98942ef
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 2 deletions.
2 changes: 1 addition & 1 deletion app/extensions/alchemy/pg_search/page_extension.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ def self.prepended(base)
:meta_keywords,
:name,
],
additional_attributes: ->(page) { { page_id: page.id } },
additional_attributes: ->(page) { { page_id: page.id, searchable_created_at: page.published_at } },
if: :searchable?,
)
end
Expand Down
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
class AddDocumentCreatedAtToPgSearchDocuments < ActiveRecord::Migration[7.1]
def change
add_column :pg_search_documents, :searchable_created_at, :datetime, if_not_exists: true
add_index :pg_search_documents, :searchable_created_at, if_not_exists: true
end
end
4 changes: 3 additions & 1 deletion spec/dummy/db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema[7.1].define(version: 2024_10_08_083843) do
ActiveRecord::Schema[7.1].define(version: 2024_11_06_130317) do
# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"

Expand Down Expand Up @@ -256,7 +256,9 @@
t.datetime "updated_at", null: false
t.bigint "page_id"
t.virtual "searchable_content", type: :tsvector, as: "to_tsvector('simple'::regconfig, COALESCE(content, ''::text))", stored: true
t.datetime "searchable_created_at"
t.index ["page_id"], name: "index_pg_search_documents_on_page_id"
t.index ["searchable_created_at"], name: "index_pg_search_documents_on_searchable_created_at"
t.index ["searchable_type", "searchable_id"], name: "index_pg_search_documents_on_searchable"
end

Expand Down
10 changes: 10 additions & 0 deletions spec/models/page_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -69,4 +69,14 @@
end
end
end

describe "additional_attributes" do
it "stores page_id" do
expect(page.pg_search_document.page_id).to eq(page.id)
end

it "stores searchable created_at" do
expect(page.pg_search_document.searchable_created_at).to eq(page.published_at)
end
end
end
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 98942ef

Please sign in to comment.