sync feed content documents in Elasticsearch (#6888) [deploy]
This commit is contained in:
parent
e6c56d408b
commit
d20a50aa2b
2 changed files with 46 additions and 0 deletions
|
|
@ -0,0 +1,28 @@
|
|||
module DataUpdateScripts
|
||||
class ReIndexFeedContentToElasticsearch
|
||||
def run
|
||||
clear_existing_feed_documents
|
||||
|
||||
index_docs(Article.pluck(:id), "Article")
|
||||
index_docs(PodcastEpisode.pluck(:id), "PodcastEpisode")
|
||||
index_docs(Comment.pluck(:id), "Comment")
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def clear_existing_feed_documents
|
||||
# Clear out documents with incorrect IDs before reindex
|
||||
Search::Client.delete_by_query(
|
||||
index: Search::FeedContent::INDEX_ALIAS, body: { query: { match_all: {} } },
|
||||
)
|
||||
end
|
||||
|
||||
def index_docs(ids, doc_type)
|
||||
ids.each do |id|
|
||||
Search::IndexToElasticsearchWorker.set(queue: :low_priority).perform_async(
|
||||
doc_type, id
|
||||
)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
@ -0,0 +1,18 @@
|
|||
require "rails_helper"
|
||||
require Rails.root.join("lib/data_update_scripts/20200326145114_re_index_feed_content_to_elasticsearch.rb")
|
||||
|
||||
describe DataUpdateScripts::ReIndexFeedContentToElasticsearch, elasticsearch: true do
|
||||
it "indexes feed content(articles, comments, podcast episodes) to Elasticsearch" do
|
||||
article = create(:article)
|
||||
podcast_episode = create(:podcast_episode)
|
||||
comment = create(:comment)
|
||||
expect { article.elasticsearch_doc }.to raise_error(Search::Errors::Transport::NotFound)
|
||||
expect { podcast_episode.elasticsearch_doc }.to raise_error(Search::Errors::Transport::NotFound)
|
||||
expect { comment.elasticsearch_doc }.to raise_error(Search::Errors::Transport::NotFound)
|
||||
|
||||
sidekiq_perform_enqueued_jobs { described_class.new.run }
|
||||
expect(article.elasticsearch_doc).not_to be_nil
|
||||
expect(podcast_episode.elasticsearch_doc).not_to be_nil
|
||||
expect(comment.elasticsearch_doc).not_to be_nil
|
||||
end
|
||||
end
|
||||
Loading…
Add table
Reference in a new issue