fix: Account based rebuild index

This commit is contained in:
Tejaswini Chile 2022-12-23 14:04:09 +05:30
parent c842c98769
commit d9d5d087d3
6 changed files with 66 additions and 14 deletions

View file

@ -0,0 +1,9 @@
class Conversations::AccountBasedSearchJob < ApplicationJob
queue_as :default
def perform
Contact.rebuild_pg_search_documents(account.id)
Conversation.rebuild_pg_search_documents(account.id)
Message.rebuild_pg_search_documents(account.id)
end
end

View file

@ -2,8 +2,8 @@ class Conversations::MultiSearchJob < ApplicationJob
queue_as :default queue_as :default
def perform def perform
Contact.rebuild_pg_search_documents Account.all.each do |account|
PgSearch::Multisearch.rebuild(Conversation) Conversations::AccountBasedSearchJob.perform_later(account.id)
PgSearch::Multisearch.rebuild(Message) end
end end
end end

View file

@ -17,17 +17,18 @@ module MultiSearchableHelpers
initialize_contact_pg_search_record.update!( initialize_contact_pg_search_record.update!(
content: "#{contact.id} #{contact.email} #{contact.name} #{contact.phone_number} #{contact.account_id}", content: "#{contact.id} #{contact.email} #{contact.name} #{contact.phone_number} #{contact.account_id}",
conversation_id: id conversation_id: id,
inbox_id: inbox_id
) )
end end
end end
def contact_pg_search_record def contact_pg_search_record
contacts_pg_search_records.find_by(conversation_id: id) contacts_pg_search_records.find_by(conversation_id: id, inbox_id: inbox_id)
end end
def initialize_contact_pg_search_record def initialize_contact_pg_search_record
record = contacts_pg_search_records.find_by(conversation_id: nil) record = contacts_pg_search_records.find_by(conversation_id: nil, inbox_id: nil)
return record if record.present? return record if record.present?

View file

@ -31,7 +31,7 @@ class Contact < ApplicationRecord
multisearchable( multisearchable(
against: [:id, :email, :name, :phone_number], against: [:id, :email, :name, :phone_number],
additional_attributes: ->(contact) { { conversation_id: nil, account_id: contact.account_id } } additional_attributes: ->(contact) { { conversation_id: nil, account_id: contact.account_id, inbox_id: nil } }
) )
validates :account_id, presence: true validates :account_id, presence: true
@ -149,21 +149,23 @@ class Contact < ApplicationRecord
# NOTE: To add multi search records with conversation_id associated to contacts for previously added records. # NOTE: To add multi search records with conversation_id associated to contacts for previously added records.
# We can not find conversation_id from contacts directly so we added this joins here. # We can not find conversation_id from contacts directly so we added this joins here.
def self.rebuild_pg_search_documents def self.rebuild_pg_search_documents(account_id)
return super unless name == 'Contact' return super unless name == 'Contact'
connection.execute <<~SQL.squish connection.execute <<~SQL.squish
INSERT INTO pg_search_documents (searchable_type, searchable_id, content, account_id, conversation_id, created_at, updated_at) INSERT INTO pg_search_documents (searchable_type, searchable_id, content, account_id, conversation_id, inbox_id, created_at, updated_at)
SELECT 'Contact' AS searchable_type, SELECT 'Contact' AS searchable_type,
contacts.id AS searchable_id, contacts.id AS searchable_id,
CONCAT_WS(' ', contacts.id, contacts.email, contacts.name, contacts.phone_number, contacts.account_id) AS content, CONCAT_WS(' ', contacts.id, contacts.email, contacts.name, contacts.phone_number, contacts.account_id) AS content,
contacts.account_id::int AS account_id, contacts.account_id::int AS account_id,
conversations.id AS conversation_id, conversations.id::int AS conversation_id,
conversations.inbox_id::int AS inbox_id,
now() AS created_at, now() AS created_at,
now() AS updated_at now() AS updated_at
FROM contacts FROM contacts
LEFT OUTER JOIN conversations INNER JOIN conversations
ON conversations.contact_id = contacts.id ON conversations.contact_id = contacts.id
WHERE contacts.account_id = #{account_id}
SQL SQL
end end

View file

@ -52,8 +52,8 @@ class Conversation < ApplicationRecord
include MultiSearchableHelpers include MultiSearchableHelpers
multisearchable( multisearchable(
against: [:display_id, :name, :email, :phone_number], against: [:display_id, :name, :email, :phone_number, :account_id],
additional_attributes: ->(conversation) { { conversation_id: conversation.id, account_id: conversation.account_id } } additional_attributes: ->(conversation) { { conversation_id: conversation.id, account_id: conversation.account_id, inbox_id: inbox_id } }
) )
validates :account_id, presence: true validates :account_id, presence: true
validates :inbox_id, presence: true validates :inbox_id, presence: true
@ -103,7 +103,7 @@ class Conversation < ApplicationRecord
after_commit :set_display_id, unless: :display_id? after_commit :set_display_id, unless: :display_id?
delegate :auto_resolve_duration, to: :account delegate :auto_resolve_duration, to: :account
delegate :name, :email, :phone_number, to: :contact delegate :name, :email, :phone_number, to: :contact, allow_nil: true
def can_reply? def can_reply?
channel = inbox&.channel channel = inbox&.channel
@ -200,6 +200,26 @@ class Conversation < ApplicationRecord
messages.chat.last(5) messages.chat.last(5)
end end
# NOTE: To add multi search records with conversation_id associated to contacts for previously added records.
# We can not find conversation_id from contacts directly so we added this joins here.
def self.rebuild_pg_search_documents(account_id)
return super unless name == 'Conversation'
connection.execute <<~SQL.squish
INSERT INTO pg_search_documents (searchable_type, searchable_id, content, account_id, conversation_id, inbox_id, created_at, updated_at)
SELECT 'Conversation' AS searchable_type,
conversations.id AS searchable_id,
CONCAT_WS(' ', conversations.display_id, conversations.email, conversations.name, conversations.phone_number, conversations.account_id) AS content,
conversations.account_id::int AS account_id,
conversations.id::int AS conversation_id,
conversations.inbox_id::int AS inbox_id,
now() AS created_at,
now() AS updated_at
FROM conversations
WHERE conversations.account_id = #{account_id}
SQL
end
private private
def execute_after_update_commit_callbacks def execute_after_update_commit_callbacks

View file

@ -170,6 +170,26 @@ class Message < ApplicationRecord
true true
end end
# NOTE: To add multi search records with conversation_id associated to contacts for previously added records.
# We can not find conversation_id from contacts directly so we added this joins here.
def self.rebuild_pg_search_documents(account_id)
return super unless name == 'Message'
connection.execute <<~SQL.squish
INSERT INTO pg_search_documents (searchable_type, searchable_id, content, account_id, conversation_id, inbox_id, created_at, updated_at)
SELECT 'Message' AS searchable_type,
messages.id AS searchable_id,
CONCAT_WS(' ', messages.content) AS content,
messages.account_id::int AS account_id,
messages.conversation_id::int AS conversation_id,
messages.inbox_id::int AS inbox_id,
now() AS created_at,
now() AS updated_at
FROM messages
WHERE messages.account_id = #{account_id}
SQL
end
private private
def ensure_content_type def ensure_content_type