From 609ba50fe8cc89dd9f3a0610e0e24bccca1823c7 Mon Sep 17 00:00:00 2001 From: Guo Xiang Tan Date: Thu, 23 Jul 2020 14:10:05 +0800 Subject: [PATCH] DEV: Add more granularity to `SearchIndexer` versions. Sometimes, we just want to reindex a specific model and not all the things. --- app/jobs/scheduled/reindex_search.rb | 10 +++++----- app/services/search_indexer.rb | 8 ++++++-- spec/components/search_spec.rb | 16 ++++++++-------- spec/jobs/reindex_search_spec.rb | 2 +- spec/services/search_indexer_spec.rb | 4 ++-- 5 files changed, 22 insertions(+), 18 deletions(-) diff --git a/app/jobs/scheduled/reindex_search.rb b/app/jobs/scheduled/reindex_search.rb index 9ec0bd4d812..651a3325d0c 100644 --- a/app/jobs/scheduled/reindex_search.rb +++ b/app/jobs/scheduled/reindex_search.rb @@ -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) diff --git a/app/services/search_indexer.rb b/app/services/search_indexer.rb index 075bd39122b..cb68c0ce595 100644 --- a/app/services/search_indexer.rb +++ b/app/services/search_indexer.rb @@ -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, } diff --git a/spec/components/search_spec.rb b/spec/components/search_spec.rb index d1be6238c1f..f01fed5221c 100644 --- a/spec/components/search_spec.rb +++ b/spec/components/search_spec.rb @@ -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 diff --git a/spec/jobs/reindex_search_spec.rb b/spec/jobs/reindex_search_spec.rb index c5ce22c2b4f..339527e49ee 100644 --- a/spec/jobs/reindex_search_spec.rb +++ b/spec/jobs/reindex_search_spec.rb @@ -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 diff --git a/spec/services/search_indexer_spec.rb b/spec/services/search_indexer_spec.rb index 64488e793dc..d23c171d066 100644 --- a/spec/services/search_indexer_spec.rb +++ b/spec/services/search_indexer_spec.rb @@ -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