Skip to content

Commit a2b0181

Browse files
committed
Search indexing must now be explicit
Removes use of callbacks to reindex of on create/update In production, this would typically only need to be done post-deploy since we’re indexing static content Touches indexed_at when Page is indexed
1 parent 6945735 commit a2b0181

File tree

6 files changed

+39
-27
lines changed

6 files changed

+39
-27
lines changed

app/models/page/searchable.rb

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@ module Searchable
66
ConfigurationError = Class.new(Error)
77

88
included do
9-
after_create_commit :create_in_search_index
10-
after_update_commit :update_in_search_index
119
after_destroy_commit :remove_from_search_index
1210

1311
scope :search, ->(query) do
@@ -57,6 +55,7 @@ def update_in_search_index
5755
transaction do
5856
remove_from_search_index
5957
create_in_search_index
58+
touch(:indexed_at)
6059
end
6160
end
6261

@@ -65,10 +64,6 @@ def remove_from_search_index
6564
end
6665

6766
class_methods do
68-
def refresh_search_index
69-
find_each(&:update_in_search_index)
70-
end
71-
7267
def create_search_index(page)
7368
id, title, body = [:id, :title, :body_text].map { |attr| page.send(attr) }
7469
Rails.logger.info "[#{self}] Creating search index: #{id} #{title}"

spec/factories/pages.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,17 @@
1818
FactoryBot.define do
1919
factory :page do
2020
request_path { "/" + Faker::Internet.slug }
21+
22+
trait :published do
23+
published_at { 1.day.ago }
24+
end
25+
26+
trait :unpublished do
27+
published_at { 1.day.from_now }
28+
end
29+
30+
trait :indexed do
31+
indexed_at { 1.day.ago }
32+
end
2133
end
2234
end

spec/jobs/pages/refresh_search_index_job_spec.rb

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,13 @@
22

33
RSpec.describe Pages::RefreshSearchIndexJob, type: :job do
44
it "doesn’t blow up" do
5-
Page.upsert_collection_from_sitepress!(limit: 2)
5+
page_1 = FactoryBot.create(:page, :published, request_path: "/articles/introducing-joy-of-rails")
6+
page_2 = FactoryBot.create(:page, :published, request_path: "/articles/custom-color-schemes-with-ruby-on-rails")
7+
page_3 = FactoryBot.create(:page, :unpublished, request_path: "/articles/joy-of-rails-2")
68

79
described_class.perform_now
10+
11+
expect(Page.join_search_index).to include(page_1, page_2)
12+
expect(Page.join_search_index).not_to include(page_3)
813
end
914
end

spec/models/page_spec.rb

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -24,34 +24,29 @@
2424
expect(page.resource).to eq Sitepress.site.get("/")
2525
end
2626

27-
describe ".refresh_search_index" do
28-
it "doesn’t blow up" do
29-
Page.find_or_create_by!(request_path: "/")
30-
31-
Page.refresh_search_index
32-
end
33-
end
34-
3527
describe ".search" do
3628
it "allows searching for pages" do
37-
page = Page.find_or_create_by!(request_path: "/")
29+
page = FactoryBot.create(:page, :published, request_path: "/")
30+
Pages::RefreshSearchIndexJob.perform_now
3831

3932
expect(Page.search("Joy of Rails")).to include page
4033
end
4134

4235
it "works after rebuilding index" do
43-
page = Page.find_or_create_by!(request_path: "/")
36+
page = FactoryBot.create(:page, :published, request_path: "/")
37+
Pages::RefreshSearchIndexJob.perform_now
4438

45-
Page.refresh_search_index
39+
Page.find_each(&:update_in_search_index)
4640

4741
expect(Page.search("Joy of Rails")).to include page
4842
end
4943
end
5044

5145
describe ".rank" do
5246
it "orders search results by rank" do
53-
page_1 = Page.find_or_create_by!(request_path: "/articles/custom-color-schemes-with-ruby-on-rails")
54-
page_2 = Page.find_or_create_by!(request_path: "/articles/mastering-custom-configuration-in-rails")
47+
page_1 = FactoryBot.create(:page, :published, request_path: "/articles/custom-color-schemes-with-ruby-on-rails")
48+
page_2 = FactoryBot.create(:page, :published, request_path: "/articles/mastering-custom-configuration-in-rails")
49+
Pages::RefreshSearchIndexJob.perform_now
5550

5651
search = Page.search("custom con*")
5752

@@ -61,7 +56,8 @@
6156

6257
describe ".with_snippets" do
6358
it "orders search results by rank" do
64-
Page.find_or_create_by!(request_path: "/articles/custom-color-schemes-with-ruby-on-rails")
59+
FactoryBot.create(:page, :published, request_path: "/articles/custom-color-schemes-with-ruby-on-rails")
60+
Pages::RefreshSearchIndexJob.perform_now
6561

6662
page = Page.search("Color*").with_snippets.first
6763

spec/requests/search_spec.rb

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,8 @@
3535
end
3636

3737
it "renders the search results without query" do
38-
Page.create!(request_path: "/pwa-showcase")
38+
FactoryBot.create(:page, :published, request_path: "/pwa-showcase")
39+
Pages::RefreshSearchIndexJob.perform_now
3940

4041
get search_path
4142

@@ -45,8 +46,9 @@
4546
end
4647

4748
it "renders the search results with query as turbo stream" do
48-
Page.create!(request_path: "/pwa-showcase")
49-
Page.create!(request_path: "/articles/introducing-joy-of-rails")
49+
FactoryBot.create(:page, :published, request_path: "/pwa-showcase")
50+
FactoryBot.create(:page, :published, request_path: "/articles/introducing-joy-of-rails")
51+
Pages::RefreshSearchIndexJob.perform_now
5052

5153
get search_path(format: :turbo_stream), params: {query: "Progressive Web Apps"}
5254

@@ -61,8 +63,9 @@
6163
end
6264

6365
it "renders the search results with query" do
64-
Page.create!(request_path: "/pwa-showcase")
65-
Page.create!(request_path: "/articles/introducing-joy-of-rails")
66+
FactoryBot.create(:page, :published, request_path: "/pwa-showcase")
67+
FactoryBot.create(:page, :published, request_path: "/articles/introducing-joy-of-rails")
68+
Pages::RefreshSearchIndexJob.perform_now
6669

6770
post search_path, params: {query: "Progressive Web Apps"}
6871

spec/system/searches_spec.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22

33
RSpec.describe "Searches", type: :system do
44
it "show search results" do
5-
Page.create!(request_path: "/pwa-showcase")
5+
FactoryBot.create(:page, :published, request_path: "/pwa-showcase")
6+
Pages::RefreshSearchIndexJob.perform_now
67

78
visit root_path
89

0 commit comments

Comments
 (0)