Skip to content

Commit 512d3fe

Browse files
committed
feat: add PostgreSQL Hybrid Search demo
Side-by-side comparison of FTS, Hybrid (RRF), and Semantic search. Uses Supabase (pgvector + PostgreSQL FTS). 30 sample articles with interactive Live Component. Related: symfony/ai#783 Author: Ahmed EBEN HASSINE <[email protected]>
1 parent 7c7071a commit 512d3fe

16 files changed

+1302
-0
lines changed

.env

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,14 @@ APP_SECRET=ccb9dca72dce53c683eaaf775bfdb253
2222
CHROMADB_HOST=chromadb
2323
CHROMADB_PORT=8080
2424
OPENAI_API_KEY=sk-...
25+
26+
###> PostgreSQL Hybrid Search with Supabase ###
27+
# Get these values from your Supabase project settings:
28+
# 1. Go to https://supabase.com/dashboard
29+
# 2. Create a new project (free tier available)
30+
# 3. Go to Settings > Database
31+
# 4. Copy the connection string
32+
POSTGRES_DSN="pgsql:host=db.xxxxxxxxxxxxx.supabase.co;port=5432;dbname=postgres;sslmode=require"
33+
POSTGRES_USER=postgres
34+
POSTGRES_PASSWORD=your-supabase-password-here
35+
###< PostgreSQL Hybrid Search with Supabase ###

compose.yaml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,18 @@ services:
99
- IS_PERSISTENT=TRUE
1010
- PERSIST_DIRECTORY=/chroma/chroma # this is the default path, change it as needed
1111
- ANONYMIZED_TELEMETRY=FALSE
12+
13+
# PostgreSQL local (optional, use Supabase instead for easier setup)
14+
# postgres:
15+
# image: ankane/pgvector:latest
16+
# ports:
17+
# - '5432:5432'
18+
# environment:
19+
# - POSTGRES_DB=symfony_ai_demo
20+
# - POSTGRES_USER=postgres
21+
# - POSTGRES_PASSWORD=postgres
22+
# volumes:
23+
# - postgres_data:/var/lib/postgresql/data
24+
25+
# volumes:
26+
# postgres_data:

config/packages/ai.yaml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,11 @@ ai:
5959
chroma_db:
6060
symfonycon:
6161
collection: 'symfony_blog'
62+
postgres_hybrid:
63+
articles:
64+
connection: '@pdo.connection'
65+
table_name: 'hybrid_articles'
66+
semantic_ratio: 0.5
6267
vectorizer:
6368
openai:
6469
model: 'text-embedding-ada-002'

