fix: Account based rebuild index
This commit is contained in:
parent
c842c98769
commit
d9d5d087d3
6 changed files with 66 additions and 14 deletions
9
app/jobs/conversations/account_based_search_job.rb
Normal file
9
app/jobs/conversations/account_based_search_job.rb
Normal 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
|
|
@ -2,8 +2,8 @@ class Conversations::MultiSearchJob < ApplicationJob
|
|||
queue_as :default
|
||||
|
||||
def perform
|
||||
Contact.rebuild_pg_search_documents
|
||||
PgSearch::Multisearch.rebuild(Conversation)
|
||||
PgSearch::Multisearch.rebuild(Message)
|
||||
Account.all.each do |account|
|
||||
Conversations::AccountBasedSearchJob.perform_later(account.id)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -17,17 +17,18 @@ module MultiSearchableHelpers
|
|||
|
||||
initialize_contact_pg_search_record.update!(
|
||||
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
|
||||
|
||||
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
|
||||
|
||||
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?
|
||||
|
||||
|
|
|
@ -31,7 +31,7 @@ class Contact < ApplicationRecord
|
|||
|
||||
multisearchable(
|
||||
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
|
||||
|
@ -149,21 +149,23 @@ class Contact < ApplicationRecord
|
|||
|
||||
# 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
|
||||
def self.rebuild_pg_search_documents(account_id)
|
||||
return super unless name == 'Contact'
|
||||
|
||||
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,
|
||||
contacts.id AS searchable_id,
|
||||
CONCAT_WS(' ', contacts.id, contacts.email, contacts.name, contacts.phone_number, contacts.account_id) AS content,
|
||||
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 updated_at
|
||||
FROM contacts
|
||||
LEFT OUTER JOIN conversations
|
||||
INNER JOIN conversations
|
||||
ON conversations.contact_id = contacts.id
|
||||
WHERE contacts.account_id = #{account_id}
|
||||
SQL
|
||||
end
|
||||
|
||||
|
|
|
@ -52,8 +52,8 @@ class Conversation < ApplicationRecord
|
|||
include MultiSearchableHelpers
|
||||
|
||||
multisearchable(
|
||||
against: [:display_id, :name, :email, :phone_number],
|
||||
additional_attributes: ->(conversation) { { conversation_id: conversation.id, account_id: conversation.account_id } }
|
||||
against: [:display_id, :name, :email, :phone_number, :account_id],
|
||||
additional_attributes: ->(conversation) { { conversation_id: conversation.id, account_id: conversation.account_id, inbox_id: inbox_id } }
|
||||
)
|
||||
validates :account_id, presence: true
|
||||
validates :inbox_id, presence: true
|
||||
|
@ -103,7 +103,7 @@ class Conversation < ApplicationRecord
|
|||
after_commit :set_display_id, unless: :display_id?
|
||||
|
||||
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?
|
||||
channel = inbox&.channel
|
||||
|
@ -200,6 +200,26 @@ class Conversation < ApplicationRecord
|
|||
messages.chat.last(5)
|
||||
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
|
||||
|
||||
def execute_after_update_commit_callbacks
|
||||
|
|
|
@ -170,6 +170,26 @@ class Message < ApplicationRecord
|
|||
true
|
||||
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
|
||||
|
||||
def ensure_content_type
|
||||
|
|
Loading…
Reference in a new issue