Objective
Implement semantic search for documents using vector embeddings. This enables finding documents by meaning rather than keywords, improving search relevance and discovery.
Step 1: Create Embedded Documents Table
Create a table for documents with embeddings.
CREATE TABLE embedded_documents (
id INTEGER PRIMARY KEY,
title VARCHAR(255) NOT NULL,
document PDF,
summary TEXT,
full_text TEXT,
category VARCHAR(50),
author VARCHAR(100),
page_count INTEGER,
title_embedding VECTOR(384),
summary_embedding VECTOR(384),
content_embedding VECTOR(384),
is_indexed BOOLEAN DEFAULT FALSE,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
Step 2: Insert Sample Documents
Add documents with content for embedding.
INSERT INTO embedded_documents (id, title, summary, full_text, category, author, page_count) VALUES
(1, 'Machine Learning in Healthcare',
'This paper explores applications of machine learning algorithms in medical diagnosis and patient care optimization.',
'Machine learning is revolutionizing healthcare by enabling predictive diagnostics, personalized treatment plans, and automated medical image analysis. This comprehensive review examines current applications and future directions.',
'research', 'Dr. Smith', 35),
(2, 'Financial Risk Management Framework',
'A comprehensive guide to implementing risk management strategies in financial institutions.',
'Risk management is essential for financial stability. This document outlines frameworks for credit risk, market risk, operational risk assessment and mitigation strategies for banking institutions.',
'finance', 'Risk Team', 48),
(3, 'Software Architecture Patterns',
'Overview of modern software architecture patterns including microservices and event-driven design.',
'Modern software systems require scalable architectures. This guide covers microservices, event-driven architecture, CQRS, and domain-driven design patterns with practical implementation examples.',
'technical', 'Tech Architects', 62),
(4, 'Employee Performance Management',
'Best practices for evaluating and developing employee performance in organizations.',
'Effective performance management drives organizational success. This handbook covers goal setting, feedback mechanisms, performance reviews, and professional development planning.',
'hr', 'HR Department', 28),
(5, 'Data Privacy Compliance Guide',
'Guidelines for ensuring compliance with data privacy regulations including GDPR and CCPA.',
'Data privacy is a critical concern for modern organizations. This guide provides comprehensive coverage of GDPR, CCPA, and other privacy regulations with compliance checklists.',
'legal', 'Compliance Team', 42),
(6, 'Cloud Migration Strategy',
'Strategic planning guide for migrating enterprise applications to cloud infrastructure.',
'Cloud migration requires careful planning. This document covers assessment, planning, migration strategies, and optimization for AWS, Azure, and GCP deployments.',
'technical', 'Cloud Team', 55),
(7, 'Customer Experience Optimization',
'Strategies for improving customer experience through data-driven insights.',
'Customer experience is key to business success. This report examines customer journey mapping, feedback analysis, and experience optimization techniques using analytics.',
'marketing', 'CX Team', 38),
(8, 'Cybersecurity Best Practices',
'Essential cybersecurity practices for protecting organizational assets and data.',
'Cybersecurity threats continue to evolve. This manual covers threat assessment, security controls, incident response, and security awareness training programs.',
'security', 'Security Team', 45);
Step 3: Generate Embeddings
Create embeddings for document fields.
UPDATE embedded_documents
SET title_embedding = EMBED(title),
summary_embedding = EMBED(summary),
content_embedding = EMBED(full_text),
is_indexed = TRUE
WHERE is_indexed = FALSE;
Step 4: Semantic Search by Query
Search documents using natural language.
SELECT
title,
category,
author,
COSINE_SIMILARITY(summary_embedding, EMBED('healthcare and medical applications')) as relevance
FROM embedded_documents
WHERE is_indexed = TRUE
ORDER BY relevance DESC
LIMIT 5;