config/services.yaml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,11 @@ services:
1818
- '../src/DependencyInjection/'
1919
- '../src/Entity/'
2020
- '../src/Kernel.php'
21+
22+
# PostgreSQL PDO connection for Hybrid Search
23+
pdo.connection:
24+
class: PDO
25+
arguments:
26+
- '%env(POSTGRES_DSN)%'
27+
- '%env(POSTGRES_USER)%'
28+
- '%env(POSTGRES_PASSWORD)%'
Lines changed: 240 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,240 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace App\PostgresHybrid;
13+
14+
/**
15+
* Tech articles fixtures for PostgreSQL Hybrid Search demo.
16+
*
17+
* @author Ahmed EBEN HASSINE <[email protected]>
18+
*/
19+
final class ArticleFixtures
20+
{
21+
/**
22+
* @return array<int, array{title: string, content: string, category: string, tags: string[]}>
23+
*/
24+
public static function getArticles(): array
25+
{
26+
return [
27+
// Performance & Optimization
28+
[
29+
'title' => 'Symfony Performance Optimization Techniques',
30+
'content' => 'Learn how to boost your Symfony application performance with advanced caching strategies, profiler usage, and query optimization. Discover techniques for speeding up page load times and improving response rates.',
31+
'category' => 'Performance',
32+
'tags' => ['symfony', 'optimization', 'caching', 'profiler'],
33+
],
34+
[
35+
'title' => 'Database Query Performance Tuning',
36+
'content' => 'Master the art of SQL optimization with indexes, EXPLAIN analysis, and query planning. Learn how to identify slow queries and optimize them for better performance and scalability.',
37+
'category' => 'Database',
38+
'tags' => ['sql', 'database', 'performance', 'indexes'],
39+
],
40+
[
41+
'title' => 'Building Lightning-Fast APIs with Symfony',
42+
'content' => 'Create high-speed REST APIs using proper HTTP caching, pagination strategies, and response optimization. Implement rate limiting and efficient serialization for maximum throughput.',
43+
'category' => 'API',
44+
'tags' => ['api', 'rest', 'fast', 'caching'],
45+
],
46+
[
47+
'title' => 'PHP 8.4 Performance Improvements',
48+
'content' => 'Explore the performance enhancements in PHP 8.4 including JIT compiler optimizations, improved memory management, and faster array operations. Benchmark comparisons included.',
49+
'category' => 'PHP',
50+
'tags' => ['php', 'php8.4', 'performance', 'jit'],
51+
],
52+
53+
// PHP Language Features
54+
[
55+
'title' => 'PHP 8.4 New Features: Property Hooks',
56+
'content' => 'Deep dive into PHP 8.4 property hooks feature. Learn how to use get and set hooks for cleaner encapsulation without writing explicit getter/setter methods.',
57+
'category' => 'PHP',
58+
'tags' => ['php', 'php8.4', 'property-hooks', 'features'],
59+
],
60+
[
61+
'title' => 'Mastering PHP 8.3 Typed Class Constants',
62+
'content' => 'Understand how typed class constants in PHP 8.3 improve code safety and IDE support. Examples of using scalar types, union types, and class types for constants.',
63+
'category' => 'PHP',
64+
'tags' => ['php', 'php8.3', 'types', 'constants'],
65+
],
66+
[
67+
'title' => 'Asynchronous PHP with Fiber and Amp',
68+
'content' => 'Learn concurrent programming in PHP using Fibers introduced in PHP 8.1 and the Amp framework. Build non-blocking applications without traditional callbacks.',
69+
'category' => 'PHP',
70+
'tags' => ['php', 'async', 'fiber', 'amp'],
71+
],
72+
73+
// Security
74+
[
75+
'title' => 'Symfony Security Best Practices 2024',
76+
'content' => 'Comprehensive guide to securing Symfony applications: authentication, authorization, CSRF protection, password hashing with Argon2, and preventing common vulnerabilities.',
77+
'category' => 'Security',
78+
'tags' => ['symfony', 'security', 'authentication', 'csrf'],
79+
],
80+
[
81+
'title' => 'Preventing SQL Injection in PHP Applications',
82+
'content' => 'Learn how to protect your PHP applications from SQL injection attacks using prepared statements, parameterized queries, and proper input validation techniques.',
83+
'category' => 'Security',
84+
'tags' => ['security', 'sql-injection', 'php', 'database'],
85+
],
86+
[
87+
'title' => 'API Security: OAuth2 and JWT Implementation',
88+
'content' => 'Implement robust API authentication using OAuth2 flows and JSON Web Tokens. Best practices for token storage, refresh tokens, and secure transmission.',
89+
'category' => 'Security',
90+
'tags' => ['security', 'oauth2', 'jwt', 'api'],
91+
],
92+
93+
// Database & Doctrine
94+
[
95+
'title' => 'Doctrine ORM Advanced Techniques',
96+
'content' => 'Master Doctrine ORM with advanced mapping strategies, lazy loading optimization, custom DQL functions, and efficient entity relationships management.',
97+
'category' => 'Database',
98+
'tags' => ['doctrine', 'orm', 'database', 'symfony'],
99+
],
100+
[
101+
'title' => 'PostgreSQL Full-Text Search Deep Dive',
102+
'content' => 'Comprehensive guide to PostgreSQL full-text search capabilities: ts_vector, ts_query, ranking algorithms, and GIN indexes for lightning-fast text searches.',
103+
'category' => 'Database',
104+
'tags' => ['postgresql', 'full-text-search', 'database', 'search'],
105+
],
106+
[
107+
'title' => 'Database Migration Strategies with Doctrine',
108+
'content' => 'Learn best practices for database migrations: versioning strategies, rollback procedures, data migrations, and zero-downtime deployment techniques.',
109+
'category' => 'Database',
110+
'tags' => ['doctrine', 'migrations', 'database', 'deployment'],
111+
],
112+
113+
// Testing
114+
[
115+
'title' => 'PHPUnit Testing Best Practices',
116+
'content' => 'Write maintainable and effective unit tests with PHPUnit. Learn about test doubles, mocking, data providers, and achieving high code coverage.',
117+
'category' => 'Testing',
118+
'tags' => ['phpunit', 'testing', 'unit-tests', 'php'],
119+
],
120+
[
121+
'title' => 'End-to-End Testing with Symfony Panther',
122+
'content' => 'Automate browser testing for Symfony applications using Panther. Write reliable E2E tests that verify your application works correctly in real browsers.',
123+
'category' => 'Testing',
124+
'tags' => ['testing', 'e2e', 'panther', 'symfony'],
125+
],
126+
[
127+
'title' => 'Test-Driven Development in PHP',
128+
'content' => 'Master TDD methodology in PHP: write tests first, refactor with confidence, and achieve better code design through test-driven development practices.',
129+
'category' => 'Testing',
130+
'tags' => ['tdd', 'testing', 'php', 'best-practices'],
131+
],
132+
133+
// Architecture
134+
[
135+
'title' => 'Hexagonal Architecture with Symfony',
136+
'content' => 'Implement clean architecture principles in Symfony applications. Learn about ports and adapters, domain-driven design, and maintainable code structure.',
137+
'category' => 'Architecture',
138+
'tags' => ['architecture', 'hexagonal', 'ddd', 'symfony'],
139+
],
140+
[
141+
'title' => 'CQRS Pattern in PHP Applications',
142+
'content' => 'Implement Command Query Responsibility Segregation pattern for scalable applications. Separate read and write operations for better performance and maintainability.',
143+
'category' => 'Architecture',
144+
'tags' => ['cqrs', 'architecture', 'php', 'patterns'],
145+
],
146+
[
147+
'title' => 'Event-Driven Architecture with Symfony Messenger',
148+
'content' => 'Build decoupled applications using Symfony Messenger component. Learn async message handling, event sourcing, and reliable message delivery.',
149+
'category' => 'Architecture',
150+
'tags' => ['messenger', 'events', 'architecture', 'symfony'],
151+
],
152+
153+
// DevOps & Deployment
154+
[
155+
'title' => 'Docker Containerization for Symfony Apps',
156+
'content' => 'Containerize Symfony applications with Docker: multi-stage builds, docker-compose orchestration, production-ready images, and optimization techniques.',
157+
'category' => 'DevOps',
158+
'tags' => ['docker', 'containers', 'devops', 'symfony'],
159+
],
160+
[
161+
'title' => 'CI/CD Pipelines with GitHub Actions',
162+
'content' => 'Automate testing, building, and deployment with GitHub Actions. Set up continuous integration workflows for PHP projects with automated quality checks.',
163+
'category' => 'DevOps',
164+
'tags' => ['ci-cd', 'github-actions', 'automation', 'devops'],
165+
],
166+
[
167+
'title' => 'Kubernetes Deployment for PHP Applications',
168+
'content' => 'Deploy and scale PHP applications on Kubernetes. Learn about pods, services, ingress, auto-scaling, and monitoring strategies for production environments.',
169+
'category' => 'DevOps',
170+
'tags' => ['kubernetes', 'k8s', 'deployment', 'scaling'],
171+
],
172+
173+
// Frontend
174+
[
175+
'title' => 'Symfony UX: Modern Frontend with Twig',
176+
'content' => 'Build reactive user interfaces with Symfony UX components: Turbo, Stimulus, LiveComponents. Create SPA-like experiences without heavy JavaScript frameworks.',
177+
'category' => 'Frontend',
178+
'tags' => ['symfony-ux', 'frontend', 'turbo', 'stimulus'],
179+
],
180+
[
181+
'title' => 'Vue.js Integration with Symfony',
182+
'content' => 'Integrate Vue.js 3 with Symfony backend. Learn about API communication, state management with Pinia, and building progressive web applications.',
183+
'category' => 'Frontend',
184+
'tags' => ['vue', 'javascript', 'frontend', 'symfony'],
185+
],
186+
[
187+
'title' => 'Tailwind CSS in Symfony Projects',
188+
'content' => 'Use Tailwind CSS utility-first approach in Symfony. Configure Webpack Encore, customize themes, and build responsive layouts efficiently.',
189+
'category' => 'Frontend',
190+
'tags' => ['tailwind', 'css', 'frontend', 'symfony'],
191+
],
192+
193+
// AI & Machine Learning
194+
[
195+
'title' => 'Building AI Chatbots with Symfony',
196+
'content' => 'Create intelligent chatbots using Symfony AI components. Integrate OpenAI, Claude, or local LLMs for conversational interfaces in your applications.',
197+
'category' => 'AI',
198+
'tags' => ['ai', 'chatbot', 'symfony', 'llm'],
199+
],
200+
[
201+
'title' => 'Vector Search with pgvector and Symfony',
202+
'content' => 'Implement semantic search using pgvector extension for PostgreSQL. Store and query vector embeddings for AI-powered search capabilities.',
203+
'category' => 'AI',
204+
'tags' => ['vector-search', 'pgvector', 'ai', 'postgresql'],
205+
],
206+
[
207+
'title' => 'RAG Systems: Retrieval Augmented Generation',
208+
'content' => 'Build RAG applications that combine vector search with LLMs. Create AI assistants that answer questions based on your custom knowledge base.',
209+
'category' => 'AI',
210+
'tags' => ['rag', 'ai', 'llm', 'vector-search'],
211+
],
212+
213+
// Additional articles
214+
[
215+
'title' => 'Microservices Communication Patterns',
216+
'content' => 'Explore different communication patterns for microservices: synchronous HTTP/REST, asynchronous messaging, event streaming, and service mesh.',
217+
'category' => 'Architecture',
218+
'tags' => ['microservices', 'architecture', 'patterns', 'communication'],
219+
],
220+
[
221+
'title' => 'Redis Caching Strategies for High Traffic',
222+
'content' => 'Implement effective caching with Redis: cache-aside, write-through, cache invalidation strategies, and handling cache stampede problems.',
223+
'category' => 'Performance',
224+
'tags' => ['redis', 'caching', 'performance', 'scaling'],
225+
],
226+
[
227+
'title' => 'GraphQL API Development with Symfony',
228+
'content' => 'Build flexible GraphQL APIs with Symfony. Learn about schema design, resolvers, N+1 problem solutions, and authentication strategies.',
229+
'category' => 'API',
230+
'tags' => ['graphql', 'api', 'symfony', 'webservices'],
231+
],
232+
[
233+
'title' => 'Elasticsearch Integration for Advanced Search',
234+
'content' => 'Implement full-text search with Elasticsearch. Learn indexing strategies, query DSL, aggregations, and real-time search capabilities.',
235+
'category' => 'Search',
236+
'tags' => ['elasticsearch', 'search', 'full-text', 'indexing'],
237+
],
238+
];
239+
}
240+
}

0 commit comments

Comments
 (0)