Skip to content

Commit

Permalink
DEV: Add more granularity to SearchIndexer versions.
Browse files Browse the repository at this point in the history
Sometimes, we just want to reindex a specific model and not all the
things.
  • Loading branch information
tgxworld committed Jul 23, 2020
1 parent 4b05346 commit 609ba50
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 18 deletions.
10 changes: 5 additions & 5 deletions app/jobs/scheduled/reindex_search.rb
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ def clean_topic_search_data
def load_problem_post_ids(limit)
params = {
locale: SiteSetting.default_locale,
version: SearchIndexer::INDEX_VERSION,
version: SearchIndexer::POST_INDEX_VERSION,
limit: limit
}

Expand All @@ -165,7 +165,7 @@ def load_problem_post_ids(limit)
def load_problem_category_ids(limit)
Category.joins(:category_search_data)
.where('category_search_data.locale != ?
OR category_search_data.version != ?', SiteSetting.default_locale, SearchIndexer::INDEX_VERSION)
OR category_search_data.version != ?', SiteSetting.default_locale, SearchIndexer::CATEGORY_INDEX_VERSION)
.order('categories.id asc')
.limit(limit)
.pluck(:id)
Expand All @@ -174,7 +174,7 @@ def load_problem_category_ids(limit)
def load_problem_topic_ids(limit)
Topic.joins(:topic_search_data)
.where('topic_search_data.locale != ?
OR topic_search_data.version != ?', SiteSetting.default_locale, SearchIndexer::INDEX_VERSION)
OR topic_search_data.version != ?', SiteSetting.default_locale, SearchIndexer::TOPIC_INDEX_VERSION)
.order('topics.id desc')
.limit(limit)
.pluck(:id)
Expand All @@ -183,7 +183,7 @@ def load_problem_topic_ids(limit)
def load_problem_user_ids(limit)
User.joins(:user_search_data)
.where('user_search_data.locale != ?
OR user_search_data.version != ?', SiteSetting.default_locale, SearchIndexer::INDEX_VERSION)
OR user_search_data.version != ?', SiteSetting.default_locale, SearchIndexer::USER_INDEX_VERSION)
.order('users.id asc')
.limit(limit)
.pluck(:id)
Expand All @@ -192,7 +192,7 @@ def load_problem_user_ids(limit)
def load_problem_tag_ids(limit)
Tag.joins(:tag_search_data)
.where('tag_search_data.locale != ?
OR tag_search_data.version != ?', SiteSetting.default_locale, SearchIndexer::INDEX_VERSION)
OR tag_search_data.version != ?', SiteSetting.default_locale, SearchIndexer::TAG_INDEX_VERSION)
.order('tags.id asc')
.limit(limit)
.pluck(:id)
Expand Down
8 changes: 6 additions & 2 deletions app/services/search_indexer.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
# frozen_string_literal: true

class SearchIndexer
INDEX_VERSION = 3
POST_INDEX_VERSION = 3
TOPIC_INDEX_VERSION = 3
CATEGORY_INDEX_VERSION = 3
USER_INDEX_VERSION = 3
TAG_INDEX_VERSION = 3
REINDEX_VERSION = 0

def self.disable
Expand Down Expand Up @@ -67,7 +71,7 @@ def self.update_index(table: , id: , raw_data:)
raw_data: indexed_data,
id: id,
locale: SiteSetting.default_locale,
version: INDEX_VERSION,
version: const_get("#{table.upcase}_INDEX_VERSION"),
tsvector: tsvector,
}

Expand Down
16 changes: 8 additions & 8 deletions spec/components/search_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,16 @@

expect do
topic.update!(title: "harpi is the new title")
end.to change { post2.reload.post_search_data.version }.from(SearchIndexer::INDEX_VERSION).to(SearchIndexer::REINDEX_VERSION)
end.to change { post2.reload.post_search_data.version }.from(SearchIndexer::POST_INDEX_VERSION).to(SearchIndexer::REINDEX_VERSION)

expect(post.post_search_data.reload.search_data).to match(/harpi/)
end

it 'should update posts index when topic category changes' do
expect do
topic.update!(category: Fabricate(:category))
end.to change { post.reload.post_search_data.version }.from(SearchIndexer::INDEX_VERSION).to(SearchIndexer::REINDEX_VERSION)
.and change { post2.reload.post_search_data.version }.from(SearchIndexer::INDEX_VERSION).to(SearchIndexer::REINDEX_VERSION)
end.to change { post.reload.post_search_data.version }.from(SearchIndexer::POST_INDEX_VERSION).to(SearchIndexer::REINDEX_VERSION)
.and change { post2.reload.post_search_data.version }.from(SearchIndexer::POST_INDEX_VERSION).to(SearchIndexer::REINDEX_VERSION)
end

it 'should update posts index when topic tags changes' do
Expand All @@ -44,8 +44,8 @@
expect do
DiscourseTagging.tag_topic_by_names(topic, Guardian.new(admin), [tag.name])
topic.save!
end.to change { post.reload.post_search_data.version }.from(SearchIndexer::INDEX_VERSION).to(SearchIndexer::REINDEX_VERSION)
.and change { post2.reload.post_search_data.version }.from(SearchIndexer::INDEX_VERSION).to(SearchIndexer::REINDEX_VERSION)
end.to change { post.reload.post_search_data.version }.from(SearchIndexer::POST_INDEX_VERSION).to(SearchIndexer::REINDEX_VERSION)
.and change { post2.reload.post_search_data.version }.from(SearchIndexer::POST_INDEX_VERSION).to(SearchIndexer::REINDEX_VERSION)

expect(topic.tags).to eq([tag])
end
Expand Down Expand Up @@ -77,10 +77,10 @@
it 'should update posts index when category name changes' do
expect do
category.update!(name: 'some new name')
end.to change { post.reload.post_search_data.version }.from(SearchIndexer::INDEX_VERSION).to(SearchIndexer::REINDEX_VERSION)
.and change { post2.reload.post_search_data.version }.from(SearchIndexer::INDEX_VERSION).to(SearchIndexer::REINDEX_VERSION)
end.to change { post.reload.post_search_data.version }.from(SearchIndexer::POST_INDEX_VERSION).to(SearchIndexer::REINDEX_VERSION)
.and change { post2.reload.post_search_data.version }.from(SearchIndexer::POST_INDEX_VERSION).to(SearchIndexer::REINDEX_VERSION)

expect(post3.post_search_data.version).to eq(SearchIndexer::INDEX_VERSION)
expect(post3.post_search_data.version).to eq(SearchIndexer::POST_INDEX_VERSION)
end
end

Expand Down
2 changes: 1 addition & 1 deletion spec/jobs/reindex_search_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@

subject.execute({})
expect(model.public_send("#{m}_search_data").version)
.to eq(SearchIndexer::INDEX_VERSION)
.to eq("SearchIndexer::#{m.upcase}_INDEX_VERSION".constantize)
end
end

Expand Down
4 changes: 2 additions & 2 deletions spec/services/search_indexer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ def scrub(html, strip_diacritics: false)
raw_data, locale, version = PostSearchData.where(post_id: post_id).pluck(:raw_data, :locale, :version)[0]
expect(raw_data).to eq("This is a test")
expect(locale).to eq(SiteSetting.default_locale)
expect(version).to eq(SearchIndexer::INDEX_VERSION)
expect(version).to eq(SearchIndexer::POST_INDEX_VERSION)

SearchIndexer.update_posts_index(post_id, "tester", "", nil, nil)

Expand Down Expand Up @@ -209,7 +209,7 @@ def scrub(html, strip_diacritics: false)
)

expect(post2.reload.post_search_data.version).to eq(
SearchIndexer::INDEX_VERSION
SearchIndexer::POST_INDEX_VERSION
)
end
end
Expand Down

0 comments on commit 609ba50

Please sign in to comment.