Reindex comments' Elasticsearch doc when commentable is updated (#12330)

This commit is contained in:
Mac Siri 2021-01-20 09:44:59 -05:00 committed by GitHub
parent a5365b762e
commit bdd05f7a9d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 61 additions and 2 deletions

View file

@ -8,6 +8,7 @@ class Article < ApplicationRecord
SEARCH_SERIALIZER = Search::ArticleSerializer
SEARCH_CLASS = Search::FeedContent
DATA_SYNC_CLASS = DataSync::Elasticsearch::Article
acts_as_taggable_on :tags
resourcify
@ -100,8 +101,10 @@ class Article < ApplicationRecord
after_update_commit :update_notifications, if: proc { |article|
article.notifications.any? && !article.saved_changes.empty?
}
after_commit :async_score_calc, :touch_collection, on: %i[create update]
after_commit :index_to_elasticsearch, on: %i[create update]
after_commit :sync_related_elasticsearch_docs, on: %i[update]
after_commit :remove_from_elasticsearch, on: [:destroy]
serialize :cached_user

View file

@ -9,8 +9,8 @@ module Search
comment.class.name
end
attribute :hotness_score, &:score
attribute :published do |_comment|
true
attribute :published do |comment|
comment.commentable&.published
end
attribute :published_at, &:created_at
attribute :readable_publish_date_string, &:readable_publish_date

View file

@ -0,0 +1,16 @@
module DataSync
module Elasticsearch
class Article < Base
RELATED_DOCS = %i[
comments
].freeze
SHARED_FIELDS = %i[
published
title
].freeze
delegate :comments, to: :@updated_record
end
end
end

View file

@ -0,0 +1,9 @@
module DataUpdateScripts
class ResyncUnpublishedArticlesCommentsElasticsearchDocument
def run
Article.unpublished.where.not(comments_count: 0).each do |article|
article.comments.each(&:index_to_elasticsearch)
end
end
end
end

View file

@ -0,0 +1,17 @@
require "rails_helper"
require Rails.root.join(
"lib/data_update_scripts/20210118194138_resync_unpublished_articles_comments_elasticsearch_document.rb",
)
describe DataUpdateScripts::ResyncUnpublishedArticlesCommentsElasticsearchDocument do
it "works" do
article = create(:article)
comment = create(:comment, commentable: article)
sidekiq_perform_enqueued_jobs
article.update_column(:published, false)
sidekiq_assert_enqueued_with(job: Search::IndexWorker, args: ["Comment", comment.id]) do
described_class.new.run
end
end
end

View file

@ -139,6 +139,12 @@ RSpec.describe Article, type: :model do
article.destroy
end
end
it "on update syncs elasticsearch data" do
allow(article).to receive(:sync_related_elasticsearch_docs)
article.save
expect(article).to have_received(:sync_related_elasticsearch_docs)
end
end
context "when published" do

View file

@ -0,0 +1,8 @@
require "rails_helper"
RSpec.describe DataSync::Elasticsearch::Article, type: :service do
it "defines necessary constants" do
expect(described_class::RELATED_DOCS).not_to be_nil
expect(described_class::SHARED_FIELDS).not_to be_nil
end
end