DEV: Add more granularity to `SearchIndexer` versions.

Sometimes, we just want to reindex a specific model and not all the
things.
This commit is contained in:
Guo Xiang Tan 2020-07-23 14:10:05 +08:00
parent 4b053462c0
commit 609ba50fe8
No known key found for this signature in database
GPG Key ID: FBD110179AAC1F20
5 changed files with 22 additions and 18 deletions

View File

@ -140,7 +140,7 @@ module Jobs
def load_problem_post_ids(limit)
params = {
locale: SiteSetting.default_locale,
version: SearchIndexer::INDEX_VERSION,
version: SearchIndexer::POST_INDEX_VERSION,
limit: limit
}
@ -165,7 +165,7 @@ module Jobs
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)
@ -174,7 +174,7 @@ module Jobs
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)
@ -183,7 +183,7 @@ module Jobs
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)
@ -192,7 +192,7 @@ module Jobs
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)

View File

@ -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
@ -67,7 +71,7 @@ class SearchIndexer
raw_data: indexed_data,
id: id,
locale: SiteSetting.default_locale,
version: INDEX_VERSION,
version: const_get("#{table.upcase}_INDEX_VERSION"),
tsvector: tsvector,
}

View File

@ -25,7 +25,7 @@ describe Search do
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
@ -33,8 +33,8 @@ describe Search do
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
@ -44,8 +44,8 @@ describe Search do
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
@ -77,10 +77,10 @@ describe Search do
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

View File

@ -28,7 +28,7 @@ describe Jobs::ReindexSearch do
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

View File

@ -103,7 +103,7 @@ describe SearchIndexer do
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)
@ -209,7 +209,7 @@ describe SearchIndexer do
)
expect(post2.reload.post_search_data.version).to eq(
SearchIndexer::INDEX_VERSION
SearchIndexer::POST_INDEX_VERSION
)
end
end