Skip to content

Commit

Permalink
Add searchable_created_at field to pg_search_documents table
Browse files Browse the repository at this point in the history
Add an additional timestamp to pg_search_documents table to store the created_at of the page. This timestamp is interesting, if the documents are order or filtered by time.
  • Loading branch information
sascha-karnatz committed Nov 7, 2024
1 parent 21049ae commit 27e7f94
Show file tree
Hide file tree
Showing 4 changed files with 20 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
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

0 comments on commit 27e7f94

Please sign in to comment.