Files
gochat/migrations/000008_add_search_indexes.up.sql
T

37 lines
2.1 KiB
SQL

-- M14: Global Search + Advanced Filter — performance indexes
-- Adds indexes to support ILIKE text search and advanced filter queries
-- across conversations, messages, and contacts.
-- Conversations: text search on status, labels, and account filtering
CREATE INDEX IF NOT EXISTS idx_conversations_account_status ON conversations (account_id, status);
CREATE INDEX IF NOT EXISTS idx_conversations_account_assignee ON conversations (account_id, assignee_id);
CREATE INDEX IF NOT EXISTS idx_conversations_account_priority ON conversations (account_id, priority);
CREATE INDEX IF NOT EXISTS idx_conversations_account_inbox ON conversations (account_id, inbox_id);
CREATE INDEX IF NOT EXISTS idx_conversations_labels ON conversations (account_id, labels);
-- Messages: text search on content, filter by type/sender/private
CREATE INDEX IF NOT EXISTS idx_messages_account_type ON messages (account_id, message_type);
CREATE INDEX IF NOT EXISTS idx_messages_account_sender ON messages (account_id, sender_type);
CREATE INDEX IF NOT EXISTS idx_messages_account_content_type ON messages (account_id, content_type);
CREATE INDEX IF NOT EXISTS idx_messages_account_private ON messages (account_id, private);
CREATE INDEX IF NOT EXISTS idx_messages_conversation_id ON messages (conversation_id);
-- Contacts: text search on name, email, phone; filter by source/source_id.
DO $$
BEGIN
IF EXISTS (
SELECT 1 FROM information_schema.columns
WHERE table_name = 'contacts' AND column_name = 'source'
) THEN
CREATE INDEX IF NOT EXISTS idx_contacts_account_source ON contacts (account_id, source);
ELSIF EXISTS (
SELECT 1 FROM information_schema.columns
WHERE table_name = 'contacts' AND column_name = 'source_id'
) THEN
CREATE INDEX IF NOT EXISTS idx_contacts_account_source_id ON contacts (account_id, source_id);
END IF;
END $$;
CREATE INDEX IF NOT EXISTS idx_contacts_name ON contacts (account_id, name);
CREATE INDEX IF NOT EXISTS idx_contacts_email ON contacts (account_id, email);
CREATE INDEX IF NOT EXISTS idx_contacts_phone ON contacts (account_id, phone_number);