|
| 1 | +-- Mega query integration seed for orm-test |
| 2 | +-- Exercises: PostGIS, pgvector, tsvector, BM25 (pg_textsearch), pg_trgm, |
| 3 | +-- scalar filters, relation filters, logical operators, M:N |
| 4 | +-- |
| 5 | +-- Requires postgres-plus:18 image with: postgis, vector, pg_textsearch, pg_trgm |
| 6 | + |
| 7 | +-- Enable extensions |
| 8 | +CREATE EXTENSION IF NOT EXISTS "uuid-ossp"; |
| 9 | +CREATE EXTENSION IF NOT EXISTS postgis; |
| 10 | +CREATE EXTENSION IF NOT EXISTS vector; |
| 11 | +CREATE EXTENSION IF NOT EXISTS pg_textsearch; |
| 12 | +CREATE EXTENSION IF NOT EXISTS pg_trgm; |
| 13 | + |
| 14 | +-- Create test schema |
| 15 | +CREATE SCHEMA IF NOT EXISTS mega_test; |
| 16 | + |
| 17 | +-- Grant usage |
| 18 | +GRANT USAGE ON SCHEMA mega_test TO PUBLIC; |
| 19 | +ALTER DEFAULT PRIVILEGES IN SCHEMA mega_test GRANT ALL ON TABLES TO PUBLIC; |
| 20 | +ALTER DEFAULT PRIVILEGES IN SCHEMA mega_test GRANT ALL ON SEQUENCES TO PUBLIC; |
| 21 | + |
| 22 | +-- ============================================================================ |
| 23 | +-- CATEGORIES (parent table for relation filter tests) |
| 24 | +-- ============================================================================ |
| 25 | +CREATE TABLE mega_test.categories ( |
| 26 | + id serial PRIMARY KEY, |
| 27 | + name text NOT NULL, |
| 28 | + description text |
| 29 | +); |
| 30 | + |
| 31 | +CREATE INDEX idx_categories_name ON mega_test.categories(name); |
| 32 | + |
| 33 | +-- ============================================================================ |
| 34 | +-- LOCATIONS (kitchen sink table: geom, embedding, tsv, BM25, trgm) |
| 35 | +-- ============================================================================ |
| 36 | +CREATE TABLE mega_test.locations ( |
| 37 | + id serial PRIMARY KEY, |
| 38 | + name text NOT NULL, |
| 39 | + body text, |
| 40 | + category_id int REFERENCES mega_test.categories(id), |
| 41 | + geom geometry(Point, 4326), |
| 42 | + embedding vector(3) NOT NULL, |
| 43 | + tsv tsvector, |
| 44 | + is_active boolean NOT NULL DEFAULT true, |
| 45 | + rating numeric(3,1), |
| 46 | + created_at timestamptz NOT NULL DEFAULT now() |
| 47 | +); |
| 48 | + |
| 49 | +CREATE INDEX idx_locations_category_id ON mega_test.locations(category_id); |
| 50 | +CREATE INDEX idx_locations_geom ON mega_test.locations USING gist(geom); |
| 51 | +CREATE INDEX idx_locations_embedding ON mega_test.locations USING ivfflat(embedding vector_cosine_ops) WITH (lists = 1); |
| 52 | +CREATE INDEX idx_locations_tsv ON mega_test.locations USING gin(tsv); |
| 53 | +CREATE INDEX idx_locations_body_bm25 ON mega_test.locations USING bm25(body) WITH (text_config='english'); |
| 54 | +CREATE INDEX idx_locations_name_trgm ON mega_test.locations USING gin(name gin_trgm_ops); |
| 55 | + |
| 56 | +-- ============================================================================ |
| 57 | +-- TAGS (child table for backward relation filter + M:N tests) |
| 58 | +-- ============================================================================ |
| 59 | +CREATE TABLE mega_test.tags ( |
| 60 | + id serial PRIMARY KEY, |
| 61 | + location_id int NOT NULL REFERENCES mega_test.locations(id), |
| 62 | + label text NOT NULL |
| 63 | +); |
| 64 | + |
| 65 | +CREATE INDEX idx_tags_location_id ON mega_test.tags(location_id); |
| 66 | + |
| 67 | +-- ============================================================================ |
| 68 | +-- M:N junction: locations <-> amenities via location_amenities |
| 69 | +-- ============================================================================ |
| 70 | +CREATE TABLE mega_test.amenities ( |
| 71 | + id serial PRIMARY KEY, |
| 72 | + name text NOT NULL UNIQUE |
| 73 | +); |
| 74 | + |
| 75 | +CREATE TABLE mega_test.location_amenities ( |
| 76 | + id serial PRIMARY KEY, |
| 77 | + location_id int NOT NULL REFERENCES mega_test.locations(id) ON DELETE CASCADE, |
| 78 | + amenity_id int NOT NULL REFERENCES mega_test.amenities(id) ON DELETE CASCADE, |
| 79 | + UNIQUE(location_id, amenity_id) |
| 80 | +); |
| 81 | + |
| 82 | +COMMENT ON TABLE mega_test.location_amenities IS E'@behavior +manyToMany'; |
| 83 | + |
| 84 | +CREATE INDEX idx_location_amenities_location ON mega_test.location_amenities(location_id); |
| 85 | +CREATE INDEX idx_location_amenities_amenity ON mega_test.location_amenities(amenity_id); |
| 86 | + |
| 87 | +-- ============================================================================ |
| 88 | +-- VECTOR SEARCH FUNCTION |
| 89 | +-- ============================================================================ |
| 90 | +CREATE FUNCTION mega_test.search_locations( |
| 91 | + query_embedding vector(3), |
| 92 | + result_limit INT DEFAULT 10 |
| 93 | +) |
| 94 | +RETURNS SETOF mega_test.locations |
| 95 | +LANGUAGE sql STABLE |
| 96 | +AS $$ |
| 97 | + SELECT l.* |
| 98 | + FROM mega_test.locations l |
| 99 | + ORDER BY l.embedding <=> query_embedding |
| 100 | + LIMIT result_limit; |
| 101 | +$$; |
| 102 | + |
| 103 | +-- ============================================================================ |
| 104 | +-- SEED DATA |
| 105 | +-- ============================================================================ |
| 106 | + |
| 107 | +-- Categories |
| 108 | +INSERT INTO mega_test.categories (id, name, description) VALUES |
| 109 | + (1, 'Restaurants', 'Places to eat'), |
| 110 | + (2, 'Parks', 'Outdoor green spaces'), |
| 111 | + (3, 'Museums', 'Cultural institutions'); |
| 112 | + |
| 113 | +-- Locations |
| 114 | +INSERT INTO mega_test.locations (id, name, body, category_id, geom, embedding, tsv, is_active, rating) VALUES |
| 115 | + (1, 'Central Park Cafe', |
| 116 | + 'A cozy cafe in the heart of Central Park serving organic coffee and pastries', |
| 117 | + 1, |
| 118 | + ST_SetSRID(ST_MakePoint(-73.968, 40.785), 4326), |
| 119 | + '[1, 0, 0]', |
| 120 | + to_tsvector('english', 'cozy cafe central park organic coffee pastries'), |
| 121 | + true, 4.5), |
| 122 | + (2, 'Brooklyn Bridge Park', |
| 123 | + 'A scenic waterfront park with stunning views of the Manhattan skyline', |
| 124 | + 2, |
| 125 | + ST_SetSRID(ST_MakePoint(-73.996, 40.698), 4326), |
| 126 | + '[0, 1, 0]', |
| 127 | + to_tsvector('english', 'scenic waterfront park stunning views manhattan skyline'), |
| 128 | + true, 4.8), |
| 129 | + (3, 'MoMA', |
| 130 | + 'The Museum of Modern Art featuring contemporary and modern art collections', |
| 131 | + 3, |
| 132 | + ST_SetSRID(ST_MakePoint(-73.978, 40.761), 4326), |
| 133 | + '[0, 0, 1]', |
| 134 | + to_tsvector('english', 'museum modern art contemporary collections'), |
| 135 | + true, 4.7), |
| 136 | + (4, 'Times Square Diner', |
| 137 | + 'A classic American diner near Times Square open twenty four hours', |
| 138 | + 1, |
| 139 | + ST_SetSRID(ST_MakePoint(-73.985, 40.758), 4326), |
| 140 | + '[0.707, 0.707, 0]', |
| 141 | + to_tsvector('english', 'classic american diner times square twenty four hours'), |
| 142 | + false, 3.2), |
| 143 | + (5, 'High Line Park', |
| 144 | + 'An elevated linear park built on historic freight rail lines above the streets', |
| 145 | + 2, |
| 146 | + ST_SetSRID(ST_MakePoint(-74.005, 40.748), 4326), |
| 147 | + '[0.577, 0.577, 0.577]', |
| 148 | + to_tsvector('english', 'elevated linear park historic freight rail lines streets'), |
| 149 | + true, 4.9), |
| 150 | + (6, 'Met Museum', |
| 151 | + 'The Metropolitan Museum of Art with encyclopedic art collections spanning five thousand years', |
| 152 | + 3, |
| 153 | + ST_SetSRID(ST_MakePoint(-73.963, 40.779), 4326), |
| 154 | + '[0, 0.707, 0.707]', |
| 155 | + to_tsvector('english', 'metropolitan museum art encyclopedic collections spanning five thousand years'), |
| 156 | + true, 4.6), |
| 157 | + (7, 'Prospect Park', |
| 158 | + 'A large public park in Brooklyn designed by the creators of Central Park', |
| 159 | + 2, |
| 160 | + ST_SetSRID(ST_MakePoint(-73.969, 40.660), 4326), |
| 161 | + '[0.333, 0.333, 0.333]', |
| 162 | + to_tsvector('english', 'large public park brooklyn creators central park'), |
| 163 | + true, NULL); |
| 164 | + |
| 165 | +-- Tags (backward relation filter) |
| 166 | +INSERT INTO mega_test.tags (location_id, label) VALUES |
| 167 | + (1, 'food'), (1, 'coffee'), |
| 168 | + (2, 'outdoor'), (2, 'scenic'), (2, 'waterfront'), |
| 169 | + (3, 'indoor'), (3, 'art'), |
| 170 | + (4, 'food'), (4, 'late-night'), |
| 171 | + (5, 'outdoor'), (5, 'historic'), |
| 172 | + (6, 'indoor'), (6, 'art'), |
| 173 | + (7, 'outdoor'); |
| 174 | + |
| 175 | +-- Amenities (M:N junction data) |
| 176 | +INSERT INTO mega_test.amenities (id, name) VALUES |
| 177 | + (1, 'WiFi'), (2, 'Parking'), (3, 'Restrooms'), (4, 'Gift Shop'); |
| 178 | + |
| 179 | +INSERT INTO mega_test.location_amenities (location_id, amenity_id) VALUES |
| 180 | + (1, 1), (1, 3), -- Central Park Cafe: WiFi, Restrooms |
| 181 | + (2, 2), (2, 3), -- Brooklyn Bridge Park: Parking, Restrooms |
| 182 | + (3, 1), (3, 3), (3, 4), -- MoMA: WiFi, Restrooms, Gift Shop |
| 183 | + (5, 3), -- High Line Park: Restrooms |
| 184 | + (6, 1), (6, 3), (6, 4); -- Met Museum: WiFi, Restrooms, Gift Shop |
| 185 | + |
| 186 | +-- Reset sequences |
| 187 | +SELECT setval('mega_test.categories_id_seq', 3); |
| 188 | +SELECT setval('mega_test.locations_id_seq', 7); |
| 189 | +SELECT setval('mega_test.tags_id_seq', 14); |
| 190 | +SELECT setval('mega_test.amenities_id_seq', 4); |
| 191 | +SELECT setval('mega_test.location_amenities_id_seq', 11); |
0 commit comments