PERF: Better use of index when queueing a topci for search reindex.

Also move `Search::INDEX_VERSION` to `SearchIndexer` which is where the
version is actually being used.
This commit is contained in:
Guo Xiang Tan
2019-04-02 09:52:59 +08:00
parent 9603c906ec
commit d8704c11ca
5 changed files with 42 additions and 20 deletions

View File

@@ -25,7 +25,7 @@ describe Jobs::ReindexSearch do
model.reload
subject.execute({})
expect(model.send("#{m}_search_data").version).to eq Search::INDEX_VERSION
expect(model.send("#{m}_search_data").version).to eq SearchIndexer::INDEX_VERSION
end
end

View File

@@ -3,6 +3,14 @@ require 'rails_helper'
describe SearchIndexer do
let(:post_id) { 99 }
before do
SearchIndexer.enable
end
after do
SearchIndexer.disable
end
def scrub(html, strip_diacritics: false)
SearchIndexer.scrub_html_for_search(html, strip_diacritics: strip_diacritics)
end
@@ -75,7 +83,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("en")
expect(version).to eq(Search::INDEX_VERSION)
expect(version).to eq(SearchIndexer::INDEX_VERSION)
SearchIndexer.update_posts_index(post_id, "tester", "", nil, nil)
@@ -86,14 +94,6 @@ describe SearchIndexer do
describe '.index' do
let(:post) { Fabricate(:post) }
before do
SearchIndexer.enable
end
after do
SearchIndexer.disable
end
it 'should index posts correctly' do
expect { post }.to change { PostSearchData.count }.by(1)
@@ -153,4 +153,23 @@ describe SearchIndexer do
)
end
end
describe '.queue_post_reindex' do
let(:post) { Fabricate(:post) }
let(:topic) { post.topic }
it 'should reset the version of search data for all posts in the topic' do
post2 = Fabricate(:post)
SearchIndexer.queue_post_reindex(topic.id)
expect(post.reload.post_search_data.version).to eq(
SearchIndexer::REINDEX_VERSION
)
expect(post2.reload.post_search_data.version).to eq(
SearchIndexer::INDEX_VERSION
)
end
end
end