chore: Search optimisations (#6644)

- Strip search term before searching
- order messages by created_at desc
- order contacts by last_activity_at desc
- order conversations by created_at desc
- Search only resolved contacts
- Optimize resolved contacts query

ref: #6583
This commit is contained in:
Sojan Jose
2023-03-13 19:10:31 +05:30
committed by GitHub
parent 7cbf1857e4
commit da76537011
7 changed files with 42 additions and 23 deletions

View File

@@ -136,9 +136,7 @@ class Contact < ApplicationRecord
end
def self.resolved_contacts
where.not(email: [nil, '']).or(
Current.account.contacts.where.not(phone_number: [nil, ''])
).or(Current.account.contacts.where.not(identifier: [nil, '']))
where("COALESCE(NULLIF(contacts.email,''),NULLIF(contacts.phone_number,''),NULLIF(contacts.identifier,'')) IS NOT NULL")
end
def discard_invalid_attrs

View File

@@ -24,6 +24,8 @@ class InstallationConfig < ApplicationRecord
before_validation :set_lock
validates :name, presence: true
# TODO: Get rid of default scope
# https://stackoverflow.com/a/1834250/939299
default_scope { order(created_at: :desc) }
scope :editable, -> { where(locked: false) }

View File

@@ -81,6 +81,10 @@ class Message < ApplicationRecord
scope :chat, -> { where.not(message_type: :activity).where(private: false) }
scope :non_activity_messages, -> { where.not(message_type: :activity).reorder('id desc') }
scope :today, -> { where("date_trunc('day', created_at) = ?", Date.current) }
# TODO: Get rid of default scope
# https://stackoverflow.com/a/1834250/939299
# if you want to change order, use `reorder`
default_scope { order(created_at: :asc) }
belongs_to :account

View File

@@ -42,6 +42,8 @@ class Notification < ApplicationRecord
after_create_commit :process_notification_delivery, :dispatch_create_event
# TODO: Get rid of default scope
# https://stackoverflow.com/a/1834250/939299
default_scope { order(id: :desc) }
PRIMARY_ACTORS = ['Conversation'].freeze

View File

@@ -20,24 +20,31 @@ class SearchService
@accessable_inbox_ids ||= @current_user.assigned_inboxes.pluck(:id)
end
def search_query
@search_query ||= params[:q].to_s.strip
end
def filter_conversations
@conversations = current_account.conversations.where(inbox_id: accessable_inbox_ids)
.joins('INNER JOIN contacts ON conversations.contact_id = contacts.id')
.where("cast(conversations.display_id as text) ILIKE :search OR contacts.name ILIKE :search OR contacts.email
ILIKE :search OR contacts.phone_number ILIKE :search OR contacts.identifier ILIKE :search", search: "%#{params[:q]}%")
ILIKE :search OR contacts.phone_number ILIKE :search OR contacts.identifier ILIKE :search", search: "%#{search_query}%")
.order('conversations.created_at DESC')
.limit(10)
end
def filter_messages
@messages = current_account.messages.where(inbox_id: accessable_inbox_ids)
.where('messages.content ILIKE :search', search: "%#{params[:q]}%")
.where('created_at >= ?', 3.months.ago).limit(10)
.where('messages.content ILIKE :search', search: "%#{search_query}%")
.where('created_at >= ?', 3.months.ago)
.reorder('created_at DESC')
.limit(10)
end
def filter_contacts
@contacts = current_account.contacts.where(
"name ILIKE :search OR email ILIKE :search OR phone_number
ILIKE :search OR identifier ILIKE :search", search: "%#{params[:q]}%"
).limit(10)
ILIKE :search OR identifier ILIKE :search", search: "%#{search_query}%"
).resolved_contacts.order_on_last_activity_at('desc').limit(10)
end
end