fix: search improvements for multiple model with separate results
This commit is contained in:
parent
23cd34cbd6
commit
a8600d79f4
4 changed files with 77 additions and 28 deletions
|
@ -34,7 +34,9 @@ class ConversationFinder
|
||||||
filter_by_assignee_type
|
filter_by_assignee_type
|
||||||
|
|
||||||
{
|
{
|
||||||
conversations: conversations,
|
conversations: @conversations,
|
||||||
|
contacts: @contacts,
|
||||||
|
messages: @messages
|
||||||
count: {
|
count: {
|
||||||
mine_count: mine_count,
|
mine_count: mine_count,
|
||||||
assigned_count: assigned_count,
|
assigned_count: assigned_count,
|
||||||
|
@ -104,8 +106,27 @@ class ConversationFinder
|
||||||
end
|
end
|
||||||
|
|
||||||
def filter_by_query
|
def filter_by_query
|
||||||
conversation_ids = PgSearch.multisearch("#{params[:q]}%").where(account_id: current_account).pluck(:conversation_id)
|
{
|
||||||
@conversations = Conversation.where(id: conversation_ids).includes(:messages)
|
messages: filter_messages,
|
||||||
|
conversations: filter_conversations,
|
||||||
|
contacts: filter_contacts
|
||||||
|
}
|
||||||
|
end
|
||||||
|
|
||||||
|
def filter_conversations
|
||||||
|
conversation_ids = PgSearch.multisearch("#{@params[:q]}%").where(account_id: @current_account,
|
||||||
|
searchable_type: 'Conversation').pluck(:searchable_id)
|
||||||
|
@conversations = Conversation.where(id: conversation_ids)
|
||||||
|
end
|
||||||
|
|
||||||
|
def filter_messages
|
||||||
|
message_ids = PgSearch.multisearch("#{@params[:q]}%").where(account_id: @current_account, searchable_type: 'Message').pluck(:searchable_id)
|
||||||
|
@messages = Message.where(id: message_ids)
|
||||||
|
end
|
||||||
|
|
||||||
|
def filter_contacts
|
||||||
|
contact_ids = PgSearch.multisearch("#{@params[:q]}%").where(account_id: @current_account, searchable_type: 'Contact').pluck(:searchable_id)
|
||||||
|
@contacts = Contact.where(id: contact_ids)
|
||||||
end
|
end
|
||||||
|
|
||||||
def filter_by_status
|
def filter_by_status
|
||||||
|
|
|
@ -12,27 +12,37 @@ module MultiSearchableHelpers
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
def self.rebuild_pg_search_documents
|
|
||||||
return 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)
|
|
||||||
SELECT 'Contact' AS searchable_type,
|
|
||||||
contacts.id AS searchable_id,
|
|
||||||
CONCAT_WS(' ', contacts.email, contacts.name, contacts.phone_number, contacts.id, contacts.account_id) AS content,
|
|
||||||
contacts.account_id::int AS account_id,
|
|
||||||
conversations.id AS conversation_id,
|
|
||||||
now() AS created_at,
|
|
||||||
now() AS updated_at
|
|
||||||
FROM contacts
|
|
||||||
INNER JOIN conversations
|
|
||||||
ON conversations.contact_id = contacts.id
|
|
||||||
SQL
|
|
||||||
end
|
|
||||||
|
|
||||||
def update_contact_search_document
|
def update_contact_search_document
|
||||||
PgSearch::Document.create({ searchable_type: 'Contact', searchable_id: contact_id, conversation_id: id, account_id: account_id,
|
return if contact_pg_search_record.present?
|
||||||
content: "#{contact.email} #{contact.name} #{contact.phone_number} #{contact.id}" })
|
|
||||||
|
initialize_contact_pg_search_record.update!(
|
||||||
|
content: "#{contact.id} #{contact.email} #{contact.name} #{contact.phone_number}",
|
||||||
|
conversation_id: id
|
||||||
|
)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def contact_pg_search_record
|
||||||
|
contacts_pg_search_records.find_by(conversation_id: id)
|
||||||
|
end
|
||||||
|
|
||||||
|
def initialize_contact_pg_search_record
|
||||||
|
record = contacts_pg_search_records.find_by(conversation_id: nil)
|
||||||
|
|
||||||
|
return record if record.present?
|
||||||
|
|
||||||
|
PgSearch::Document.new(
|
||||||
|
searchable_type: 'Contact',
|
||||||
|
searchable_id: contact_id,
|
||||||
|
account_id: account_id
|
||||||
|
)
|
||||||
|
end
|
||||||
|
|
||||||
|
def contacts_pg_search_records
|
||||||
|
contacts_pg_search_records ||= PgSearch::Document.where(
|
||||||
|
searchable_type: 'Contact',
|
||||||
|
searchable_id: contact_id,
|
||||||
|
account_id: account_id
|
||||||
|
)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -29,10 +29,10 @@ class Contact < ApplicationRecord
|
||||||
include PgSearch::Model
|
include PgSearch::Model
|
||||||
include MultiSearchableHelpers
|
include MultiSearchableHelpers
|
||||||
|
|
||||||
# multisearchable(
|
multisearchable(
|
||||||
# against: [:email, :name, :phone_number, :id],
|
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 } }
|
||||||
# )
|
)
|
||||||
|
|
||||||
validates :account_id, presence: true
|
validates :account_id, presence: true
|
||||||
validates :email, allow_blank: true, uniqueness: { scope: [:account_id], case_sensitive: false },
|
validates :email, allow_blank: true, uniqueness: { scope: [:account_id], case_sensitive: false },
|
||||||
|
@ -147,6 +147,24 @@ class Contact < ApplicationRecord
|
||||||
email_format
|
email_format
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def self.rebuild_pg_search_documents
|
||||||
|
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)
|
||||||
|
SELECT 'Contact' AS searchable_type,
|
||||||
|
contacts.id AS searchable_id,
|
||||||
|
CONCAT_WS(' ', contacts.email, contacts.name, contacts.phone_number, contacts.id, contacts.account_id) AS content,
|
||||||
|
contacts.account_id::int AS account_id,
|
||||||
|
conversations.id AS conversation_id,
|
||||||
|
now() AS created_at,
|
||||||
|
now() AS updated_at
|
||||||
|
FROM contacts
|
||||||
|
LEFT OUTER JOIN conversations
|
||||||
|
ON conversations.contact_id = contacts.id
|
||||||
|
SQL
|
||||||
|
end
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|
||||||
def ip_lookup
|
def ip_lookup
|
||||||
|
|
|
@ -99,7 +99,7 @@ class Conversation < ApplicationRecord
|
||||||
|
|
||||||
after_update_commit :execute_after_update_commit_callbacks
|
after_update_commit :execute_after_update_commit_callbacks
|
||||||
after_create_commit :notify_conversation_creation
|
after_create_commit :notify_conversation_creation
|
||||||
after_create :update_contact_search_document
|
after_create_commit :update_contact_search_document, if: :contact_id?
|
||||||
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
|
||||||
|
|
Loading…
Reference in a new issue